CarpeNoctem
CarpeNoctem

Reputation: 5660

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

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.

Upvotes: 199

Views: 31131

Answers (5)

MDAbdurrahman
MDAbdurrahman

Reputation: 1

git branch -d branch name -> deletes local branch name. If there are unmerged changes, Git does not allow you to delete it.

git branch -D branch name -> forces delete local branch name, even if there are unmerged changes. When you are sure you want to delete local branch name permanently, execute this command.

Then follow up with: git push origin -delete branch name -> deletes the branch name from GitHub

Then to be sure follow up with: git branch -a -> lists all branches (local and remote) or git branch -av -> lists all branches (local and remote) with last commit

Upvotes: 0

Janne Enberg
Janne Enberg

Reputation: 2049

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

Upvotes: 0

ngbtwby
ngbtwby

Reputation: 412

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

Upvotes: 27

Pablo Maurin
Pablo Maurin

Reputation: 1490

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

Upvotes: 62

manojlds
manojlds

Reputation: 301147

You have to do:

git remote prune origin

Upvotes: 396

Related Questions