← all posts

5 Git mistakes I made in my first year

2024-11-12 · ~6 min read

Looking back at my first year using Git seriously, I made every mistake it's possible to make. Here are the five I remember most clearly, mostly because each one cost me at least an hour of recovery.

1. Force-pushing to a shared branch

I rebased main and ran git push --force because the regular push was rejected. Wiped out two coworkers' commits. The lesson: --force-with-lease is the safer version — it refuses to push if the remote has commits you don't have locally. Better yet, never force-push to a branch anyone else uses.

2. Committing on a detached HEAD

I checked out a tag to look at something, made changes, committed them, then ran git checkout main. Git warned me. I ignored the warning. Commits gone. Recovering them via git reflog took longer than just redoing the work.

git reflog
# 7a3b2c1 HEAD@{0}: checkout: moving from HEAD to main
# 5f8e9d2 HEAD@{1}: commit: my lost work
git checkout 5f8e9d2
git checkout -b recovered-work

3. Adding to .gitignore after committing

.gitignore only blocks files Git hasn't seen yet. If you've already committed secrets.json, adding it to .gitignore does nothing — the file is still tracked. You need:

git rm --cached secrets.json
echo "secrets.json" >> .gitignore
git commit -m "stop tracking secrets.json"

And remember: it's still in the history. If it was a real secret, rotate it.

4. Force-pushing to the wrong branch

I meant to clean up my feature branch. I cleaned up main. Recovery involved checking the GitHub events API for the previous SHA, then hard-resetting. I now keep feature branches in a personal prefix (alex/feature-x) so the autocompletion can't bite me.

5. Trusting git stash for too long

git stash is fine for a few minutes. It's not durable storage. I once had three weeks of small experiments stacked in stashes that I assumed were safe. A bad merge wiped them. Technically recoverable through reflog, but it took forever. Now I just commit WIP work to a throwaway branch:

git checkout -b wip/$(date +%Y%m%d)
git add -A
git commit -m "WIP"

The pattern

Every Git command that mutates history — rebase, push --force, reset --hard, stash drop — deserves a moment of "wait, am I on the right branch?" Take a breath. Run git status. Then proceed.