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
| Command | Does | Lesson |
git config --global user.name "Name" | Set the name recorded in your commits | 03 |
git config --global user.email "you@example.com" | Set your commit email | 03 |
git config --global init.defaultBranch main | Name the first branch main | 03 |
git config --global --list | Show your settings | 03 |
Starting a repository
| Command | Does | Lesson |
git init | Turn the current folder into a repository | 05 |
git clone <url> [folder] | Copy an existing repository | 33 |
Everyday work
| Command | Does | Lesson |
git status / git status -s | What's changed and staged | 07 |
git add <file> / git add . | Stage changes | 08 |
git add -p | Stage selected parts of files | 08 |
git commit -m "Message" | Commit the staged changes | 09 |
git commit -am "Message" | Stage tracked changes and commit | 09 |
git diff / git diff --staged | Unstaged / staged changes, line by line | 12 |
git log --oneline --graph --all | Compact history of all branches | 11 |
git show <commit> | One commit and its changes | 13 |
git mv old new / git rm file | Rename / delete and stage it | 15 |
Undoing
| Command | Does | Lesson |
git restore <file> | Discard uncommitted edits (permanent!) | 16 |
git restore --staged <file> | Unstage, keeping the edits | 16 |
git commit --amend | Fix the last (unpushed) commit | 17 |
git reset --soft HEAD~1 | Undo the last commit, keep changes staged | 18 |
git reset --hard HEAD~1 | Remove the last commit and its changes | 18 |
git revert <commit> | Undo a (pushed) commit with a new commit | 19 |
git reflog | Find "lost" commits | 20 |
git clean -n / -fd | Preview / delete untracked files | 21 |
Branches
| Command | Does | Lesson |
git branch / git branch -vv | List branches (with upstreams) | 22 |
git switch -c <name> | Create a branch and switch to it | 23 |
git switch <name> / git switch - | Switch branch / back to the previous one | 23 |
git merge <branch> | Merge a branch into the current one | 24 |
git merge --abort | Cancel a conflicted merge | 25 |
git rebase main | Replay the current branch on top of main | 26 |
git rebase -i HEAD~n | Squash, reword, reorder or drop recent commits | 27 |
git cherry-pick <commit> | Copy one commit onto the current branch | 28 |
git stash / git stash pop | Shelve / restore uncommitted work | 29 |
git branch -d <name> | Delete a merged branch | 22 |
Remotes
| Command | Does | Lesson |
git remote add origin <url> | Connect a remote | 30 |
git remote -v | List remotes | 30 |
git push -u origin <branch> | First push of a branch, setting its upstream | 32 |
git push / git pull | Send / receive commits | 32, 34 |
git fetch --prune | Download without merging; drop deleted branches | 35 |
git pull --rebase | Pull, replaying your commits on top | 34 |
git push --force-with-lease | Overwrite your own remote branch safely | 32 |
git push origin --delete <branch> | Delete a remote branch | 35 |
History and releases
| Command | Does | Lesson |
git tag -a v1.0.0 -m "…" | Create an annotated tag | 39 |
git push origin --tags | Push tags | 39 |
git grep "text" | Search tracked files | 40 |
git log -S "text" | Commits that added or removed text | 40 |
git blame <file> | Last change to each line | 40 |
git bisect start / good / bad | Binary-search for a bad commit | 41 |
Naming commits
| Reference | Means |
HEAD | Where you are now |
HEAD~1, HEAD~3 | One / three commits back |
main..feature | Commits on feature not on main |
origin/main | The remote's main, as of your last fetch |
HEAD@{2} | Where HEAD was two moves ago (reflog) |
The golden rules
- Run
git status before and after anything important.
- Commit small, commit often, write clear messages.
- Never rewrite history that others have pulled — use
revert on shared branches.
- Never commit secrets; add a
.gitignore before the first commit.
- Committed work is almost never lost — check the reflog before panicking.