Search code examples
git

Git says remote ref does not exist when I delete remote branch


I ran git branch -a

* master
  remotes/origin/test
  remotes/origin/master

I want to delete my remote branch

I've tried

git push origin --delete remotes/origin/test

I got

error: unable to delete 'remotes/origin/test': remote ref does not exist

How is it not exist ?

I did a git branch -a, and I saw it listed.

Did I miss anything ?


Solution

  • The command git branch -a shows remote branches that exist in your local repository. This may sound a bit confusing but to understand it, you have to understand that there is a difference between a remote branch, and a branch that exists in a remote repository. Remote branches are local branches that map to branches of the remote repository. So the set of remote branches represent the state of the remote repository.

    The usual way to update the list of remote branches is to use git fetch. This automatically gets an updated list of branches from the remote and sets up remote branches in the local repository, also fetching any commit objects you may be missing.

    However, by default, git fetch does not remove remote branches that no longer have a counterpart branch on the remote. In order to do that, you explicitly need to prune the list of remote branches:

    git fetch --prune
    

    This will automatically get rid of remote branches that no longer exist on the remote. Afterwards, git branch -r will show you an updated list of branches that really exist on the remote: And those you can delete using git push.

    That being said, in order to use git push --delete, you need to specify the name of the branch on the remote repository; not the name of your remote branch. So to delete the branch test (represented by your remote branch origin/test), you would use git push origin --delete test.