How do you remove an invalid remote branch reference from Git?

GitBranchRemote Branch

Git Problem Overview


In my current repo I have the following output:

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

I want to delete remotes/public/master from the branch list:

$ git branch -d remotes/public/master
error: branch 'remotes/public/master' not found.

Also, the output of git remote is strange, since it does not list public:

$ git remote show 
origin

How can I delete 'remotes/public/master' from the branch list?

Update, tried the git push command:

$ git push public :master
fatal: 'public' does not appear to be a git repository
fatal: The remote end hung up unexpectedly

Git Solutions


Solution 1 - Git

You might be needing a cleanup:

git gc --prune=now

or you might be needing a prune:

git remote prune public

   

> prune > >Deletes all stale tracking branches under <name>. These stale branches have already been removed from the remote repository referenced by <name>, but are still locally available in "remotes/<name>". > >With --dry-run option, report what branches will be pruned, but do no actually prune them.

However, it appears these should have been cleaned up earlier with

git remote rm public 

>rm > >Remove the remote named <name>. All remote tracking branches and configuration settings for the remote are removed.

So it might be you hand-edited your config file and this did not occur, or you have privilege problems.

Maybe run that again and see what happens.


Advice Context

If you take a look in the revision logs, you'll note I suggested more "correct" techniques, which for whatever reason didn't want to work on their repository.

I suspected the OP had done something that left their tree in an inconsistent state that caused it to behave a bit strangely, and git gc was required to fix up the left behind cruft.

Usually git branch -rd origin/badbranch is sufficient for nuking a local tracking branch , or git push origin :badbranch for nuking a remote branch, and usually you will never need to call git gc

Solution 2 - Git

All you need to do is

git fetch -p

It'll remove all your local branches which are remotely deleted.

If you are on git 1.8.5+ you can set this automatically

git config fetch.prune true

or

git config --global fetch.prune true

Solution 3 - Git

git push public :master

This would delete the remote branch named master as Kent Fredric has pointed out.

To list remote-tracking branches:

git branch -r

To delete a remote-tracking branch:

git branch -rd public/master

Solution 4 - Git

All you need to do is

$ git branch -rd origin/whatever 

It's that simple. There is no reason to call a gc here.

Solution 5 - Git

git gc --prune=now is not what you want.

git remote prune public

or git remote prune origin # if thats the the remote source

is what you want

Solution 6 - Git

The accepted answer didn't work for me when the ref was packed. This does however:

$ git remote add public http://anything.com/bogus.git
$ git remote rm public

Solution 7 - Git

In my case I was trying to delete entries that were saved in .git/packed-refs. You can edit this plain text file and delete entries from it that git br -D doesn't know how to touch (At least in ver 1.7.9.5).

I found this solution here: https://stackoverflow.com/a/11050880/1695680

Solution 8 - Git

git push origin --delete <branch name>

Referenced from: http://www.gitguys.com/topics/adding-and-removing-remote-branches/

Solution 9 - Git

I didn't know about git branch -rd, so the way I have solved issues like this for myself is to treat my repo as a remote repo and do a remote delete. git push . :refs/remotes/public/master. If the other ways don't work and you have some weird reference you want to get rid of, this raw way is surefire. It gives you the exact precision to remove (or create!) any kind of reference.

Solution 10 - Git

I had a similar problem. None of the answers helped. In my case, I had two removed remote repositories showing up permanently.

My last idea was to remove all references to it by hand.

Let's say the repository is called “Repo”. I did:

find .git -name Repo 

So, I deleted the corresponding files and directories from the .git folder (this folder could be found in your Rails app or on your computer https://stackoverflow.com/a/19538763/6638513).

Then I did:

grep Repo -r .git

This found some text files in which I removed the corresponding lines. Now, everything seems to be fine.

Usually, you should leave this job to git.

Solution 11 - Git

Only slightly related, but still might be helpful in the same situation as we had - we use a network file share for our remote repository. Last week things were working, this week we were getting the error "Remote origin did not advertise Ref for branch refs/heads/master. This Ref may not exist in the remote or may be hidden by permission settings"

But we believed nothing had been done to corrupt things. The NFS does snapshots so I reviewed each "previous version" and saw that three days ago, the size in MB of the repository had gone from 282MB to 33MB, and about 1,403 new files and 300 folders now existed. I queried my co-workers and one had tried to do a push that day - then cancelled it.

I used the NFS "Restore" functionality to restore it to just before that date and now everythings working fine again. I did try the prune previously, didnt seem to help. Maybe the harsher cleanups would have worked.

Hope this might help someone else one day!

Jay

Solution 12 - Git

Not sure how I got into the mess, but my error message was slightly different:

>

> git remote
> > git branch
>   warning: ignoring broken ref refs/remotes/origin/HEAD
>   * main
>

But I was able to fix it by slightly reinterpreting a fix from elsewhere on this page. By this I mean I substituted the keyword HEAD for the branch name in <remote>/<branch>. None of the other suggestions people mentioned had worked (gc, prune, etc.) so I was running out of ideas and hoping for the best. Anyway, it worked like a charm:

>

> git remote
> git branch -rd origin/HEAD > Deleted remote-tracking branch origin/HEAD (was refs/remotes/origin/main).

> git branch
* main
>

Solution 13 - Git

I tried everything here and nothing worked. If all else fails, remove the offending branches from the text file '.git/packed-refs', then remove the offending branches from '.git\refs\remotes\origin<branchName>'

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
QuestioncmcgintyView Question on Stackoverflow
Solution 1 - GitKent FredricView Answer on Stackoverflow
Solution 2 - GitPawan MaheshwariView Answer on Stackoverflow
Solution 3 - GitAlan Haggai AlaviView Answer on Stackoverflow
Solution 4 - GitjpswainView Answer on Stackoverflow
Solution 5 - GittonguerooView Answer on Stackoverflow
Solution 6 - GitchrisView Answer on Stackoverflow
Solution 7 - GitThorSummonerView Answer on Stackoverflow
Solution 8 - Gitkip2View Answer on Stackoverflow
Solution 9 - GitclackeView Answer on Stackoverflow
Solution 10 - GitKeinsteinView Answer on Stackoverflow
Solution 11 - GitJGlassView Answer on Stackoverflow
Solution 12 - GitGlenn SlaydenView Answer on Stackoverflow
Solution 13 - GitMarvin ThobejaneView Answer on Stackoverflow