Scott
Scott

Reputation: 13921

Can't delete branch even though it shows up in git branch

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

Upvotes: 2

Views: 2008

Answers (2)

tratatun
tratatun

Reputation: 133

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)

Upvotes: -1

larsks
larsks

Reputation: 311258

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.

Upvotes: 5

Related Questions