Git Tricks Worth Actually Remembering
The handful of Git commands beyond add/commit/push that save real time — undoing safely, finding the commit that broke things, and rescuing work you thought was gone.
Everyone knows add, commit, push. These are the next ten that pay for themselves.
Undo the last commit but keep the changes
Committed too early? Move the commit off but leave your edits staged:
git reset --soft HEAD~1Use --mixed (the default) to also unstage, or --hard to throw the changes away entirely — the only one of the three that loses work.
Fix the last commit message (or add a forgotten file)
git commit --amend # edit the message
git add forgotten.txt && git commit --amend --no-edit # sneak a file in, keep the messageOnly amend commits you haven't pushed to a shared branch.
Stash with a name, and get it back
git stash push -m "wip: half-done refactor"
git stash list
git stash pop # apply newest and drop it
git stash apply stash@{1} # apply a specific one, keep it in the listFind the commit that broke something — bisect
Binary-search your history. Mark a known-good and known-bad commit and Git walks you to the culprit:
git bisect start
git bisect bad # current commit is broken
git bisect good v1.4.0 # this old tag worked
# Git checks out the midpoint; test it, then:
git bisect good # or: git bisect bad
# repeat until Git names the first bad commit
git bisect resetTen steps bisects a thousand commits.
See who last touched a line — blame
git blame -L 40,60 app.c # authorship for lines 40–60Pair it with git log -p -S "someString" to find when a string was introduced or removed ("pickaxe" search).
The safety net: reflog
git reflog records where HEAD has been, even across resets and rebases. It is how you recover "lost" commits:
git reflog
# find the hash you want back, then:
git reset --hard <hash> # or: git checkout -b rescue <hash>Clean up a messy branch before a PR
git rebase -i HEAD~5 # squash, reword, reorder the last 5 commitsInteractive rebase lets you present a clean story even if your actual work was chaotic. (Rebase only your own un-shared commits.)
Quick quality-of-life
git switch -c feature/x # create+switch (clearer than checkout -b)
git restore file.txt # discard unstaged changes to a file
git log --oneline --graph --all # readable history at a glance
git diff --staged # review exactly what you're about to commitTakeaways
reset --softandcommit --amendfix fresh mistakes without losing work.bisectfinds regressions fast;blame+ pickaxe find when/why a line changed.reflogis the undo-for-your-undo — learn it before you need it.