Handcrafting Codes← Index

Engineering

Git tricks I keep reaching for

These are the handful I've wired into muscle memory, the ones that earn their place because they save a real minute or a real mistake.

Jul 02, 2026 / 4 min read / By the author

COVER IMAGE — 16:9

title: "Git tricks I keep reaching for" date: 2026-06-26 tags: [git, cli, productivity] draft: true

Most git "tips" lists are padding. These are the handful I've wired into muscle memory, the ones that earn their place because they save a real minute or a real mistake.

Let git fix your typos

Type git stats instead of git status and git tells you what you meant, then makes you type it again. You can skip the second step:

git config --global help.autocorrect 1

The value is in tenths of a second, so 1 means git waits 0.1 seconds and then runs its best guess. Bump it to 20 if you want two seconds to hit Ctrl-C when it guesses wrong. Drop --global to scope it to one repo. After this, git stats just runs git status.

Count commits without scrolling

git rev-list --count master

Useful when you tie a build number to commit count, or when you just want a number to compare two branches. Point it at any valid ref.

Read a file from another branch without switching

You're on dev, you want to see what README.md looks like on main, and you don't want to stash and checkout to do it:

git show main:README.md

The <ref>:<path> syntax works for any commit, not just branch tips. git show HEAD~3:src/app.js shows you that file three commits back. I use this constantly to check what changed before I go diffing.

Trigger a build with an empty commit

You need to kick a CI pipeline but you have nothing to change. The bad habit is touching the README. The right move:

git commit --allow-empty -m "trigger build"

No phantom whitespace diff, no "fix typo" commit that fixed nothing.

Recover a commit you thought you destroyed

You rebased, something went sideways, and a commit is gone. It usually isn't. As long as you committed the work, it's still in the object database, and the reflog knows where it was:

git reflog
git checkout <sha>   # grab what you need
git checkout -        # jump back to where you were

The reflog is the single most reassuring command in git. It records where HEAD has been even after a rebase rewrites history, so "I lost a commit" is almost always "I misplaced a commit." This is the trick that turns a panic into an inconvenience.

Clean up branches that no longer exist on the remote

When a branch is merged and deleted on the server, your local copy keeps a stale tracking reference. Make fetch prune them automatically:

git config --global fetch.prune true

And to clear out local branches you've already merged into master:

git branch --merged master | grep -v "master" | xargs -n 1 git branch -d

The lowercase -d is deliberate. It refuses to delete anything that isn't merged, which is the safety net that keeps this one-liner from eating work in progress.

Back up untracked files before you blow them away

Sometimes you want a pristine working tree but you're not quite ready to lose what's untracked. Archive it first:

git ls-files --others --exclude-standard -z | xargs -0 tar rvf ~/backup-untracked.zip

This respects .gitignore and packs only the untracked files into a zip you can dig through later.

Compact your repo

After a lot of heavy push and pull traffic, the object store accumulates cruft:

git gc --prune=now --aggressive

This collects unreachable objects and repacks. You won't run it often, and you don't need to, since git runs a lighter gc on its own. Reach for the aggressive form when a repo feels bloated, not as a habit.

Know what lives in .git

Every repo has a .git folder, and it's mostly off-limits, but it's worth knowing what's in there so it stops being a black box. cat .git/HEAD tells you what HEAD points at. The hooks/ directory ships with sample hooks you can read to learn what's possible. Delete the folder and your source files survive, but the history, the remotes, and the version control itself are gone. It's the repo. The files are just the current checkout.

None of these are clever for the sake of it. They're the small frictions worth removing, because you hit them every single day.