About the Advanced Git cheat sheet
Most advanced Git usage is recovery rather than cleverness. The single most valuable thing to know is that the reflog records where every branch pointer has been, so almost anything that appears destroyed locally is still reachable for weeks.
That changes how the riskier commands feel. A rebase that goes wrong, a hard reset onto the wrong commit, a branch deleted too early — all are recoverable by finding the previous position in the reflog and resetting back to it. Commits are only truly gone once garbage collection runs, and unreferenced objects are kept for a generous default period first.
The one genuinely dangerous operation is force-pushing to a shared branch, because it rewrites history other people have already based work on. Force-with-lease is the safer form: it refuses to push if the remote has moved since you last fetched, which prevents overwriting a colleague's commits.
Frequently asked questions
How do I recover a commit I lost with a hard reset?
Use the reflog to find where the branch pointed before the reset, then reset back onto that entry, or create a branch at it. The reflog records every movement of HEAD and of each branch, including ones made by rebase, merge, and checkout. This works for anything that was ever committed, which is why committing before an experiment is worth the ten seconds.
When should I rebase instead of merge?
Rebase to tidy your own unpushed work into a reviewable sequence. Merge when integrating shared branches, because it preserves what actually happened and does not rewrite commits others may hold. The rule that avoids most pain is to never rebase a branch that someone else has pulled.
What is the difference between revert and reset?
revert creates a new commit undoing an earlier one, leaving history intact — it is the correct choice for anything already pushed. reset moves the branch pointer, rewriting history, and is appropriate only for local work. Using reset on a shared branch and force-pushing is how other people's work disappears.
How do I find which commit introduced a bug?
git bisect. Mark a known-good commit and a known-bad one and it performs a binary search, asking you to test each midpoint. With a script that exits non-zero on failure it runs unattended. Over a thousand commits that is around ten tests rather than a thousand.
How do I remove a secret from history?
Rewrite history with git filter-repo, which is the maintained replacement for filter-branch, then force-push and have everyone re-clone. Then rotate the secret regardless — it has been on other machines, in CI caches, and possibly in forks. Treat the rewrite as damage limitation, not as remediation.
Need this managed for you, not just automated?
We're also a hands-on DevOps consultancy — Kubernetes, CI/CD, and cloud infrastructure.