Great Stack to Doesn't Work — Bonus
10 Advanced Git Commands You'll Actually Use
Beyond add, commit, push.
1. git bisect — Find the exact commit that broke something.
Binary search through your commit history. Tell git which commit was good, which is bad, and it narrows down the culprit.
git bisect start
git bisect bad # Current commit is broken
git bisect good abc123 # This old commit worked
# Git checks out the middle commit. Test it.
git bisect good # or: git bisect bad
# Repeat until git identifies the exact commit.
git bisect reset # Go back to normal
Automated version with a test script:
git bisect start HEAD abc123
git bisect run npm test
Git runs your test at each step. Fully automated. The commit that introduced the bug is identified in minutes across thousands of commits.
2. git reflog — Undo anything. Literally anything.
Accidentally reset --hard? Deleted a branch? Force-pushed and lost commits? Reflog has the receipts.
git reflog
# Shows every HEAD movement: commits, resets, checkouts, rebases
# Find the state you want to go back to
git reset --hard HEAD@{5}
Reflog entries expire after 90 days by default. Until then, nothing is truly lost.
3. git worktree — Work on two branches simultaneously.
No more stashing and switching. Create a second working directory from the same repo.
git worktree add ../hotfix-branch hotfix/urgent-fix
# Now you have two directories, two branches, one repo
cd ../hotfix-branch
# Work on the hotfix without touching your feature branch
git worktree remove ../hotfix-branch # Clean up when done
Essential for reviewing PRs while you have uncommitted work on your feature branch.
4. git stash --keep-index — Stash only unstaged changes.
You've staged some files and have other modifications you're not ready to commit. git stash grabs everything. git stash --keep-index keeps your staged changes and stashes only the rest.
git add file1.js file2.js # Stage what you want
git stash --keep-index # Stash the rest
# Test with only your staged changes
git stash pop # Bring back the stashed stuff
5. git cherry-pick — Grab a single commit from another branch.
A bug fix landed on develop but you need it on release right now. Don't merge the whole branch.
git checkout release
git cherry-pick abc123
One commit. Surgically applied. Conflicts are possible if the branches have diverged significantly, but for isolated fixes it's clean.
6. git rebase -i — Rewrite history before pushing.
Your branch has 15 commits: "wip", "fix typo", "actually fix it", "oops forgot a file." Interactive rebase lets you squash, reorder, edit, or drop commits before anyone sees them.
git rebase -i HEAD~5 # Edit the last 5 commits
# Mark commits as 'squash', 'fixup', 'reword', 'drop'
Golden rule: never rebase commits that have been pushed to a shared branch. Rewrite your local history all you want. Leave shared history alone.
7. git log --oneline --graph --all — See the actual branch structure.
git log --oneline --graph --all --decorate
This shows merge commits, branch points, and the full topology. When someone says "this branch is behind main," this command shows you exactly how.
Alias it:
git config --global alias.lg "log --oneline --graph --all --decorate"
git lg
8. git diff --stat — See what changed without the noise.
git diff --stat main..feature-branch
Shows file names and change counts. Quick way to understand the scope of a PR before diving into the full diff. If a "small change" PR shows 40 files modified, you know to ask questions.
9. git blame -w -M -C — Find who actually wrote this code.
git blame shows the last person to touch each line. But whitespace changes, moved code, and copied code create false blame.
git blame -w -M -C file.js
# -w: ignore whitespace changes
# -M: detect moved lines within the file
# -C: detect copied lines from other files
Now you see who actually wrote the logic, not who reformatted it.
10. git clean -fd — Nuke untracked files.
Build artifacts, generated files, test output cluttering your working directory.
git clean -fd # Delete untracked files and directories
git clean -fdx # Also delete files in .gitignore (node_modules, etc.)
git clean -fdn # Dry run: show what would be deleted without deleting
Always run with -n first. There's no undo for git clean.
Over to You
Which advanced git command saved your life? Any git horror stories where reflog was the only way back?
If you enjoyed this, I write about production engineering, AI systems, and the messy reality of building software at scale.
Follow me:
This is part of the **Great Stack to Doesn't Work* series — a survival guide for when everything goes wrong in production. Follow the series to catch every episode.*
Top comments (0)