Working on new features in Git branches is great – until you realize you have branches piling up from old projects. Let‘s look at how to thoughtfully prune local branches to keep your repositories clean.
Why Bother Deleting Local Branches?
Before jumping into the commands, it helps to understand what branches represent in Git:
- Branches let you isolate work for new features, bug fixes, experiments, and more.
- They essentially act as alternate timelines of commits that can be merged together.
- The main branch (usually called master or main) is the default that ships to production.
With this power comes some responsibility. On large projects with several contributors, branches can quickly multiply and cause clutter.
Here are some solid reasons for routinely deleting local branches:
- Avoid confusing which branches contain active work vs finished/merged work.
- Visualize a linear commit history more clearly after branches are merged in.
- Remove branches that represent abandoned features or experiments.
- Reduce storage space taken up by inactive branches on your machine.
- Practice good Git hygiene and make your repositories tidy.
In my experience, most projects end up accumulating branches over time without diligent cleanup. Let‘s look at how to prune these efficiently.
When Should Branches Be Deleted?
First, when shouldn‘t you delete a branch?
- If it contains in-progress work that needs to be saved.
- If the changes in the branch haven‘t yet been merged or backed up.
Some good times to delete a branch include:
- After it‘s merged – No need to keep branches that have been successfully integrated into the main codebase.
- When a feature is cancelled – Ditch branches for prototypes or spike solutions that didn‘t work out.
- During project milestones – Merge related features and delete old branches at major points like releases.
- When switching contexts – Removing old branches frees your mind to focus on new tasks.
- General maintenance – Periodic cleanup keeps your repository organized.
Following these deletion opportunities will keep your branches lean. Now let‘s go over how to actually delete local branches.
Prerequisites for Branch Deletion
Before removing branches, good to have:
- Git installed – Git version control needs to be set up on your system.
- A repository with branches – You need an existing repo with multiple branches to practice deleting.
- Unique commits merged or saved – Don‘t lose needed changes by deleting hastily!
- Remote tracking – If branches were pushed remotely, you‘ll need to clean those up too.
Okay, equipped with Git and a test repo, let‘s dive into deleting.
Deleting Local Branches from the Command Line
The Git command line will be the fastest way to prune branches. The core command is git branch with additional flags:
View Existing Branches
First view your current branches:
git branch
This will list local branches and highlight the currently checked out branch with an * next to it.
Checkout the Branch to Keep
Before deleting, make sure you‘re on a branch you want to keep:
git checkout main
You can‘t delete the branch that‘s currently checked out.
Delete a Merged Local Branch
To delete a branch that‘s been merged in, use -d:
git branch -d branch-name
For example:
git branch -d my-merged-branch
This will safely delete merged branches since that work exists in another branch.
Force Delete an Unmerged Local Branch
To remove incomplete work that‘s unmerged, use -D instead to force delete:
git branch -D unmerged-branch-name
For example:
git branch -D prototype-feature
Only do this if you‘re sure the branch work won‘t be needed!
Confirm Branch Deletion
Now list branches again to confirm deletion:
git branch
The deleted branch should disappear from the list.
Using git branch -d and -D give you control over safely deleting merged branches versus forcefully removing unfinished work.
Pruning Branches Visually with GitHub Desktop
For a more visual branch workflow, you can use GitHub Desktop. This GUI tool lets you manage Git repos and branches with a few clicks.
Here‘s how to delete branches on GitHub Desktop:
-
In the top menu, click
Branch>Show Branchesto see a list. - Right click on the branch you want to delete.
-
Choose
Delete...and confirm when prompted.

The branch will be instantly removed from your local repository.
Caveats of Using GitHub Desktop
While the GUI is handy, be aware that:
- It won‘t prevent you from deleting unsaved work like on the command line.
- You still need to push branch deletions to your remote repository.
- There‘s less control over merge status compared to the command line.
So make sure your unique commits are safely merged or backed up first!
Best Practices for Managing Branches
Beyond just deleting branches, here are some tips for structuring branches to stay organized:
- Use semantic naming schemes like
feature/new-widgetorbugfix/issue-123. - Merge frequently to avoid diverging too far from main.
- Limit active branches to stay focused and avoid merge conflicts.
- Review branches regularly to prune inactive ones.
- Delete local and remote branches in tandem.
Average Number of Active Branches
To give a sense of normal branch activity:
- Small projects often have 5-15 active branches.
- Medium projects around 10-30 active branches.
- Large projects easily 50+ active branches.
Aim to keep branches on the lower side of these ranges for the best overview.
Common Branch Deletion Mistakes
Here are some potential pitfalls to avoid:
- Forgetting to merge or backup unique commits before deleting.
- Assuming GitHub Desktop will handle remote branch deletion.
- Deleting the main or production branch accidentally!
- Mixing up local and remote branch deletion. More on that next.
Being cautious about these mistakes will help avoid losing work or impacting production branches.
Local vs. Remote Branch Deletion
One important distinction is that deleting a local branch won‘t remove the remote branch on GitHub/GitLab.
You need to push the branch deletion for it to be reflected remotely:
git push origin --delete deleted-branch
Conversely, deleting a remote branch won‘t affect your local cloned version – but it‘s good to clean both up.
Recovering a Deleted Branch
Made a mistake and need that deleted branch back? As long as the commits haven‘t been pruned, you can recover a branch:
# Recover merged branch
git branch deleted-branch-name SHA-of-commit
# Recover unmerged branch
git cherry-pick SHA-of-commit
Act quickly though, as those commits can eventually be garbage collected.
Keeping Your Branches Tidy
Branching is a powerful Git feature, but managing branches well is key. Let‘s recap some best practices:
- Merge diligently – Keep features integrated with main via frequent merging.
- Delete judiciously – Remove local and remote branches after merging or abandoning work.
- Name descriptively – Use semantic naming conventions for better organization.
- Review regularly – Take time to prune stale branches and keep active ones relevant.
- Back up unmerged – Don‘t delete unpublished commits without saving them elsewhere.
- Use GUI wisely – Leverage visual tools like GitHub Desktop alongside command line workflows.
By following these branch hygiene habits, you‘ll keep your repositories lean and your commits logically organized.
Have any other tips for effective branch management? Reach out with your top practices!


