0 votes
1 view
in Devops and Agile by (28.1k points)

With git remote prune origin I can remove the local branches that are not on the remote anymore.

But I also want to remove local branches that were created from those remote branches (a check if they are unmerged would be nice).

How can I do this?

1 Answer

0 votes
by (48k points)

After pruning, you can get the list of remote branches with git branch -r. The list of branches with their remote-tracking branch can be retrieved with git branch -vv

Then this single liner will do the trick

git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -d

This string gets the list of remote branches and passes it into egrep through the standard input.

And filters the branches that have a remote-tracking branch. Then get the first column of that output which will be the branch name. Finally passing all the branch names into the delete branch command.

Also, remember that you'll need to run git fetch --prune first, otherwise git branch -r will still see the remote branches.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...