Skip to content

Git Commands Cheat Sheet

Branching, merging, rebasing, stashing, and the recovery commands that get you out of a bad state without losing work.

Branching & Merging

git checkout -b feature/xCreate and switch to a new branch
git switch -c feature/xModern equivalent of checkout -b
git merge feature/xMerge a branch into the current branch
git branch -d feature/xDelete a branch that's been merged
git branch -D feature/xForce-delete a branch, merged or not
git branch -vvList branches with their upstream tracking status

Rebasing & History

git rebase mainReplay current branch's commits on top of main
git rebase -i HEAD~5Interactively squash/reorder/edit the last 5 commits
git cherry-pick <sha>Apply a single commit from another branch
git log --oneline --graph --allCompact visual history of all branches
git blame file.jsShow which commit last changed each line

Undoing Things

git restore file.jsDiscard uncommitted changes to a file
git reset --soft HEAD~1Undo last commit, keep changes staged
git reset --hard HEAD~1Undo last commit, discard changes entirely
git revert <sha>Create a new commit that undoes a previous commit (safe on shared history)
git reflogShow every HEAD movement — recovers 'lost' commits after a bad reset

Stashing & Remotes

git stashTemporarily shelve uncommitted changes
git stash popReapply and remove the most recent stash
git fetch originDownload remote changes without merging
git pull --rebaseFetch and rebase local commits on top instead of merging
git push -u origin feature/xPush a new branch and set its upstream

Related Articles

FAQ

Frequently Asked Questions

Yes — every cheat sheet on this site is free to view and download as a PDF, with no sign-up required.