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.
Back to Version ControlCommit & Stash 6
git status -sQuick view of working tree status
git add -pInteractively stage changes hunk by hunk
git commit -m "msg"Commit staged changes
git commit --amendAmend the last commit (message or content)
git stashTemporarily save changes, useful for switching branches
git stash popRestore the most recent stash
Branch & Merge 5
git branch -aList local and remote branches
git switch -c feat/xCreate and switch to a new branch (new syntax)
git merge --no-ff feat/xMerge branch and preserve merge commit
git rebase mainRebase 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~1Undo last commit, keep changes staged
git reset --hard HEAD~1Discard 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 -vView remote repository URLs
git fetch --pruneFetch and prune deleted remote branch references
git log --oneline --graphGraphical commit history view
git reflogView HEAD movement history, lifesaver for recovering lost commits
git blame <file>Annotate file line by line with last author and commit
FAQ 5
Q: How do I undo the last commit?A: git reset --soft HEAD~1 keeps changes staged; git reset --hard HEAD~1 discards them (use with caution). For pushed commits use git revert <sha>.
Q: What is the difference between git merge and git rebase?A: merge preserves branch history (creates a merge commit) and suits shared branches; rebase linearizes history (rewrites commits) and suits personal cleanup.
Q: How do I change the last commit message?A: git commit --amend edits the last commit message or content. If already pushed, use git push --force-with-lease and mind team safety.
Q: How do I stash unfinished changes?A: git stash saves working tree changes; after switching branches use git stash pop to restore. git stash list shows all stashes.
Q: How do I resolve a merge conflict?A: Conflicted files are marked with <<<<<<< HEAD and >>>>>>> branch. Edit manually, then git add <file> and git commit to finish.
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.
Official References
Commands are compiled from the official docs below. Click to verify the latest usage.
Maintained by LaoHand
Publicly updated on Jul 21, 2026, continuously proofread against official docs.
Contact Us
Wrong command or description? Send us corrections, business inquiries or product feedback by email.
Contact Us