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
💡 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.