Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (29.3k 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 (50.2k 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.

Related questions

Browse Categories

...