Git Commands Cheatsheet
Commands grouped by what you are trying to do, with recovery recipes and notes on what can lose work.
By BoltQuickTools team · Updated November 8, 2026
In the examples, abc123 stands for a commit hash. git switch and git restore need Git 2.23 or newer. Before anything risky, git branch backup-here costs nothing and gives you a way back.
Daily basics
Status and history
git status -sb # short status with branch git log --oneline -20 # last 20 commits git log --graph --oneline --all -20 git diff # working tree vs staging area git diff --staged # staging area vs last commit git diff HEAD # working tree vs last commit
Make a commit
git add . # stage everything under this directory git add -A # stage everything in the repository git add -p # choose hunks one at a time git commit -m "message" git commit --amend # replace the most recent commit git commit --amend --no-edit # add staged changes, keep the message
Amending creates a new commit with a new hash. If the original was pushed, you will need a force push.
Branches
git branch -vv # list, with upstream and last commit git switch main # change branch git switch - # back to the previous branch git switch -c feature/x # create and change git branch -m new-name # rename the current branch git branch -d feature/x # delete if merged git branch -D feature/x # delete even if unmerged git branch --contains abc123 # which branches have this commit
Sync with a remote
git fetch --prune # update, dropping deleted remote branches git merge --ff-only origin/main # fast-forward, or refuse git pull --rebase # replay local commits on top of remote git push git push -u origin feature/x # first push of a new branch
Comparing and inspecting
git show HEAD # last commit git show abc123:path/to/file.js # file as of a commit git blame -L 10,30 file.js # who last changed lines 10 to 30 git log --follow file.js # history across renames git log main..feature # commits on feature but not main git diff main...feature # feature's changes since it branched from main git log --diff-filter=A -- file.js # the commit that added a file
Two dots and three dots mean different things. For git log, A..B is the list you usually want. For git diff, A...B is what a pull request shows.
Rebase and merge
Update a feature branch with latest main
git switch feature/x git fetch origin git rebase origin/main # fix conflicts, git add the files, then: git rebase --continue git push --force-with-lease
--force-with-lease refuses to overwrite the remote if it has moved since your last fetch. A background git fetch can update that reference and weaken the check; adding --force-if-includes (Git 2.30+) guards against that. Do not rewrite branches other people are working on.
Interactive rebase
git rebase -i HEAD~5 # Change "pick" to: # reword keep the commit, edit its message # edit stop here so you can amend or split it # squash meld into the previous commit, combine messages # fixup meld into the previous commit, discard this message # drop delete the commit # Reorder lines to reorder commits.
Fix an older commit in one step
git add -p git commit --fixup=abc123 git rebase -i --autosquash abc123^
Merge a feature branch
git switch main git merge --no-ff feature/x # always record a merge commit git merge --squash feature/x # one combined change, then git commit git merge --abort # give up during conflicts
Conflicts
git diff --name-only --diff-filter=U # files still in conflict git checkout --ours file.js git checkout --theirs file.js git config --global merge.conflictstyle zdiff3 # also show the common ancestor (Git 2.35+) git config --global rerere.enabled true # reuse earlier resolutions
During a rebase, "ours" is the branch you are rebasing onto and "theirs" is your own commit: the reverse of what you might expect.
Stash
git stash push -m "wip: login" # save tracked changes with a name
git stash -u # also include untracked files
git stash list
git stash show -p stash@{1} # view a stash as a diff
git stash pop # apply most recent and remove it
git stash apply stash@{2} # apply, keep it in the list
git stash drop stash@{2}
git stash branch new-branch # new branch from where the stash was madeUndo and recover
Which undo do you need?
git restore --staged file.js # unstage, keep the changes git reset --soft HEAD~1 # undo commit, keep changes staged git reset HEAD~1 # undo commit, keep changes unstaged git revert abc123 # new commit reversing abc123 (safe if pushed) git revert -m 1 abc123 # revert a merge commit, keeping parent 1
Recover a lost commit
git reflog # every position HEAD has been at git branch rescue abc123 # put a branch on the commit you found git reset --hard ORIG_HEAD # undo the last reset, merge or rebase
The reflog is local to your clone. By default its entries are kept for 90 days, or 30 days for commits no longer reachable from a branch, after which git gc can remove the commits. Git runs that cleanup automatically.
Recover something added but never committed
git fsck --lost-found # lists dangling blobs git show <blob-hash> # inspect one
Works only for content that was staged at some point.
Throw away work (destructive)
git restore file.js # discard unstaged edits to one file git reset --hard HEAD # discard all changes to tracked files git clean -nd # preview untracked files to delete git clean -fd # delete untracked files and directories
These have no normal undo. Always run clean -n first. reset --hard leaves untracked files in place.
Stop tracking a file you meant to ignore
echo "secrets.env" >> .gitignore git rm --cached secrets.env git commit -m "Stop tracking secrets.env"
The file stays in earlier commits. If it held credentials, rotate them. Removing a file from history needs a tool such as git filter-repo and a force push.
Finding things
Find the commit that introduced a bug
git bisect start git bisect bad # current commit is bad git bisect good v1.0 # this old version was good # Git checks out a midpoint. Test it, then: git bisect good # or: git bisect bad git bisect reset # return to where you started git bisect run npm test # automate: exit 0 = good, non-zero = bad
Search
git log -S "stringToFind" # commits changing how often a string occurs git log -G "regex" # commits whose diff has a matching line git log --grep="bug" # search commit messages git log --author="Alice" git log --since="2 weeks ago" git grep "TODO" v1.0 # search files at a commit or tag
Remotes
git remote -v git remote add upstream <url> git remote set-url origin <url> git push origin --delete old-branch git ls-remote --heads origin # list remote branches without fetching
Tags
git tag v1.0.0 # lightweight git tag -a v1.0.0 -m "Release notes" # annotated, the usual choice for releases git push origin v1.0.0 git push --follow-tags # push commits plus their annotated tags git tag -d v1.0.0 git push origin --delete refs/tags/v1.0.0
Cherry-pick
git cherry-pick abc123 git cherry-pick abc123..def456 # after abc123, through def456 git cherry-pick abc123^..def456 # including abc123 git cherry-pick -x abc123 # note the source hash in the message git cherry-pick --abort
Work on two branches at once
git worktree add ../hotfix hotfix/x # second checkout in another folder git worktree list git worktree remove ../hotfix
Avoids stashing when you need to look at another branch. A branch can be checked out in only one worktree at a time.
Useful aliases
# Add to ~/.gitconfig: [alias] st = status -sb sw = switch unstage = restore --staged last = log -1 HEAD lg = log --graph --pretty=format:'%C(yellow)%h%Creset -%C(red)%d%Creset %s %C(cyan)(%cr)%Creset' --abbrev-commit amend = commit --amend --no-edit undo = reset HEAD~1 --mixed pushf = push --force-with-lease
Last updated