Git Cheatsheet

60+ essential git commands β€” searchable, filterable, copy with one click.

60 commands

Pro tips

Alias your most-used commands

Add to ~/.gitconfig: [alias]
st = status
co = checkout
br = branch

Use --fixup for clean history

Made a small fix? git commit --fixup=HEAD~2 then git rebase -i --autosquash HEAD~3 to clean up history before pushing.

Never force-push to main

Protect shared branches. Force-push is safe on your own feature branches before a PR, but git push --force-with-lease is safer than --force.

stash with a message

git stash push -m "WIP: auth refactor" β€” so you actually know what each stash contains when you run git stash list.

Frequently asked git questions

How do I undo the last commit without losing my changes? Run git reset --soft HEAD~1 β€” this removes the commit but keeps all your changes staged, ready to recommit.
What's the difference between git pull and git fetch? git fetch downloads changes from the remote but doesn't merge them into your working branch. git pull is fetch + merge (or fetch + rebase if configured). Use fetch when you want to see what changed before merging.
How do I see all branches including remote ones? Run git branch -a to see all local and remote branches. Remote branches show as remotes/origin/branch-name.
How do I merge only specific commits from another branch? Use git cherry-pick commit-hash to apply a single commit. For a range, use git cherry-pick A..B.
How do I find which commit introduced a bug? Use git bisect start, mark a bad commit with git bisect bad, and a known-good commit with git bisect good commit-hash. Git will binary-search through commits until it finds the culprit.
What does git clean do and is it safe? git clean -fd removes untracked files and directories β€” this is irreversible (those files aren't in git's history). Always run git clean -n (dry run) first to see what would be deleted.