149

I'm trying to delete a remote git branch with

git push origin :my_remote_branch

and getting:

error: unable to push to unqualified destination: my_remote_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.
error: failed to push some refs to 'git@example.com:/myrepo'

these are my current branches

git branch -a
* develop
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/develop
  remotes/origin/my_remote_branch

git branch -r --merged
  origin/HEAD -> origin/master
  origin/develop
  origin/master

Any ideas on how I can get rid of this branch would be appreciated.

Hugo
  • 12,334
  • 6
  • 30
  • 36
  • Did you try a `git fetch`to see if it fixed that non-existing remote name? Did you try to go in your `.git\refs\remotes\origin` and delete the `my_remote_branch` file, to see if that is enough? – VonC Apr 24 '12 at 06:16
  • did `git fetch` but that didn't help. Will try to search for a more straight forward solution before trying to delete the file manually. – Hugo Apr 24 '12 at 06:28
  • try to delete or at least move that file: it contains only the SHA1 of the tip of that branch. – VonC Apr 24 '12 at 06:52
  • 46
    You must do `git fetch -p` to get rid of stale tracking branches. – Jan Hudec Apr 24 '12 at 07:13

9 Answers9

286

The fact that refs/remotes/origin/my_remote_branch exists in your local repository does not imply refs/heads/my_remote_branch exists in the origin remote repository.

Do git fetch -p origin to make refs/remotes/origin/my_remote_branch go away if it's already deleted in origin. The -p option tells fetch to delete any tracking branches that no longer exist in the corresponding remotes; by default they are kept around.

Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
  • 1
    pruning did not help! while `git push origin --delete origin/feature/x` gave me error about non-existence ref `git push origin :feature/x` did delete the branch. So I guess `git push origin --delete ` should not include origin in the branchName (?) but it is bash-completed which is confusing. – dashesy May 24 '16 at 16:50
  • @dashesy, you were in different situation than the original asker. In the original question, the problem is that the remote branch was _already deleted_, presumably by somebody else, so only the local had to be synced. While you are deleting the remote. Indeed, the command should be `git push origin --delete feature/x`, because you are giving the name of the branch _at origin_, and it does _not_ have that prefix there (the two forms are otherwise synonymous anyway). – Jan Hudec May 24 '16 at 17:11
  • @JanHudec makes sense. Bash complete is smart when using `:` to not include `origin` but it completes as `origin/branch_name` instead of simply `branch_name` when using the `--delete` variant. – dashesy May 24 '16 at 17:56
61

Found question cleaning up old remote git branches and this did the trick

git branch -r -d origin/my_remote_branch
Community
  • 1
  • 1
Hugo
  • 12,334
  • 6
  • 30
  • 36
  • I've been trying to do exactly this for around an hour now, thanks a lot! – sebkkom Jan 19 '14 at 01:06
  • 1
    This does not delete the remote branch. If you go a git fetch you will see the branch again. It just remove it from your local list of remote branch. – Patrick Desjardins Oct 27 '14 at 15:07
  • 2
    @PatrickDesjardins exactly `git push origin :my_remote_branch` deletes the branch from the remote and `git branch -r -d origin/my_remote_branch` removes it from my local list of remote branches – Hugo Oct 28 '14 at 10:10
14

I ran across this when trying to delete a remote branch that had already been deleted. All that was needed was a prune:

git remote prune origin
Matt
  • 9,068
  • 12
  • 64
  • 84
6

Try following two options to delete remote branch forcibly

Option 1

get push origin --delete <branchName>

Option 2

git fetch -p origin
git branch -r -d origin/<branchName>
Pawan Maheshwari
  • 15,088
  • 1
  • 48
  • 50
3
git branch -r -d origin/my_remote_branch

was not enough for me. Before I had to go to server and work with git directory directly (which is dangerous and ugly) to remove the branch:

ssh mygitserver
su - git
cd /home/git/repositories/my_remote_branch.git/
git  --git-dir=. --work-tree=/tmp/ branch -D my_remote_branch
pevik
  • 4,523
  • 3
  • 33
  • 44
  • It's not just dangerous and ugly, it's also _wrong_. For one thing, this question is about deleting the branch locally when it _has_ been deleted at remote. And for the other, deleting a branch from remote is just a matter of `git push origin :my_remote_branch`. Mind the colon. – Jan Hudec Apr 07 '14 at 17:39
  • You're right Jan, that `git push origin :my_remote_branch` is normal way. This ugly and dangerous way is when this does not work (e.g. due to misconfigured git). – pevik Apr 08 '14 at 04:40
2

For me the problem was, that this was my default branch on github. I changed default branch, then delete operation was succeeded.

Hope it helps to someone

Vlad Pulichev
  • 3,162
  • 2
  • 20
  • 34
1

I have the similar problem. First went to this discussion, however I couldn't solve the problem until I saw https://stackoverflow.com/a/32147743/4209849.

which simply add a tip on distinguishing origin/my-branch-name and my-branch-name.

To be specific, I should use:

git push origin :my_remote_branch

instead of

git push origin :origin/my_remote_branch

This solved my problem at least, hope it would help others as well.

Community
  • 1
  • 1
Kangqiao Zhao
  • 111
  • 1
  • 8
0

Had this same issue, I manually edited my ./.git/config file to include:

[branch "branchName"]
remote = origin
merge = refs/heads/branchName

Which resulted into: error: src refspec branchName matches more than one. This I fixed by running $git tag -d branchName. After which I was able to push the new branch to upstream.

cfi
  • 10,915
  • 8
  • 57
  • 103
0

This worked for me: I created the remote branch on github UI and then pushed my local branch which had the same name to it. Try it in case other ways dont work. Other way would be creating a new branch locally and pushing an empty branch and later cherry-pick your commit and push again to your remote.

Nutan
  • 1,287
  • 11
  • 15