DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,673 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for Git basics: remove all local branches
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

Git basics: remove all local branches

There will come a time when you optimize your workflow to ensure PR requests are as small as possible.
Each request can be minimal, so you end up with tons of new local branches that have probably already been merged.

Below is an example of my local daily.dev repo with all the branches I have locally.

List of many local branches

Time to make some changes and clean up our mess.

Removing local git branches

We could go to the editor and click remove on the local branches.
However, we wouldn't be developers if we didn't use the terminal correctly.

To delete one local branch, we can use the following command.

git branch -d BRANCH_NAME
Enter fullscreen mode Exit fullscreen mode

However, this will only work for merged branches. If you still want to proceed, you can use the capital D like this:

git branch -D BRANCH_NAME
Enter fullscreen mode Exit fullscreen mode

Deleting all local branches

However, when we have many local branches, we might want to delete all of them at once.

For that, it's important to note that the delete call can handle multiple files.

First, we have to find all the branches. I used the following command.

git branch | grep -v \*
Enter fullscreen mode Exit fullscreen mode

However, this also includes our master/main branch.

git branch | grep -v "master\|main"
Enter fullscreen mode Exit fullscreen mode

And if you only want to remove merged branches, you can use the following addition.

git branch --merged | grep -v "master\|main"
Enter fullscreen mode Exit fullscreen mode

To execute deletion, we can pass another argument for the delete command.

git branch --merged | grep -v "master\|main" | xargs git branch -D
Enter fullscreen mode Exit fullscreen mode

Conclusion

We can have many local branches that we might want to clean up in one go.

To delete a single branch, use the following command.

git branch -d BRANCH_NAME
# use -D for unmerged branches
Enter fullscreen mode Exit fullscreen mode

If you want to delete all merged local branches except master/main, use the following command.

git branch --merged | grep -v "master\|main" | xargs git branch -D
Enter fullscreen mode Exit fullscreen mode

Note: The above only deletes merged branches

If you want to delete all local branches except master/main, use the following.

git branch | grep -v "master\|main" | xargs git branch -D
Enter fullscreen mode Exit fullscreen mode

Note: The above also deletes unmerged branches

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Top comments (26)

Collapse
 
damian_cyrus profile image
Damian Cyrus

Very handy one-liners.

There should be a git command especially for this cases. Something like:

git branch --all-branches --merged --delete --ignore="fix-this-bug,prefix-*"
Enter fullscreen mode Exit fullscreen mode

And the main branch is ignored by default, to not do something dangerous. Maybe also a hint if you delete a branch when you are checkout into it at the moment ...

Or some similar feature, with a good described documentation in one place πŸ˜„

Collapse
 
jmfayard profile image
Jean-Michel Fayard πŸ‡«πŸ‡·πŸ‡©πŸ‡ͺπŸ‡¬πŸ‡§πŸ‡ͺπŸ‡ΈπŸ‡¨πŸ‡΄

Git is a goldmine for content creators.

So many common tasks don't have an intuitive solution, and the official documentation has a chicken and egg problem where you can search only what you already know how to do.

So lots of Google traffic.

Collapse
 
dailydevtips1 profile image
Chris Bongers Author

There should really be a really good cleanup way actually.
Heck, even simply an automated way, merged branch -> get info back to delete it when it's not active anymore.

But at the very least indeed a option to make it easier and safe for people to do this.

Collapse
 
yawaramin profile image
Yawar Amin

There will come a time when you optimize your workflow to ensure PR requests are as small as possible.
Each request can be minimal, so you end up with tons of new local branches that have probably already been merged.

When this starts happening at scale, this is probably an indication that we need to think at a higher level of abstraction than 'branches' and 'pull requests', or at least we need a tool that simplifies part of this workflow. Lately I am trying out Git Patch Stack for this. It creates a branch and a PR automatically from a single commit. At this level of abstraction it calls the combination of the branch and PR a 'patch'. You can create and layer multiple patches in a stack, the intention is that each patch gets sent as a PR to be reviewed. As patches are reviewed and merged, the patches above those ones are automatically rebased on top of the new branch head.

It takes a little setup though–the tool by itself doesn't know how to create PRs, it relies on shell scripts called 'hooks' to do that. I am trying it out now but haven't yet made up my mind about whether it's the ultimate solution to the problem. I just think a solution above the level of branches is needed.

Collapse
 
dailydevtips1 profile image
Chris Bongers Author

Thanks for that vision Yawar.
At what large scale are you working at, just interested to hear :)

Collapse
 
owieth profile image
Oli

Maybe this command is helpful to someone ;)
The command deletes all local branches without an origin attached

git fetch -p && for branch in $(git branch -vv | grep ': gone]' | awk '{print $1}'); do git branch -D $branch; done

Collapse
 
dailydevtips1 profile image
Chris Bongers Author

Thanks for sharing Oli

Collapse
 
himanshupal0001 profile image
Himanshupal0001

Hi Chris, thanks for the post. Could you create series regarding git and git bash. I am struggling with git commands. Mostly when i've to work on already present repositeries. Could you make series how to use git and it's commands affectvily both for new repos and existed repos. Thanks in advance.

Collapse
 
dailydevtips1 profile image
Chris Bongers Author

Hey,

I actually already did a whole series on Git, this one was kind of the aftermath.
You can find the full series here:
daily-dev-tips.com/tags/git/

Collapse
 
adamisheff profile image
adamISheff • Edited on

Love these. I would suggest a mention of how to create an alias for these commands to make using them more intuitive(a challenge with git other comments point out). That is how I've set up similar commands for my shell.

example:
alias deleteLocalBranches="git branch | grep -v "main" | xargs git branch -D"

Collapse
 
dailydevtips1 profile image
Chris Bongers Author

Yeah, wrote the alias on my todo list to refactor.
If you do have some time before me, feel free to modify it in my repo already.

github.com/rebelchris/daily-dev-ti...

Collapse
 
renanfranca profile image
Renan Franca

Thanks ❀️! That's a useful command! I used to do git prune than going to delete the highlights branches in red by vscode πŸ€“!

Collapse
 
nachokai profile image
Nacho Caiafa

With Oh My Zsh you can use the alias "gbda" :)
(git branch --no-color --merged | command grep -vE "^(+||\s($(git_main_branch)|development|develop|devel|dev)\s*$)" | command xargs -n 1 git branch -d)

Collapse
 
dailydevtips1 profile image
Chris Bongers Author

Oh dang! TIL πŸ˜…

Collapse
 
xapuu profile image
Xapuu

Clone the repo again ;)

Collapse
 
noviicee profile image
Anamika

I used to do this πŸ˜‚ πŸ€“

Collapse
 
dailydevtips1 profile image
Chris Bongers Author

Also good

Collapse
 
dailydevtips1 profile image
Chris Bongers Author

Nice! Love it πŸ™
Actually if you want to can add it to my repo?
Would be great for other people too.
github.com/rebelchris/daily-dev-ti...

Collapse
 
sylwiavargas profile image
Sylwia Vargas

I literally went blank about how to do it earlier today (and then got distracted so never googled it). Thank you!

Collapse
 
dailydevtips1 profile image
Chris Bongers Author

Glad it helped :)

Collapse
 
noviicee profile image
Anamika

Very useful.

Collapse
 
dailydevtips1 profile image
Chris Bongers Author

Thanks Anamika

Update Your DEV Experience Level:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. πŸ›