Git Cheat Sheet

The commands you'll use most, grouped by task, with the lesson that explains each one. Keep this page open while you work until the commands become second nature.

Setup

CommandDoesLesson
git config --global user.name "Name"Set the name recorded in your commits03
git config --global user.email "you@example.com"Set your commit email03
git config --global init.defaultBranch mainName the first branch main03
git config --global --listShow your settings03

Starting a repository

CommandDoesLesson
git initTurn the current folder into a repository05
git clone <url> [folder]Copy an existing repository33

Everyday work

CommandDoesLesson
git status / git status -sWhat's changed and staged07
git add <file> / git add .Stage changes08
git add -pStage selected parts of files08
git commit -m "Message"Commit the staged changes09
git commit -am "Message"Stage tracked changes and commit09
git diff / git diff --stagedUnstaged / staged changes, line by line12
git log --oneline --graph --allCompact history of all branches11
git show <commit>One commit and its changes13
git mv old new / git rm fileRename / delete and stage it15

Undoing

CommandDoesLesson
git restore <file>Discard uncommitted edits (permanent!)16
git restore --staged <file>Unstage, keeping the edits16
git commit --amendFix the last (unpushed) commit17
git reset --soft HEAD~1Undo the last commit, keep changes staged18
git reset --hard HEAD~1Remove the last commit and its changes18
git revert <commit>Undo a (pushed) commit with a new commit19
git reflogFind "lost" commits20
git clean -n / -fdPreview / delete untracked files21

Branches

CommandDoesLesson
git branch / git branch -vvList branches (with upstreams)22
git switch -c <name>Create a branch and switch to it23
git switch <name> / git switch -Switch branch / back to the previous one23
git merge <branch>Merge a branch into the current one24
git merge --abortCancel a conflicted merge25
git rebase mainReplay the current branch on top of main26
git rebase -i HEAD~nSquash, reword, reorder or drop recent commits27
git cherry-pick <commit>Copy one commit onto the current branch28
git stash / git stash popShelve / restore uncommitted work29
git branch -d <name>Delete a merged branch22

Remotes

CommandDoesLesson
git remote add origin <url>Connect a remote30
git remote -vList remotes30
git push -u origin <branch>First push of a branch, setting its upstream32
git push / git pullSend / receive commits32, 34
git fetch --pruneDownload without merging; drop deleted branches35
git pull --rebasePull, replaying your commits on top34
git push --force-with-leaseOverwrite your own remote branch safely32
git push origin --delete <branch>Delete a remote branch35

History and releases

CommandDoesLesson
git tag -a v1.0.0 -m "…"Create an annotated tag39
git push origin --tagsPush tags39
git grep "text"Search tracked files40
git log -S "text"Commits that added or removed text40
git blame <file>Last change to each line40
git bisect start / good / badBinary-search for a bad commit41

Naming commits

ReferenceMeans
HEADWhere you are now
HEAD~1, HEAD~3One / three commits back
main..featureCommits on feature not on main
origin/mainThe remote's main, as of your last fetch
HEAD@{2}Where HEAD was two moves ago (reflog)

The golden rules

  1. Run git status before and after anything important.
  2. Commit small, commit often, write clear messages.
  3. Never rewrite history that others have pulled — use revert on shared branches.
  4. Never commit secrets; add a .gitignore before the first commit.
  5. Committed work is almost never lost — check the reflog before panicking.