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·26 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

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