~/alonge.dev — never-mix-up-your.md
home.mdessays/never-mix-up-your.md
👁 preview
title:never mix-up your git branches again!
date:2020-05-10
read:2 min
tags:[git, developer-tools, workflow]
series:none
aliases:[never-mix-up-your-git-branches-again]

never mix-up your git branches again!

gitdeveloper-toolsworkflow

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~1
don't miss the next note

open the next note in your inbox.

i'll send each new essay the morning it ships. nothing else.

connected·essays/2020/never-mix-up-your-git-branches-again.md·PREVIEW190 words·1,158 chars·ln 1, col 1·● saved