Guides · Git
Git habits for solo developers
When you work alone, nobody reviews your commits, so Git habits slip: one giant commit called “fix”, everything on main, a .env pushed by accident. None of it hurts until the day you need to undo something. These are the habits that make that day easy.
Why bother when it's just you
Your Git history is a note to your future self. Six months from now you'll want to know when a bug crept in, why a line changed, or what the app looked like before a redesign. A good history answers in seconds. A history of wip, fix and update answers nothing.
You also get a safety net. With small commits and a branch per change, any experiment can be thrown away without losing anything else.
Set it up once
A few global settings remove most of the daily friction:
git config --global init.defaultBranch main
git config --global pull.rebase true
git config --global push.autoSetupRemote true
git config --global rerere.enabled true
git config --global fetch.prune true| Setting | What it does |
|---|---|
init.defaultBranch | New repos start on main |
pull.rebase | git pull replays your commits on top instead of adding a merge commit |
push.autoSetupRemote | The first git push on a new branch just works, no --set-upstream |
rerere.enabled | Git remembers how you resolved a conflict and reuses it next time |
fetch.prune | Branches deleted on GitHub disappear locally too |
Add a global ignore file for things that never belong in any repo, like .DS_Store and editor folders:
printf ".DS_Store\n.idea/\n*.log\n" >> ~/.gitignore_global
git config --global core.excludesFile ~/.gitignore_globalCommit small, commit often
A commit should be one change you could describe in a sentence: "add dark mode toggle", "fix date format on invoices". If the sentence needs "and", it's probably two commits.
Small commits are easy to read, easy to revert, and easy to search. When something breaks, git bisect can find the commit that did it, but only if each commit is small enough to point at the cause.
When you've made several changes in one go, stage them in pieces:
git add -pGit shows each changed chunk and asks whether to stage it. Commit the first change, then run it again for the next.
Write messages you'll understand later
A good message has a short summary line, and a body only when the why isn't obvious from the code:
fix(invoice): show dates in the customer's time zone
Invoices were formatted with the server's zone, so customers in
Bangkok saw yesterday's date on anything created after 5pm UTC.The fix(invoice): prefix follows Conventional Commits. You don't need tooling for it, but the habit pays off: feat, fix, refactor, docs, chore and perf tell you at a glance what kind of change it was, and they make a changelog easy to write when you need one.
A few rules of thumb:
- Write the summary as an instruction: "add", "fix", "remove", not "added" or "fixes".
- Keep it under about 50 characters, so it isn't cut off in
git log --onelineor on GitHub. - Say what changed for the user or the code, not what you did: "fix login redirect loop" beats "changed some stuff in auth".
Branch, even alone
Working straight on main means half-finished work is always mixed in with working code. Make a branch for every change, however small:
git switch -c feat/dark-modeName branches by type and topic: feat/…, fix/…, chore/…. When the work is done, merge it and delete the branch:
git switch main
git merge --ff-only feat/dark-mode
git branch -d feat/dark-mode--ff-only refuses to merge if main has moved on since you branched. If it does, rebase the branch first (git rebase main while on it) so the history stays a straight line.
Branches also make it painless to drop an idea. If the experiment fails, delete the branch and main never knew it happened.
Open a pull request anyway
On GitHub, opening a pull request from your own branch costs a minute and gives you a page that shows the whole change at once. Reading your own diff there catches leftover console.logs, debug flags and files you didn't mean to commit. It's also where your CI checks run before anything reaches main.
Clean up before you push
Commits on a branch you haven't pushed yet are yours to rewrite. Use that.
Forgot a file in the last commit:
git add src/forgotten-file.ts
git commit --amend --no-editFound a mistake in an earlier commit on the branch: commit the fix as a "fixup" of that commit, then fold it in:
git commit --fixup <commit-sha>
git rebase -i --autosquash mainThe rebase opens an editor with the fixup already moved into place; save and close it. The result reads as if you got it right the first time.
If the branch was already pushed, push the rewritten version with:
git push --force-with-leaseUnlike --force, it refuses to overwrite commits on the remote that you haven't seen, so you can't wipe out work pushed from another machine. Never rewrite main once it's pushed.
Park work without committing it
Need to switch tasks halfway through something? Stash it:
git stash push -m "half-done search filter"
git switch fix/urgent-bug
# …fix, commit, come back…
git switch feat/search
git stash popGive stashes a message. git stash list full of WIP on main is no help a week later.
For anything longer than a quick switch, a worktree is better. It checks out another branch in a second folder, so both can be open at once, with separate dev servers:
git worktree add ../myapp-hotfix -b fix/login mainWhen you're done, remove it with git worktree remove ../myapp-hotfix.
Keep secrets out
The .env file is the classic mistake. Add it to .gitignore before your first commit, and commit an .env.example with the variable names and fake values instead.
If you've already committed a file that should be ignored, stop tracking it without deleting it from disk:
git rm --cached .env
git commit -m "chore: stop tracking .env"If a real key or password was ever pushed, treat it as leaked. Rotate it with the provider first. Removing it from the history afterwards (with a tool like
git filter-repo) is still worth doing, but anyone who cloned or forked the repo in the meantime already has it.
GitHub's secret scanning and push protection will catch many common key formats before they land. Turn them on in the repo's settings.
Mark releases with tags
When a version goes live, tag it:
git tag -a v1.4.0 -m "Search filters and dark mode"
git push --follow-tagsTags give you a name for "what was live on Tuesday". You can check out a tag to reproduce a bug from an old release, compare two releases with git log v1.3.0..v1.4.0 --oneline, or turn a tag into a GitHub release with notes.
The safety net: reflog
Almost nothing in Git is truly lost. Every time HEAD moves (a commit, a reset, a rebase, a checkout), Git records it in the reflog:
git refloga1b2c3d HEAD@{0}: reset: moving to HEAD~3
e4f5a6b HEAD@{1}: commit: feat(search): add filtersReset too far, or a rebase went wrong? Find the line from before the mistake and go back to it:
git reset --hard HEAD@{1}reset --hard throws away uncommitted changes, so commit or stash anything you want to keep first. The reflog only covers things that were committed at some point; it's another reason to commit often.
Find bugs with bisect
When something worked last month and doesn't now, let Git do a binary search for the commit that broke it:
git bisect start
git bisect bad # the current commit is broken
git bisect good v1.3.0 # this release was fineGit checks out a commit halfway between. Test it, then tell Git git bisect good or git bisect bad, and repeat. Across 500 commits it takes about nine steps. Run git bisect reset when it names the culprit. If you have a script that exits non-zero on failure, git bisect run npm test does the whole search for you.
Aliases worth having
git config --global alias.st "status -sb"
git config --global alias.lg "log --oneline --graph --decorate -20"
git config --global alias.undo "reset --soft HEAD~1"
git config --global alias.last "log -1 --stat"git undo takes back the last commit but keeps its changes staged, which is handy when you committed too early or on the wrong branch.
Push every day
Your laptop is not a backup. Push your branches at the end of each day, finished or not. A pushed branch is safe, and it's available on any machine you sit down at.
Cheat sheet
| Situation | Command |
|---|---|
| Start a change | git switch -c feat/name |
| Stage part of a file | git add -p |
| Add a forgotten file to the last commit | git commit --amend --no-edit |
| Fix an earlier commit on the branch | git commit --fixup <sha>, then git rebase -i --autosquash main |
| Undo the last commit, keep the changes | git reset --soft HEAD~1 |
| Unstage a file | git restore --staged <file> |
| Throw away changes to a file | git restore <file> |
| Switch tasks mid-change | git stash push -m "note" |
| Push a rewritten branch safely | git push --force-with-lease |
| Stop tracking a file | git rm --cached <file> |
| Find a lost commit | git reflog |
| Find the commit that broke something | git bisect start |
| Mark a release | git tag -a v1.0.0 -m "…" |
None of these habits take more than a few seconds each. Together they turn your history from a pile of saves into something you can read, search and rely on, which is exactly what you want the day something goes wrong and the only person who can help is you from six months ago.