Let’s be honest: git delete branch isn’t the most exciting command you’ll ever run, but it’s definitely one of the most useful. Git branches start organized and full of promise, like a neatly folded sock drawer. Fast-forward a few weeks, and you type git branch -a
only to find chaos: dozens of random branches, half merged, half forgotten, and a few you don’t even remember creating.
That’s when you realize — your repository is basically hoarding.
That’s where git delete branch comes in. Clearing out old local and remote branches doesn’t make you a superhero, but it does make your workflow cleaner and your future self grateful. It’s a small, unglamorous habit that makes a big difference in keeping your projects sane.
In this post, I’ll walk you through how to delete local and remote branches (without hitting the dreaded “cannot delete branch ‘main’ checked out” error). I’ll also share a few mistakes I’ve made along the way — plus some opinionated takes on Git hygiene.
Ready to tidy up that branch drawer? Let’s dive in.

Switch Before You Delete
Here’s the thing: every branch in your repo is like an open tab in Chrome. One or two? Fine. Forty-seven? Suddenly, your brain feels like spaghetti.
I used to ignore branches, thinking, “Eh, they’re harmless.” But then I’d onboard new teammates and watch their faces as they scrolled through a repo littered with bugfix-jan2021
, temp-test
, and i-think-this-works
. Not a good first impression.
By running a git delete branch every so often, you:
- Reduce confusion (yours and your teammates’).
- Prevent accidental checkouts into abandoned work.
- Keep your workflow smooth and your repo clean.
Think of it like cleaning your desk. No, you don’t need to — but try writing code surrounded by empty coffee cups and random sticky notes. Exactly.
Deleting a Local Git Branch
Alright, let’s start small. You’ve got a local branch that’s overstayed its welcome. Maybe it’s already merged. Perhaps it was just an experiment that failed faster than you expected. Either way — it’s time to let it go.
First things first, open your terminal and ensure you’re not on the branch you want to delete. Git will straight-up refuse if you try. (And yes, I’ve embarrassed myself by forgetting this too many times.)
Here’s the basic flow:
# Step 1: Check where you are
git status
# Step 2: Switch to another branch (usually main or dev)
git switch main
# or
git checkout main
# Step 3: Delete the old branch
git branch --delete old-feature
Want to double-check? Run this:
git branch -a
If the branch is gone, congrats — one less ghost in your repo.
That Time I Tried to Delete main
…
True story: I once absentmindedly tried to delete the main
branch. Git smacked me with:
error: Cannot delete branch 'main' checked out
Honestly, thank goodness for that error. Imagine nuking your entire working base because you forgot to switch first. Moral of the story: switch before you delete.
And if you’re stubborn and really want to force it (not recommended unless you’re 100% sure), there’s the -D
option. But hey — if you’re using git delete branch
it with -D
, double-check twice, type once.
Two Ways to Run git delete branch
Locally
Here’s a neat trick: you’ve got options.
git branch --delete old-branch
or the shorter version:
git branch -d old-branch
They do the same thing. I personally lean toward the long version (--delete
) because it makes future-me thankful when scanning bash history at 2 AM. Shortcuts are nice until you forget what they meant six months later.
Local vs Remote Deletions
Here’s a common trap: you delete a local branch, pat yourself on the back, and think you’ve cleaned house. Then you open GitHub or GitLab, and surprise! The branch is still there, haunting your repo like that one ex who won’t stop lurking on social media.
Deleting a branch locally does not delete it remotely. They’re two separate worlds.
So if you really want to wipe a branch off the face of the Earth, you need to push a deletion to the remote too.
How to Delete a Remote Git Branch (Carefully!)
Okay, here’s where you need to slow down a bit. Deleting a remote branch is like taking out the trash at work — once it’s gone, everyone loses access to it. No “oops” button.
Here’s the syntax:
git push origin --delete old-branch
This tells Git: “Hey, remove this branch from the remote server too.”
Want to check if it worked? Just run:
git fetch --all --prune
git branch -a
The --prune
flag cleans up any references to remote branches that no longer exist. It’s like sweeping after you throw out the garbage.
Pro tip: Don’t delete remote branches in the middle of the day unless you want angry teammates pinging you on Slack. Always check if it’s safe, merged, and truly unneeded.
My Two Cents on Git Branch Hygiene
I’ll admit it: I get a little obsessive about Git cleanliness. To me, running a git delete branch every week is like taking out the trash or cleaning the kitchen counter.
Here are my personal “rules of thumb”:
- If a branch is merged and old, delete it. Don’t hoard.
- If a branch was an experiment and didn’t work, delete it. Fail fast, move on.
- If you need to keep it for reference, at least rename it something obvious (like
archive/feature-x
).
And here’s a hot take: branches are cheap. Don’t get sentimental. Delete ruthlessly, create freely. Your repo should serve your workflow, not become a digital landfill.
The “Oh Yeah, I Needed That” Git Tips
If you’re diving deeper into Git cleanup, here are some helpful guides I recommend:
- How to remove untracked files in Git (because branches aren’t the only clutter).
- A step-by-step guide on renaming Git branches without breaking everything.
- Why using Git Worktree can save you from juggling too many branches locally.
And if you’re brand new to Git branching strategies, the folks at Atlassian have a fantastic beginner-friendly breakdown.
Wrapping It Up: Keep It Clean, Keep It Simple
At the end of the day, learning how to run a git delete branch — locally and remotely — isn’t just about memorizing commands. It’s about building the habit of keeping your development environment clean and sane.
Yes, Git will happily let you collect dozens of branches. But should you? Nah. That’s how chaos sneaks in.
So next time you finish a feature, run through your checklist: merged? tested? shipped? Cool — then delete that branch.
And hey, if you ever feel bad about deleting code, remember this: Git never really forgets. You can always dig into the history if you need it. But for your sanity? Clean it up.
Happy coding, and happy cleaning.
Leave a Reply