Git Cheatsheet - Command Reference

All essential Git commands organized by Commit, Branch, Revert, Remote, and Diagnostics. Find solutions when you mess up a commit or branch.

Version Control·21 commands·Last updated 2026-07-21
Back to Version Control

Commit & Stash 6

git status -s
Quick view of working tree status
git add -p
Interactively stage changes hunk by hunk
git commit -m "msg"
Commit staged changes
git commit --amend
Amend the last commit (message or content)
git stash
Temporarily save changes, useful for switching branches
git stash pop
Restore the most recent stash

Branch & Merge 5

git branch -a
List local and remote branches
git switch -c feat/x
Create and switch to a new branch (new syntax)
git merge --no-ff feat/x
Merge branch and preserve merge commit
git rebase main
Rebase current branch onto main for linear history
git cherry-pick <sha>
Apply a specific commit to the current branch

Revert & Undo 5

git restore <file>
Discard working tree changes (new syntax)
git restore --staged <file>
Unstage a file, keep changes
git reset --soft HEAD~1
Undo last commit, keep changes staged
git reset --hard HEAD~1
Discard last commit and changes entirely, use with caution
git revert <sha>
Create a reverse commit to undo changes, safe for shared branches

Remote & Diagnostics 5

git remote -v
View remote repository URLs
git fetch --prune
Fetch and prune deleted remote branch references
git log --oneline --graph
Graphical commit history view
git reflog
View HEAD movement history, lifesaver for recovering lost commits
git blame <file>
Annotate file line by line with last author and commit

💡 Tips

  • reset --hard discards changes — double-check before running; use git reflog to recover if truly lost.
  • Use revert to undo commits already pushed to shared branches — never reset and force-push or you will break your teammates.
  • switch/restore are newer commands with clearer semantics than checkout; older scripts still use checkout.