Git in Cheatsheet
| (5 intermediate revisions by the same user not shown) | |||
| Line 83: | Line 83: | ||
git branch # list local branches | git branch # list local branches | ||
git branch -a # include remote-tracking branches | git branch -a # include remote-tracking branches | ||
git switch -c feature | git branch feature-branch # create a copy of current branch named feature-branch and switch to it | ||
git branch main-backup main # create a copy of main named main-backup and switch to it | |||
git switch -c feature-branch # = branch feature-branch + switch to it | |||
git switch -c main-backup main # = git branch main-backup main + switch to it | |||
git switch feature # switch to an existing branch | git switch feature # switch to an existing branch | ||
git switch - # switch back to the previous branch | git switch - # switch back to the previous branch | ||
| Line 98: | Line 101: | ||
== Rebase == | == Rebase == | ||
First create a backup of the branch to be rebased: | |||
<syntaxhighlight lang="bash" copy> | <syntaxhighlight lang="bash" copy> | ||
git rebase main # | git branch main-backup main # main-backup is an exact copy of main | ||
git rebase -i HEAD~3 # | git switch main # switches back to target the main branch | ||
</syntaxhighlight> | |||
<syntaxhighlight lang="bash" copy> | |||
git rebase main # replays current branch on top of main | |||
git rebase -i HEAD~3 # interactively reorganises the last 3 commits on current branch | |||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 120: | Line 129: | ||
<syntaxhighlight lang="bash" copy> | <syntaxhighlight lang="bash" copy> | ||
git remote -v # list remotes | git remote -v # list remotes | ||
git remote add origin <url> # connect a remote | git remote add <remote name:e.g., origin> <url> # connect a remote | ||
git fetch | git fetch <remote name> # download commits, don't touch your work | ||
git pull # fetch + merge | git pull # fetch + merge | ||
git push # upload your commits | git push # upload your commits | ||
| Line 136: | Line 145: | ||
== Undoing == | == Undoing == | ||
<syntaxhighlight lang="bash" copy> | |||
git restore file.py # discard unstaged changes to a file | |||
git restore --staged file.py # unstage a file | |||
git commit --amend # fix the last commit (message / forgotten file) | |||
git reset HEAD~1 # undo the last commit, unstage the changes | |||
git reset --soft HEAD~1 # undo the last commit, keep changes staged | |||
git reset --hard HEAD~1 # undo the last commit and drop everything | |||
git revert <hash> # undo a commit that is already pushed | |||
</syntaxhighlight> | |||
<blockquote> | <blockquote> | ||
'''<syntaxhighlight lang="bash" inline>git reset --hard</syntaxhighlight> | '''<syntaxhighlight lang="bash" inline>git reset --hard</syntaxhighlight> can be destructive:''' it trashes the resetted commit ''and'' uncommitted edits. | ||
However, partial recovery immediately after accidental trigger is possible. | |||
<syntaxhighlight lang="bash" copy> | |||
git reflog # lists previous HEAD locations | |||
</syntaxhighlight> | |||
Expected output: | |||
<pre> | |||
abc1234 HEAD@{0}: reset: moving to HEAD~3 | |||
def5678 HEAD@{1}: commit: Latest work you want back | |||
</pre> | |||
Recover committed changes | |||
<syntaxhighlight lang="bash" copy> | |||
git reset --hard "HEAD@{1}" # reset once more to the desired commit just before reset. | |||
</syntaxhighlight> | |||
Attempt to recover staged changes: | |||
<syntaxhighlight lang="bash" copy> | |||
git fsck --unreachable # search for dangling tip objects (often leftovers from reset --hard) | |||
</syntaxhighlight> | |||
Changes to untracked files are normally kept on disk. Unstaged changes to tracked files are '''permanently lost'''. | |||
</blockquote> | </blockquote> | ||
<blockquote> | <blockquote> | ||
''' | '''<code>amend</code> rewrites history''' (new commit hashes). Normally, it should only be used on local commits that are not yet shared. | ||
</blockquote> | </blockquote> | ||
== Stash == | == Stash == | ||
Latest revision as of 12:34, 9 October 2026
Quick reference for smart people — part of our dev cheatsheets collection.
Git is a distributed version control system that tracks changes to files and coordinates work between people on the same codebase.
New to Git? Learn Git Branching is a visual, interactive tutorial; Pro Git is the free, thorough book.
Basic example: clone, edit, commit, push
git clone https://github.com/user/repo.git
cd repo
git switch -c my-feature # branch off main
# ... edit files ...
git add . # stage all changes
git commit -m "Add my feature" # snapshot them
git push -u origin my-feature # share on the remote, remember upstream
Every change follows the same loop: edit → stage (git add) → commit → push. The working tree is what you edit, the index is what you stage, the remote is where you share.
Setup
git config --global user.name "Ada Lovelace"
git config --global user.email [email protected]
git config --global init.defaultBranch main
git config --global alias.co checkout # `git co` works from now on
git config --global pull.rebase true # pull = fetch + rebase
| Command | Effect |
|---|---|
git config --global user.name "Ada" |
who you are, for every repo — needed before the first commit |
git config --global init.defaultBranch main |
new repos default to main |
git config --global alias.co checkout |
create a short alias |
git config --global --list |
show all config |
Start a repository:
git init # new repo in the current directory
git init my-project # ... in a subdirectory
git clone <url> # copy an existing repository
git clone <url> dir # ... into a given directory
Daily workflow
git status # what changed?
git diff # unstaged changes
git diff --staged # staged changes
git add file.py # stage one file
git add . # stage all changes
git add -p # stage hunks interactively
git commit -m "Fix typo" # commit the staged changes
git commit -am "Fix typo" # stage tracked changes + commit in one go
git commit -amonly stages tracked files. A brand-new file is untracked and still needs an explicitgit add.
Inspecting history
git log --oneline # one line per commit
git log --oneline --graph --all # branch topology
git log -p # full diffs
git log --follow -- file.py # history of a single file
git show <hash> # one commit in detail
git blame file.py # who changed each line
Branching & merging
git branch # list local branches
git branch -a # include remote-tracking branches
git branch feature-branch # create a copy of current branch named feature-branch and switch to it
git branch main-backup main # create a copy of main named main-backup and switch to it
git switch -c feature-branch # = branch feature-branch + switch to it
git switch -c main-backup main # = git branch main-backup main + switch to it
git switch feature # switch to an existing branch
git switch - # switch back to the previous branch
git merge feature # merge feature into the current branch
git branch -d feature # delete a merged branch
git branch -D feature # force-delete an unmerged branch
Git before 2.23 has no
git switch/git restore. Usegit checkout -b featureto branch,git checkout featureto switch, andgit checkout -- file.pyto discard changes.
Rebase
First create a backup of the branch to be rebased:
git branch main-backup main # main-backup is an exact copy of main
git switch main # switches back to target the main branch
git rebase main # replays current branch on top of main
git rebase -i HEAD~3 # interactively reorganises the last 3 commits on current branch
Merge conflicts
# git status shows "both modified"; files contain markers:
# <<<<<<< HEAD ... ======= ... >>>>>>> feature
# edit the file to keep the right lines, then:
git add <resolved files>
git commit # after a merge
git rebase --continue # after a rebase
git merge --abort # back out of the merge entirely
git rebase --abort # ... or of the rebase
Remotes
git remote -v # list remotes
git remote add <remote name:e.g., origin> <url> # connect a remote
git fetch <remote name> # download commits, don't touch your work
git pull # fetch + merge
git push # upload your commits
git push -u origin feature # first push of a branch: set upstream
git push origin --delete feature # delete a branch on the remote
git tag v1.0 # tag the current commit
git push origin v1.0 # share the tag
pull is fetch + merge. It can create merge commits. Prefer a linear history with
git pull --rebase, or set it once withgit config --global pull.rebase true.
Undoing
git restore file.py # discard unstaged changes to a file
git restore --staged file.py # unstage a file
git commit --amend # fix the last commit (message / forgotten file)
git reset HEAD~1 # undo the last commit, unstage the changes
git reset --soft HEAD~1 # undo the last commit, keep changes staged
git reset --hard HEAD~1 # undo the last commit and drop everything
git revert <hash> # undo a commit that is already pushed
git reset --hardcan be destructive: it trashes the resetted commit and uncommitted edits.However, partial recovery immediately after accidental trigger is possible.
git reflog # lists previous HEAD locationsExpected output:
abc1234 HEAD@{0}: reset: moving to HEAD~3 def5678 HEAD@{1}: commit: Latest work you want backRecover committed changes
git reset --hard "HEAD@{1}" # reset once more to the desired commit just before reset.Attempt to recover staged changes:
git fsck --unreachable # search for dangling tip objects (often leftovers from reset --hard)Changes to untracked files are normally kept on disk. Unstaged changes to tracked files are permanently lost.
amendrewrites history (new commit hashes). Normally, it should only be used on local commits that are not yet shared.
Stash
Put uncommitted work aside to switch branches, then bring it back. Stashes are not tied to a branch — a stack you can pop anywhere.
git stash # stash tracked changes
git stash -u # also stash untracked files
git stash -m "WIP" # stash with a message
git stash list # see what is stashed
git stash show -p # inspect the newest stash's diff
pop vs apply — pop restores and drops the stash; apply restores and keeps it:
git stash pop # restore newest stash, then drop it
git stash apply # restore, keep the stash
git stash apply stash@{1} # restore a specific stash
git stash drop stash@{1} # delete a specific stash
git stash clear # delete all stashes
git stash branch fix # new branch at the stash point, pop onto it
Untracked files are left behind by default.
git stashonly stashes tracked changes — a brand-new file survives in the working tree. Add-ufor untracked files too, or-ato also include ignored files.
A conflicting pop keeps the stash. Restoring onto a branch that changed the same lines leaves the stash in place: resolve like a merge conflict, then
git stash droponce the work is safely committed.
Further reading
- Git documentation — the official docs
- Learn Git Branching — interactive, visual tutorial — the recommended start
- Pro Git — the free book
- Oh Shit, Git!?! — quick fixes for common mistakes