2

git branch -r --merged | grep 123

OUTPUT

origin/feature/123-some-feature

git branch -r --merged | grep 123 | xargs git branch -d

OUTPUT error: branch 'origin/feature/123-some-feature' not found.

Why can't I delete this remote branch?

EDIT: Sorry, should have been more clear on my initial post (I was in a meeting when I wrote this). I don't have any of these branches locally and I'm trying to do a cleanup of the remote repository to delete all merged branches (I'm testing it by filtering to just '123' for now).

I want to get a filtered (via grep) list of the remote branches that have been merged so I can review them locally to be sure I'm not going to delete any branches I want to keep.

Then I want to execute it again with | xargs git branch -d to actually remove those branches from the remote repository.

I think this would be what I want based on the posted answer and one of the comments?

git branch -r --merged | grep 123 | xargs git push origin --delete

Scott
  • 13,735
  • 20
  • 94
  • 152
  • To delete a remote branch you need to `git push --delete` (or `git push origin :branch`, notice the colon), not just `git branch -d` – madhead Apr 22 '16 at 16:34
  • Depending on what exactly you're trying to do, this may be a duplicate of: http://stackoverflow.com/q/1072171/1157054 – Ajedi32 Apr 22 '16 at 16:43

2 Answers2

5

You can't delete it because it's a remote branch. It exists in the remote repository, not in your local repository. If you want to delete it on the remote, you can:

git push --delete origin feature/123-some-feature

But understand that this will affect the availability of the branch on the remote repository.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • Sorry, should have been more clear. I actually want to delete these branches on the remote repository. – Scott Apr 22 '16 at 16:45
-1

You shouldn't use git branch -r to delete remote branch.
Use git push origin :your/branch/name (be sure you entered colon : before branchname)

tratatun
  • 135
  • 1
  • 8
  • why would you post a more cryptic version of the accepted answer (using a colon `:` is the same as saying `--delete`)? – haelix Jun 11 '19 at 12:39