Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete all remote git branches which have already been integrated?

Tags:

git

git-branch

At work we are using topic branches which are integrated into a few (3) master branches at some point. Now I'd like to delete all topic branches from my remote repository which have been fully integrated into a master branch. If that's not possible, retrieving a list of local branches which have been integrated would be fine, too.

like image 878
ThiefMaster Avatar asked Nov 22 '11 15:11

ThiefMaster


People also ask

How do I delete all remote branches?

Let's break this command: First we get all remote branches using the git branch -r command. Next, we get the local branches not on the remote using the egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) command, Finally we delete the branches using the xargs git branch -d command.

How do I delete old branches in git?

The easiest way to delete local Git branches is to use the “git branch” command with the “-d” option. The “-d” option stands for “–delete” and it can be used whenever the branch you want to clean up is completely merged with your upstream branch.

How do I force delete a remote branch?

Deleting remote branches To delete a remote branch, you can't use the git branch command. Instead, use the git push command with --delete flag, followed by the name of the branch you want to delete. You also need to specify the remote name ( origin in this case) after git push .


1 Answers

Another answer edited in by someone who thought it's the best (and it looks good):

git branch -r --merged origin/master | grep -v master | grep "origin/" | cut -d "/" -f 2- | xargs -n 20 git push --delete origin 

Explanation:

  • git branch -r --merged origin/master
    • -r/--remotes list the remote-tracking branches.
    • --merged origin/master only list branches whose tips are reachable from origin/master.
  • grep -v master remove any branch name containing master from list.1-v means negative match.
  • grep "origin/" select only branches on origin remote.
  • cut -d "/" -f 2- drop the origin/ prefix
  • xargs -n 20 git push --delete origin do something similar to git push --delete origin branch-a branch-b branch-c …
    • -n 20/--max-args=20 use at most 20 arguments per command line.

As for -n, I choose 20 just as an example. Fewer arguments will make it slower, for example -n 1 makes it delete one at a time; you have more progress hints, because it will report each time it deletes a branch. More arguments like -n 200 will make it faster (less total time), but it only reports once every 200 branches, making you think that it is frozen at first (while it is not). Tune the number to your need. If you omit this option, the default number is really large (2048 in my machine).

1. Note that this also removes origin/HEAD -> origin/master, but you won't want to mess with origin/HEAD anyway.

Original answer:

git push --delete remote topicbranch 

or

git push remote :topicbranch 

Giving a list of branches, would be something with git branch --merged master

like image 169
Michael Krelin - hacker Avatar answered Oct 03 '22 11:10

Michael Krelin - hacker