198

This is probably a dumb question, but I'm brand new to git and am seeing a remote branch that no longer exists.

$ git branch -a
* master
  remotes/origin/master
  remotes/origin/production

I don't believe the production branch exists remotely and can't figure out why it still shows locally. How can I delete/remove this branch? Here's what an attempt to remove it looks like:

$ git push origin :production

error: unable to push to unqualified destination: production
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@IP:puppet.git'

I can checkout the supposedly remote production branch but get this:

$ git checkout origin/production
Note: checking out 'origin/production'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at c323996... added powerdns module, no really

I have no clue what the heck I'm doing. Any help would be appreciated.

3

4 Answers 4

393

You have to do:

git remote prune origin
4
  • 10
    Thanks that worked! Can you elaborate on what's going on behind the scenes? Commented Jan 7, 2012 at 1:46
  • 16
    These are remote tracking branches on your local repo and you have to clear them off if the branches are deleted on the remote repo.
    – manojlds
    Commented Jan 7, 2012 at 1:47
  • 2
    Rarely do I so quickly find and implement such a straightforward question and answer.
    – jleach
    Commented Apr 7, 2017 at 12:46
  • Superb one.In my local i was seeing weird branches which had no connection with the code base repo .when i ran this command , it tuned my local origin branch and then i added upstream master .thanks Commented Sep 14, 2017 at 1:16
62

So there are two problems. In both cases, remember Git is distributed.

First. When you do things like

$ git branch -a

the operation is performed on your local repo NOT the remote computer. In other words, your local repo is reporting all the branches that is knows about. These could be local branches (like 'master') or remote branches that it has fetched from a remote. Since the last fetch, the 'production' branch of the remote repo has changed, but your local repo does not know this. The answer from manojlds, is correct. Run

$ git remote prune origin

to remove stale branches.

The 'git push origin :production' command is used for deleting the branch from the remote computer's git repo. Not your local repo. In this case, someone else has already deleted the branch on the remote computer's git repo, so you see this error message.

Here is a link that summarizes these commands.

The second problem deals with checkout.

When checking out a branch, you want to do so from a local branch, not the remote branch. That is why you get the error about a detached HEAD. The git-notes repo has a good explanation of the problem in gory detail. Basically the key phrase is

However, when you checkout anything that is not a proper, local, branch name, then HEAD is no longer a symbolic reference to anything. Instead, it actually contains the SHA-1 hash (the commit id) of the commit you are switching to.

Now, how to check out a local branch, that is the same as the remote branch?

Easy, you create a local branch, at the time of checkout remote branch.

$ git checkout -b my_local_branch origin/production

27
git remote prune origin

is right, just adding you can use --dry-run option, that reports what branches will be pruned from your local repo, but doesnt actually prune them

git remote prune origin --dry-run
0

The commands from the other answers don't seem to accomplish the desired goal, which seems to be to clean up your list of branches listed under origin from branches that no longer exist. At least not in 2023.

What did work however is:

git fetch --prune origin

Not the answer you're looking for? Browse other questions tagged or ask your own question.