0

Based on https://stackoverflow.com/a/227026/117421

I've come up with the following is there any errors?

The idea is to find and remove old branches in git.

git branch -r --merged origin/master gives you all remote branches that have been merged into master. However it includes by default master itself and appears to contain HEAD -> origin/master as well.

Find all branches merged into master excluding master and HEAD

git branch -r --merged origin/master | grep -v origin/master excludes these two entries.

This is trivial to get to a loop in bash.

for remotebranch in $(git branch -r --merged origin/master | grep -v origin/master); do
echo $remotebranch
done

However this lists the branch name including origin which is the wrong format for push --delete

This can be fixed by replacing origin/ with the empty string.

Remove origin/ prefix

for remotebranch in $(git branch -r --merged origin/master | grep -v origin/master | sed 's/origin\///g'); do
echo $remotebranch
done

So I think the following may be run, is it correct, or is there a better way to do it?

Final Solution

for remotebranch in $(git branch -r --merged origin/master \
| grep -v origin/master \
| sed 's/origin\///g'); do
git push origin --delete $remotebranch
done

finally you can always run git fetch --prune

Community
  • 1
  • 1
Wes
  • 5,797
  • 5
  • 29
  • 55

0 Answers0