Git habits worth keeping
Five small habits that make history easier to read, bugs easier to find, and mistakes easier to undo.
Git will let you work almost any way you like. A few habits make the difference between a history you can use and one you scroll past.
Commit one idea at a time
A commit should be one change you could describe in a sentence and revert on its own. Mixing a rename, a bug fix and a formatting pass into one commit means none of them can be undone without the others.
Stage by hunk when a file holds more than one idea:
git add -pWrite the message for the person debugging
The diff already says what changed. The message is the only place to say why.
Retry uploads on connection reset
Mobile clients drop the connection when the app is backgrounded
mid-upload. Retrying up to three times recovers most of those
without the user seeing an error.Let bisect do the searching
When something worked last week and doesn't today, don't read every commit in between. Mark one good commit and one bad one, and Git walks you to the change that broke it.
git bisect start
git bisect bad
git bisect good v2.3.0
git bisect run pnpm testThis only works if each commit builds on its own, which is one more reason to keep commits small.
Know your undo commands
| Situation | Command |
|---|---|
| Unstage a file | git restore --staged <file> |
| Fix the last commit message | git commit --amend |
| Undo a pushed commit | git revert <sha> |
| Find a commit you thought you lost | git reflog |
Almost nothing in Git is really gone. If you committed it,
reflogcan usually find it for at least a few weeks.
Pull with rebase on your own branches
Merging the main branch into a feature branch every morning fills the history with merge commits that say nothing. Rebasing replays your work on top of the latest main and keeps the story linear.
git pull --rebase origin mainOnly rebase commits that are yours alone. Once someone else has built on them, rewriting them rewrites their work too.