116

In short;

  • How can I delete remote multiple merged remotes?

More background;

I have a git repo with tens of remotes which have been merged into master. I can delete these remotes one at a time by using:

git push --delete origin myBranch-1234

However this is a slow and tedious process for all remotes. So I'm trying this command:

git branch -r --merged | grep origin | grep -v master | xargs git push origin --delete

git branch -r --merged lists all merged remotes.
grep origin tells the command to include origin.
grep -v master tells the command to exclude master.
xargs git push origin --delete tells the command to delete the list of remotes.

All together, I expect this to gather all merged remotes and delete them.

When I run the above command, I receive the following for every merged remote;

error: unable to delete 'origin/myBranch-1234': remote ref does not exist
error: unable to delete 'origin/myBranch-1235': remote ref does not exist
error: unable to delete 'origin/myBranch-1236': remote ref does not exist
error: unable to delete 'origin/myBranch-1237': remote ref does not exist
... etc

However these remotes do exist and I can checkout each of them. Many sites and people recommend that I run git fetch --prune to clean up missing references. This does nothing because all of these remotes exist.

So I ask you, dear stack exchange;

  • Why can I delete one remote, but not many?
  • Is my command correct?

I think I'm missing something small. Every time I research this, it seems like I'm doing this correctly, but I'm getting the above errors.

Community
  • 1
  • 1
Jqw
  • 1,171
  • 2
  • 8
  • 4

3 Answers3

244

You may need to prune your local "cache" of remote branches first. Try running:

git fetch -p origin

before deleting.

Igor
  • 33,276
  • 14
  • 79
  • 112
  • 2
    That solved it for me, thanks! However, why is it not updating when I fetch without `--prune`? In my opinion that's pretty misleading – dave0688 Mar 27 '18 at 14:06
  • 9
    Just a note for people arriving here from a search - while this is clearly useful to the majority who land here, it's not actually correct in terms of the original issue - the OP's problem was that his command incorrectly included `origin/` in the branch names, as described/resolved in other answers. – CupawnTae Apr 11 '18 at 09:58
113

Are those branches removed from the remote (origin)? If yes, you can simply do

git fetch --prune origin

Otherwise they might return even after you delete them locally.

Update: Looking at your command again, it looks like you're building it incorrectly. You probably want

git push origin --delete myBranch-1234

but instead you are doing something like

git push origin --delete origin/myBranch-1234
Matthew Read
  • 1,365
  • 1
  • 30
  • 50
Mykola Gurov
  • 8,517
  • 4
  • 29
  • 27
  • None of the branches are removed in origin. I can checkout any of them. – Jqw Aug 21 '15 at 19:55
  • This command works for me: `git push --delete origin myBranch-1234`. When I try to delete all merged branches by `git branch -r --merged | grep origin | grep -v master | xargs git push origin --delete`, It tells me that the remote ref does not exist. – Jqw Aug 21 '15 at 20:05
  • 1
    yes, because you execute different commands in the second case. – Mykola Gurov Aug 21 '15 at 20:12
  • 1
    I don't understand your point. I know that I'm using two different commands. The second case is getting all merged remotes with `git branch -r --merged` and then I am trying to delete them with `xargs git push origin --delete`. The command is attempting to delete the remotes except that it thinks the `remote ref does not exist`. – Jqw Aug 21 '15 at 20:23
  • 1
    in your second, piped command, put echo right next to `xargs` to see what is actually executed. – Mykola Gurov Aug 21 '15 at 20:25
  • I included the `echo` as you recommended to create this command: `git branch -r --merged | grep origin | grep -v master | grep -v dev | echo xargs git push --delete origin` and it returned `xargs git push --delete origin`. This is what I expected to see. I also tried `... | xargs git push --delete origin/myBranch-1234` and it still resulted in `remote ref does not exist` as I've explained in my summary above. – Jqw Aug 21 '15 at 20:54
  • 2
    @Jqw You cannot include the `origin/` prefix on the branch name, git already knows you're working with origin's branches since you are running `git push origin`. Mykola's suggestion was to make the last part of the command `xargs echo`, rather than `echo xargs [...]`. – Matthew Read Aug 23 '16 at 19:15
15

Use sed to remove 'origin/' part and change a lttile xargs part.

git branch -r --merged | grep origin | grep -v -e master | sed s/origin\\/// |  xargs -I{} git push origin --delete {}
kost
  • 705
  • 7
  • 18