Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete multiple branches in a single run

I can see the article Delete branches in Bitbucket but this is just deleting single branch at a time. I want to delete multiple branches in a single run, does this possible by command line or manually?

like image 298
Anupam Mishra Avatar asked Dec 17 '22 16:12

Anupam Mishra


1 Answers

Step 1, local branches

Like Flimzy suggested, branch -d accepts a series of refs.

Also, if needed, maybe consider having a first command to generate the branch list, and pass the result to the branch command. For example if you decided to delete merged branches

git branch -d `git branch --merged`

or, other hypothetical situation, if you want to get rid of all branches having some bad commit (introduced a bug)

git branch -d `git branch --contains <bad-commit>`

In this second example, git might complain that some branches are not fully merged, i.e. have commits that are present only on them, not reachable from any other branches. It's a safeguard to avoid accidentally losing work, right in the git principles.

To override this mechanism in cases where you do know that these branches are not fully merged but you still want to delete them, the alternative is to use the -D flag instead of -d, but beware if you use this in conjonction with a list like we saw above, or be sure to output and check the list before feeding it to the deletion command.


Step 2, remote branches

And to answer Vlad's useful comment, yes this operation only affects the local repo, so to reflect it on remote (I'll here assume origin but do adapt it to your case), you'd have to

git push -d origin <branchList>

Step 3, remote-tracking branches

Finally, as your local repo still at this point retains the obsolete remote-tracking branches corresponding to the remote branches we just deleted, finish with a

git remote prune origin
# or
git fetch --prune
like image 182
Romain Valeri Avatar answered Jan 26 '23 23:01

Romain Valeri