How to delete git branches that don’t exist on the remote

Asked

Viewed 782 times

4

In my current project, I locally created several branches for local testing - all from master. Now, I have more than 20 local branches that don’t exist on the server.

I wanted to clean up here, and delete all local branches that don’t exist remotely (either because they’ve already been deleted remotely, or because they never existed there). I didn’t want to delete one by one in my hand, but I still haven’t found a way...

What do you recommend? Does anyone know how to do with one command? A script is also acceptable - I use Windows, but I don’t want to limit responses to other operating systems if someone suggests a script or something like that...

  • I think it would be good to limit to your operating system, otherwise it will rain answers that will not even be useful to you.

2 answers

4

The command below deletes all deleted branches that never existed on the remote:

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

    grep - used on this line - is Linux command. I accepted the other answer because at the moment I am using Windows and that worked. But this one is very useful also for those who use Linux

  • 1

    if you have in windows using git bash this command works too!

  • True, Eduardo... good point

2


In Windows Power Shell, in response:

git checkout master; git remote update origin --prune; git branch -vv | Select-String -Pattern ": gone]" | % { $_.toString().Trim().Split(" ")[0]} | % {git branch -d $_}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.