never mix-up your git branches again!
ever finished working on a feature only to commit your code and discover you did so in the wrong branch? here's how to avoid that.
Never Mix-up Your Git Branches Again!
Ever finished working on that shiny new feature or making that little code update only to commit your code and discover you did so in the wrong branch? We've all been there.
Here are some practical tips and workflows to ensure you never make this mistake again:
1. Always Check Your Branch First
git branch --show-current
Make this a habit before starting any work.
2. Customize Your Terminal Prompt
Add the current Git branch to your terminal prompt so it's always visible. Most modern shells (Oh My Zsh, Starship) support this out of the box.
3. Use Git Worktrees
Git worktrees let you have multiple branches checked out simultaneously in different directories:
git worktree add ../feature-branch feature-branch
4. Set Up Pre-commit Hooks
Use pre-commit hooks to prevent commits to protected branches like main or production.
5. If You Already Committed to the Wrong Branch
# save the commit hash
git log --oneline -1
# switch to the correct branch
git checkout correct-branch
# cherry-pick the commit
git cherry-pick <commit-hash>
# go back and reset the wrong branch
git checkout wrong-branch
git reset --hard HEAD~1backlinks 0
see also
open the next note in your inbox.
i'll send each new essay the morning it ships. nothing else.