Git Cheat Sheet
A searchable Git cheat sheet: switching and restoring, safe force pushes, interactive rebase and fixups, stash, log searches, blame, bisect, reflog, undo and worktrees.
Free · No sign-up · Your inputs stay in your browser
Reference
Git cheat sheet
| Command | Does | Notes |
|---|---|---|
| git switch -c feature/x | Create a branch and switch to it | The modern form of git checkout -b. |
| git restore file | Discard unstaged changes to a file | git restore --staged file unstages instead. |
| git add -p | Stage changes hunk by hunk | Review what goes into the commit. |
| git commit --amend --no-edit | Add staged changes to the last commit | Only before pushing, or push with --force-with-lease. |
| git push --force-with-lease | Force push safely | Refuses if someone else pushed in the meantime; plain --force does not. |
| git pull --rebase | Update without a merge commit | git config --global pull.rebase true makes it the default. |
| git rebase -i HEAD~5 | Squash, reorder or reword recent commits | Interactive; mark commits fixup or squash. |
| git commit --fixup=<sha> && git rebase -i --autosquash <sha>~1 | Fix an older commit | The fixup is folded into the commit automatically. |
| git stash push -m "wip" && git stash pop | Park and restore work in progress | git stash list shows them; -u includes untracked files. |
| git log --oneline --graph --all | See the branch structure | Add --decorate for branch names on older Git. |
| git log -S 'functionName' --oneline | Find commits that added or removed a string | The pickaxe: how to find when code appeared. |
| git log --follow -p -- path/to/file | History of one file, across renames | |
| git blame -w -C -L 10,40 file | Who last changed lines 10-40 | -w ignores whitespace, -C follows code moved from other files. |
| git bisect start BAD GOOD | Binary-search for the commit that broke something | Then git bisect good / bad until it names the commit; git bisect run automates it. |
| git reflog | Every position HEAD has been in | How to recover a branch lost to a reset or rebase. |
| git reset --soft HEAD~1 | Undo the last commit, keep the changes staged | --mixed (default) unstages them, --hard deletes them. |
| git revert <sha> | Undo a pushed commit with a new commit | Safe on shared branches; reset rewrites history. |
| git cherry-pick <sha> | Apply one commit onto the current branch | -x records where it came from. |
| git clean -fdn | Preview deleting untracked files | Remove the n to actually delete; add -x for ignored files too. |
| git branch --merged main | grep -v main | xargs git branch -d | Delete local branches already merged | |
| git fetch --prune | Drop remote-tracking branches deleted on the server | git config --global fetch.prune true makes it the default. |
| git worktree add ../hotfix main | Check out a second branch in another folder | No stashing needed to work on two branches at once. |
| git diff --staged | What will be committed | git diff alone shows unstaged changes. |
| git diff main...feature | What a branch changed since it forked | Three dots: changes on feature only, as a pull request shows them. |
| git shortlog -sn --no-merges | Commits per author | |
| git tag -a v1.2.0 -m "v1.2.0" && git push --tags | Create and push an annotated tag | Annotated tags carry a date and author; release tools expect them. |
| git config --global init.defaultBranch main | Name new repositories' first branch main | |
| git rm --cached file | Stop tracking a file but keep it on disk | Then add it to .gitignore. |
| git submodule update --init --recursive | Fetch submodules after cloning | Or clone with --recurse-submodules. |
| git maintenance start | Schedule background repository optimisation | Keeps large repositories fast. |
For Git 2.23 and later, which split checkout into switch (branches) and restore (files). Anything that rewrites history (amend, rebase, reset) is safe on commits you have not pushed; on shared branches, prefer revert.
About this tool
Method, assumptions, practical next steps, and limits.
The modern commands
Git 2.23 split checkout into switch, for branches, and restore, for files, because one command doing both was a common source of lost work. The older forms still work. The Git reference documents every command.
Rewriting history safely
amend, rebase and reset rewrite commits, which is fine for commits nobody else has. Once pushed to a shared branch, prefer revert, which adds a new commit, and if a rewrite is needed, push with --force-with-lease so you do not overwrite someone else's work.