Skip to content

Git cheat sheet

The Git commands for committing, branching, merging, rebasing, undoing and recovering that the Git & Version Control course uses, on one printable page.

Everyday

CommandWhat it does
git status / git diff / git diff --stagedwhat changed / unstaged / staged
git add -pstage part of a file
git commit -m "msg"commit what is staged
git log --oneline --graph --allthe history as a graph
git show <commit>one commit in full
git blame filewho last changed each line

Branches

CommandWhat it does
git switch -c featurecreate and move to a branch
git switch mainmove back
git branch -d featuredelete a merged branch
git merge featuremerge it into the current branch
git rebase mainreplay my commits on top of main
git rebase -i mainsquash, reword or drop my commits

Conflicts

Edit the files between <<<<<<< and >>>>>>>, then git add file and git commit (merge) or git rebase --continue (rebase). Give up: git merge --abort / git rebase --abort.

Remotes

CommandWhat it does
git fetch / git pull --rebasedownload / download and rebase
git push -u origin featurefirst push of a branch
git push --force-with-leasepush a rewritten branch, safely

Undo

SituationCommand
discard changes to a filegit restore file
unstage a filegit restore --staged file
fix the last commitgit commit --amend
undo last commit, keep changesgit reset --soft HEAD~1
throw away last commit and changesgit reset --hard HEAD~1
undo a pushed commitgit revert <commit>
copy one commit heregit cherry-pick <commit>
put work asidegit stash / git stash pop
find a lost commitgit reflog

Find the commit that broke it

git bisect start
git bisect bad              # this commit is broken
git bisect good v1.2        # this one worked
# test, then: git bisect good | git bisect bad … until Git names the commit
git bisect run ./test.sh    # or let a script decide
git bisect reset

Find any command in the Git command finder.

Practise it on a real machineGit & Version Control course
Open the lab