Bratish Goswami

Personal website and blog

Underused Git Commands

After almost 15 years of building and shipping software, I still get through most days with add/commit/pull/push/merge. Then there's an urgent hotfix, a long-running branch to rebase, or a regression to track down. A handful of less-used commands have saved me hours in those situations and, occasionally, a release. These are the notes I wish I'd had earlier.

git worktree: parallel work without extra clones

Context switching used to mean stashing work and hoping it would apply cleanly later, or cloning a big repo again. With git worktree, I can keep several working directories on different branches, all sharing one repository's object database.

Spin up a sibling workspace for a hotfix

git worktree add -b hotfix/latency ../hotfix-latency origin/main

See what’s active

git worktree list

Clean up when done

git worktree remove ../hotfix-latency

I find it easier to handle an urgent fix when I can leave my unfinished work where it is, without juggling stashes or duplicate clones. That's why git worktree is one of the first commands I'd suggest trying.

git rerere: resolve recurring conflicts once

Long-lived branches and regular rebases can bring back the same conflict repeatedly. I enabled rerere years ago to help with this. It records how a conflict was resolved and reuses that resolution when an identical conflict appears.

Turn it on (once)

git config --global rerere.enabled true

Inspect what’s being remembered

git rerere status

It won't resolve every conflict, since small context changes can get in the way. I've found it reliable for repetitive “rename plus nudge” merges and package.json/version bumps. A rebase that used to take 10 minutes can then take 30 seconds.

git notes: add context without rewriting history

I like tidy commit messages, but I don't always have the full story when I commit. Git notes lets me add review summaries, build IDs, or links to incident reports later, without changing the original commit or amending public history.

Add a quick note to the latest commit

git notes add -m "Load-test: 12k rps; p95=180ms; see report #427"

Make notes visible in logs

git log --show-notes

Share notes explicitly (they’re not pushed by default)

git push origin refs/notes/commits

Teammates need to fetch the notes, and some hosting UIs don't display them. For teams comfortable with the CLI, they're a useful place for context that doesn't belong in the commit message.

git range-diff: reviewing a rebased series

After force-pushing a rebased feature branch, I often need to explain what changed between v1 and v2. A file diff doesn't tell the whole story. range-diff compares two commit ranges and pairs up corresponding commits, showing how each one changed.

Compare a series before and after rebase

git range-diff origin/main..feature-v1 origin/main..feature-v2

This helps when reviewing successive versions of a patch set. Reviewers can see which commits moved, which were dropped, and what changed inside each one without rereading the entire feature. It's been available in Git for a while.

git bisect: binary search for regressions

I used to hunt regressions by intuition and grep. With git bisect, I give it a known good revision and a known bad one, then work through a binary search of the history to find the offending commit.

Minimal flow

git bisect start
git bisect bad                # current commit is broken
git bisect good v2.3.1        # a tag or SHA that definitely worked
# build & test...
git bisect good               # or: git bisect bad
# repeat until it identifies one commit
git bisect reset

If the test is scriptable

git bisect start
git bisect bad
git bisect good v2.3.1
git bisect run ./repro.sh

This saves a lot of time on large histories, provided I have a reliable “good/bad” test to run at each step.

git reflog: the real safety net

Reflog has rescued me from more “I think I’ve lost it” moments than anything else. It tracks where HEAD and other refs have been, even when those commits are no longer reachable by normal logs. Deleted the wrong branch? Botched a rebase? Hard reset a little too hard? Reflog is usually the way back.

See recent moves

git reflog

Undo a bad rebase (jump back to before it started)

git reset --hard HEAD@{1}

Recreate a deleted branch (from a recent position)

git branch restore-me HEAD@{3}

Even stash has a reflog

git reflog show stash
git stash apply stash@{1}

Reflog is local and time-limited by default. If months have passed and garbage collection has run, unreachable objects may be gone. For recent mistakes, it's still the first place I look.

How I use these together

On busy weeks, I use git worktree for urgent fixes and keep rerere on to avoid repeating conflict resolutions. I add git notes after CI finishes and use range-diff when rebasing a patch series under review. For a regression, I turn to bisect; for a mistake in my own Git work, I check reflog.

A short checklist

  • Enable rerere globally:
    git config --global rerere.enabled true
  • Learn the 3-4 worktree commands you'll actually use.
  • Keep a simple “is this commit good?” script around for bisect.
  • Make “log with notes” part of your habit on teams that embrace notes.
  • Reach for range-diff when explaining a force‑pushed rebase.
  • When in doubt, check reflog before assuming work is lost.

These commands have helped me recover from mistakes and switch tasks with less disruption. I don't use every one daily, but I'm glad to have them when the usual commands aren't enough.