Vitalii Zurian
Vitalii Zurian

Reputation: 17976

GIT - unable to remove specific branch from remote repository

I cannot remove a remote branch called origin/featureBranch. I guess it's because the branch name starts with origin, but I am not sure:

$ git branch -r | grep featureBranch
  origin/origin/featureBranch

$ git push origin :origin/featureBranch

    error: unable to push to unqualified destination: origin/featureBranch

    The destination refspec neither matches an existing ref on the remote nor
    begins with refs/, and we are unable to guess a prefix based on the source ref.

    error: failed to push some refs to '[email protected]:myCompany/my-repo.git'

UPDATE

$ git push origin :featureBranch gives the same error.

NOTE

In remote branch is origin/origin/myFeature, locally it is origin/myFeature.

I know what origin usually means, but in my case - this is part of name of the branch.

Github does not see this branch.


Could please anyone explain me what happens "behind the scene" and how can I remove this branch?

Upvotes: 4

Views: 859

Answers (4)

Sergiu Dumitriu
Sergiu Dumitriu

Reputation: 11601

Try this:

git push origin :refs/heads/origin/featureBranch

You can always reference branches by their technical name under refs/heads/.

A branch is stored as a small text file under .git/refs/. Local branches go under .git/refs/heads/, and remote branches under .git/refs/remotes/<remotename>/. A simple branch like master will thus be found at .git/refs/heads/master and .git/refs/remotes/origin/master, but your buggy branch will actually reside under .git/refs/heads/origin/featureBranch. It will not be confused with a remote branch on the origin repository because it's not under refs/remotes/origin/, but under refs/heads/.

On the remote server, the origin/featureBranch branch is local to the server, so it will be stored under refs/heads/. When pushing to a given branch, you can identify it either by its name, or by its path, so if a name doesn't work, just use the full path starting with refs.

How you ended up with this weird branch name? I can't tell for sure, since I don't know what you did, but I encountered the same problem when I used git push --mirror, which pushes all the references, including remote ones, so it will create an origin/branchname as a local branch.

Upvotes: 11

Michael Anderson
Michael Anderson

Reputation: 73480

This bit of the message says it can't find any matching branch.

The destination refspec neither matches an existing ref on the remote nor
begins with refs/, and we are unable to guess a prefix based on the source ref.

It's what I get if I try to delete the branch twice. (The first succeeds, and the second fails). Or if the branch has been deleted by someone else. You could try git remote prune and see if it disapears from your git branch -r output. You could also check the branch is on origin using git ls-remote [email protected]:myCompany/my-repo.git.

Deleting oddly (badly?) named branches like origin/master on origin works just fine for me:

$ git push origin master:origin/master
Counting objects: 3, done.
Writing objects: 100% (3/3), 218 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To ../bare/
 * [new branch]      master -> origin/master

$ git branch -r
  origin/origin/master

$ git push origin :origin/master
To ../bare/
 - [deleted]         origin/master

Upvotes: 1

Tigraine
Tigraine

Reputation: 23648

This should work:

$ git push origin :featureBranch

You already specified where to push, the origin/ is only the name of your local branch.

Background: When git does a fetch, it creates so called 'remote tracking branches'. Those don't really differ from your own branches, they are simply prefixed with the remote name (origin/master for example) and represent the server branches locally (all commits have already been fetched).

When you do a push origin you are not referring at all to your local remote tracking branches but rather tell Git "push to origin sourcebranch:targetbranch"

Upvotes: 2

Urs Reupke
Urs Reupke

Reputation: 6921

Simply drop origin behind the colon:

git push origin :featureBranch

As Michael explains, it means to push "nothing" to the named branch. Here is a more complete explanation.

Upvotes: 0

Related Questions