23

Looking for a command to delete all branches on Git repository except for master and push them to remote.

This is to clean up Git repository before making a release as the previous history branches everything else are totally dev changes and doesn't matter.

Biffen
  • 5,791
  • 5
  • 29
  • 34
willsteel
  • 907
  • 1
  • 10
  • 21
  • 4
    This sounds kind of dangerous especially if the team is working on new features for a future release. You may take a look at a branching strategy named Git Flow. This strategy makes it such that as soon as you are done with a branch and have merged it up, that it should then be deleted. https://leanpub.com/git-flow/read – dmoore1181 Feb 21 '19 at 13:47
  • **See Also**: [Delete all local git branches](https://stackoverflow.com/q/10610327/1366033) – KyleMit Jul 02 '21 at 18:53

3 Answers3

51

This will remove all branches (except for master), even if the branch has a slash '/' in it:

git branch -r | grep 'origin' | grep -v 'master$' | grep -v HEAD | cut -d/ -f2- | while read line; do git push origin :heads/$line; done;

This will do the same, leaving both develop and master branches alone:

git branch -r | grep 'origin' | grep -v 'master$' | grep -v 'develop$' | grep -v HEAD | cut -d/ -f2- | while read line; do git push origin :heads/$line; done;

This is the script for fish shell:

git branch -r | grep 'origin' | grep -v 'master$' | grep -v 'develop$' | grep -v HEAD | cut -d/ -f2- | while read line; git push origin :heads/$line; end;
staxim
  • 979
  • 11
  • 14
  • this ask passphrase for each branch – Jones G Aug 13 '21 at 12:55
  • 1
    This solution will run multiple `git push origin ...` commands, one for each branch to be removed. If your git configuration requires authentication for each `git push` and this is not as intended, you may consider using SSH auth or looking into your git configuration. I `git push` without interactive auth, so the command above executes without any intervention or additional steps. As they say, YMMV. – staxim Aug 13 '21 at 15:41
  • Brilliant my friend. – Florin Dec 13 '21 at 17:58
-1

Finally below command just worked like charm for me, you can tweak it for further requirements.

git branch -r | grep origin/ | grep -v 'master$' | grep -v HEAD| cut -d/ -f2 | while read line; do git push origin :$line; done;
willsteel
  • 907
  • 1
  • 10
  • 21
  • 6
    why don't you just approve @staxim answer your's is the same, just write a comment below his that is `origin/` instead of `'origin'` – stanimirsp Jun 26 '20 at 07:19
  • 2
    this is a copy of the previous answer. Please just upvote that rather than repeat it. – RichieHH Jan 10 '21 at 08:01
  • 2
    @RichieHH I may be missing something, but it looks like willsteel's answer was posted on the day he asked his question while staxim's answer was posted a few months later. It would seem better if staxim had edited willsteel's answer instead of posting his own – takanuva15 Aug 23 '21 at 18:19
-3

Can use below command, it will remove all branches except master and the current one

git branch | grep -v "master\|$(git branch --show-current)" | xargs git branch -D

Maneesh Sharma
  • 220
  • 1
  • 2
  • 8
  • 2
    This will remove local branches, and the author asks about remote branches – Danon Nov 20 '20 at 09:15
  • 1
    Better to read the question rather than give a potentially dangerous answer. This removes LOCAL branches potentially destroying weeks of work. – RichieHH Jan 10 '21 at 08:02
  • This is actually what I was looking for even though it didn't answer the question lol – takanuva15 Aug 23 '21 at 18:26