06-22-2026, 02:09 PM
Git is one of those tools where most people learn just enough to get by and then stay at that level forever. Here's a reference for the commands that go beyond the basics and are worth knowing.
The stuff everyone knows
Branching properly
Staging selectively
Fixing mistakes
Investigating history
Finding regressions
Stashing work in progress
Cleaning up
A good Git mental model: commits are snapshots, branches are pointers to commits, and most "scary" commands (reset, rebase) just move these pointers around. Once that clicks, the tool becomes much less mysterious.
The stuff everyone knows
Code:
git clone, git add, git commit, git push, git pullBranching properly
Code:
git switch -c feature/my-feature # create and switch to new branch
git switch main # go back to main
git branch -d feature/my-feature # delete branch after mergeStaging selectively
Code:
git add -p # interactively choose hunks to stage (very useful)Fixing mistakes
Code:
git restore --staged file.txt # unstage a file
git restore file.txt # discard local changes to a file
git commit --amend --no-edit # add staged changes to the last commit without changing the message
git reset HEAD~1 # undo the last commit but keep the changes stagedInvestigating history
Code:
git log --oneline --graph --all # visual branch history
git log -p file.txt # full history of changes to a file
git blame file.txt # who changed each line and when
git diff main...feature # what changed on feature branch since it diverged from mainFinding regressions
Code:
git bisect start
git bisect bad HEAD # current commit is broken
git bisect good v1.2.0 # this version was fine
# git tests each midpoint, you mark good/bad until it finds the commitStashing work in progress
Code:
git stash # save dirty working tree
git stash list # see all stashes
git stash pop # restore most recent stashCleaning up
Code:
git clean -fd # delete untracked files and directories (careful - irreversible)A good Git mental model: commits are snapshots, branches are pointers to commits, and most "scary" commands (reset, rebase) just move these pointers around. Once that clicks, the tool becomes much less mysterious.
