'git branch -av' showing remote branch that no longer exists

Git

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

You have to do:

git remote prune origin

Solution 2 - Git

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

Solution 3 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCarpeNoctemView Question on Stackoverflow
Solution 1 - GitmanojldsView Answer on Stackoverflow
Solution 2 - GitPablo MaurinView Answer on Stackoverflow
Solution 3 - GitngbtwbyView Answer on Stackoverflow