DEV Community

Cover image for Remove merged branches from your local machine

Remove merged branches from your local machine

Tom Nijhof on January 17, 2024

Remove merged branches from your local machine If you work with a remote Git repository, you might encounter a situation where you sti...
Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

Cool script, but personally I would feel a bit paranoid about just yeeting branches like that. I'd probably drop this into a script and make it so it opens a list of commands in vim or something so I could individually decide if I want to keep any of them after all

Collapse
 
wagenrace profile image
Tom Nijhof

It does check if the branches are related to a "gone" origin branch. If you have a branch without origin, it will stay. So only if the origin branch is merged or delete it will delete the local one

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

Yea, but I can think of cases where that might happen where you still wouldn't want to delete the local branch. Knowing myself, I just think this looks like a massive foot-gun, and I'd add some safety features to protect myself from myself.

Thread Thread
 
wagenrace profile image
Tom Nijhof

Do you not work with feature branches? I normally make a branch for a feature and when I am done with it I merge

But in the name of science!! I added a commit to a local branch where the remote was merged and deleted. It gave me this error:
Image description
Meaning it is not possible to delete local branches that are commits ahead

Thread Thread
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

What if you create a branch for local use, push it by accident and delete it from the remote repository again? Wouldn't that then cause the local branch to get deleted as well?

Thread Thread
 
wagenrace profile image
Tom Nijhof

Not if there are still commits on the branch, it will give the same error with not fully merged

Collapse
 
joolsmcfly profile image
Julien Dephix

And if you're using a JetBrains product you have the simple and effective Git Branch Cleaner plugin.
I mapped it to Ctrl+Shift+Alt+Delete so I get a modal which lists local branches that no longer have their remote equivalent. "select all" and "delete". Done!

Collapse
 
tiuwill profile image
Willian Ferreira Moya

Thanks! That's a great tip!
I'll start cleaning up my local branchs. My local branchs are a total mess, I'm starting to get loss on thing that I'm not working anymore, and this will help me do that fast!

Collapse
 
dfabiolb profile image
Fábio Luiz Barbosa

I'm not sure how this will help, but I might be able to translate it to a PowerShell version if there is someone interested in that works a few tries and for the challenge as well. The main issue for me was how I could convert awk and xargs so far shorter that I was able to reach it was something like this:

git fetch -p; git branch -vv | ForEach-Object { if ($_ -match ':\s+gone\]') { git branch -d $_.Split(' ')[2] } }
Enter fullscreen mode Exit fullscreen mode
Collapse
 
wagenrace profile image
Tom Nijhof

Thank you! Very useful for PowerShell users!!

Collapse
 
jeremysawesome profile image
Jeremiah Smith

This will work for PowerShell, cause posh-git is the bestest. Update it for your own use case (i.e. swap master for main). Also - I keep the first run as a lowercase -d and just scan the proposed branches to delete before I swap the git branch -d command to use a -D and run again.

git checkout master; git remote update origin --prune; git branch -vv | Select-String -Pattern ": gone]" | % { $_.toString().Trim().Split(" ")[0]} | % {git branch -d $_}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
pixelrella profile image
Pixelrella

Thanks for sharing! I appreciate that you took the time to break down the different parts of the commands. Your explanations were clear and concise.
Thanks to your descriptions, piping finally makes sense for me💡

Collapse
 
benajaero profile image
ben ajaero

Thank you for the informative tutorial Tom!

This is definitely a useful workflow to clean things up.

I've been in situations where a branch is removed from origin but still exists locally, causing confusion down the line. Please keep up the great content. I'll be sure to check out other articles on your blog!

Collapse
 
wagenrace profile image
Tom Nijhof

I will move my other articles too, the community interaction here is exactly what I missed on Medium!
Thank you all for a warm welcome

Collapse
 
jnareb profile image
Jakub Narębski

You have perfectly fine git for-each-ref for scripting; why use git branch -vv intended for user output and then extracting data with awk?

Collapse
 
wagenrace profile image
Tom Nijhof

Simple: I did not though of it

Collapse
 
tnypxl profile image
tnypxl

Might be a great command for running at the end of every year on a cron job or something.

Collapse
 
wagenrace profile image
Tom Nijhof

I like to do it manually, given that I want to be in control of deleting actions. But I will not stop you from living life more dangerous!

Collapse
 
tnypxl profile image
tnypxl

I’m trying to automate my life away over here.

Collapse
 
thomasbnt profile image
Thomas Bnt

Thanks for this information!
You have a little typo error with the markdown image 🙌

Collapse
 
wagenrace profile image
Tom Nijhof

Thanks for pointing it out, I think all of them are gone now. Was a bit more than one

Collapse
 
pavithra_sandamini profile image
Pavithra Sandamini

Great content, thanks for sharing

Collapse
 
ccoveille profile image
Christophe Colombier

I'm a bit surprised no one mentioned

git config --global fetch.prune true
Enter fullscreen mode Exit fullscreen mode

Then you have nothing to do.

Collapse
 
msamgan profile image
Mohammed Samgan Khan

if you want to delete all the local branches but master, you can use this codebysamgan.com/how-to-delete-all...