why you need a cheat sheet for git
Git is one of those tools where you use maybe 15 commands constantly, forget the exact flags, and then the man pages greet you with three paragraphs of abstract nouns when you want to know how to undo a commit.
The commands themselves are not hard. The problem is that git's vocabulary is inconsistent. checkout does three unrelated things. reset has three modes that all do something different. restore exists because the git team eventually admitted checkout was a mess. You won't memorize all of this.
staging interactively
Instead of git add . and crossing your fingers, use the patch flag to stage chunks of a file selectively:
git add -p
Git shows you each hunk and asks what to do. y to stage it, n to skip it, s to split it into smaller pieces. Useful when you've mixed two unrelated changes in one file and want clean commits.
undoing things without panicking
If you committed something and haven't pushed yet:
# keep the changes, just undo the commit
git reset --soft HEAD~1
# undo the commit and unstage everything
git reset HEAD~1
# nuclear: undo the commit and discard the changes
git reset --hard HEAD~1
If you've already pushed, use git revert HEAD instead. It makes a new commit that reverses the previous one, which is safer than rewriting history on a shared branch. Learn this the hard way once and you won't forget it.
seeing what changed
# what changed in the last commit
git show
# what you're about to commit
git diff --staged
# one-line log with branch graph
git log --oneline --graph --decorate
--staged is the flag you look up every time. --cached does the same thing (they're aliases), but --staged is easier to remember.
get the full reference
The Git cheat sheet covers branches, remotes, stashing, rebasing, and the rest of the commands you reach for weekly. PDF and Markdown.