834

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
cmcginty
  • 113,384
  • 42
  • 163
  • 163
  • 43
    Did `git remote prune [remote-name]` or `git fetch -p [remote-name]` not work in your scenario? Doing it with `git gc` is a lot more forceful than is normally needed. – rjmunro Dec 06 '12 at 12:11
  • 7
    `git remote prune [remote-name]` won't work with git svn, although neither does `git gc`... `git branch -rd origin/name` does work though. @Casey, you probably should select the second answer -it's slightly less dangerous. – naught101 Jan 07 '15 at 03:01
  • 4
    I love this question. Coming back almost every month – oluckyman Jun 18 '15 at 23:33
  • Related, if not a dupe target: [Delete a Git branch both locally and remotely](http://stackoverflow.com/q/2003505/456814). –  Oct 18 '15 at 01:27
  • 2
    To avoid n00b gitter error in the future, I recommend using a different example branch than `master`...particularly when deleting on the remote. – absynce Apr 14 '16 at 21:30
  • The real question is, how/why does Git manage to get into the broken state in the first place... Local branch is deleted, remote branch is deleted, but Git is clinging to old dated information. Who thought it was a good idea to not delete everything when a branch was deleted. What disingenuous behavior.... – jww Sep 08 '16 at 16:13
  • How I arrived at this kind of situation: I had origin cloned at two machines, `computer1` and `computer2`. On `computer2`, I added `computer1` as a remote, and then did `git push computer2 --mirror`. Now on `computer1`, I have branches of the `remotes/computer1/branchname` form even though (quite understandably) it is not tracking itself as a remote. – Ansa211 Dec 17 '18 at 14:45

13 Answers13

825

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

Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42
Kent Fredric
  • 56,416
  • 14
  • 107
  • 150
  • 5
    I don't want to delete the branch on the remote side. I think there is a subtle difference. – cmcginty Jul 02 '09 at 02:51
  • 2
    er, the question is effectively asking "how do I delete a remote branch". Thats what those paths are. – Kent Fredric Jul 02 '09 at 02:52
  • 1
    I will rephrase the subject if that makes it more clear what I'm asking, but the command show exactly what my problem is. – cmcginty Jul 02 '09 at 02:57
  • 44
    `git gc` isn't needed here, but `git remote prune` makes me feel safer than manually deleting things with `git branch -rd`, since git is verifying which remote branches are done. – Mike Seplowitz Nov 30 '11 at 23:38
  • 4
    this didn't work for me - the 'git branch -rd' worked fine, however. – dsummersl May 17 '12 at 03:28
  • @orange80 is right. His answer below is the correct one. This is yet another case of the wrong answer being marked correct just because it works. Please update the correct answer. – Martin Jun 18 '12 at 21:49
  • I did the `git branch -rd origin/whatever` and Git Bash did report `Deleted remote branch origin/whatever`, however, when I look at the Project in GitHub the branch is still shown. Why is it still shown on GitHub and how can I get off there ? NOTE: `git branch -a` doesn't show the branch anymore that I deleted (but it's still shown in GitHub) – Someone Somewhere Sep 04 '12 at 16:20
  • I just did a `git fetch origin` and the remote branch is back. It was happily announced with "[new branch]" – Someone Somewhere Sep 04 '12 at 18:41
  • The accepted solution here worked for me (`git push origin --delete `) http://stackoverflow.com/a/2003515/550471 – Someone Somewhere Sep 04 '12 at 19:20
  • For context, the need to call 'git gc' was because they'd inherently done something that left their git tree in a somewhat broken state, ie: the configuration no longer pointed to the remotes, but the remotes were still stored on disk , and 'gc' is just fixing this up. But in the general case, the push and -rd techniques are preferred. – Kent Fredric Oct 25 '12 at 05:55
  • @SomeoneSomewhere , you were confusing "tracking branches" ( mirrors of branches on the remote side ) with "remote branches" ( the branch that there is a mirror of ). Hence why you need a `git push` of some kind to tell the far side "this branch can die now". But `git branch -rd `, despite the name 'remote', only behaves locally. – Kent Fredric Jan 29 '14 at 09:08
  • Lol... yet another piece of Git advice found on Stack Overflow that simply does not work.... – jww Jun 10 '16 at 06:35
  • Its almost as if git changed in the last 7 years, or that there might be more than one way for this problem to happen... ( Sorry ) – Kent Fredric Jun 14 '16 at 13:58
  • For the disappeared branch issue, see the solution below that uses 'git remote add ...' followed by 'git remote rm ...' which was the only thing that worked for me. – taranaki Dec 25 '16 at 20:03
  • I which I read the last part of `usually you will never need to call git gc` before. It's the first thing you are suggesting. Nevertheless, it's a good answer. Thanks. – JAponte Nov 22 '17 at 16:24
767

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
Pawan Maheshwari
  • 15,088
  • 1
  • 48
  • 50
  • 10
    This is what I was looking for as well - the question is describing a scenario more complicated than the common one. – rjmunro Dec 06 '12 at 12:09
  • 27
    I'm looking for a way to delete local branches where the corresponding remote has been deleted, but this doesnt work for me. Any idea why? – jackocnr May 31 '13 at 00:13
  • 13
    This removes the branches listed in remote/origin but doesn't delete local tracking branches, which is just as important. – BlueRaja - Danny Pflughoeft Apr 22 '14 at 14:57
  • @Cupcake Since you didn't roll back my first edit (which was fixing the incorrect information about Git 1.8.5+), you have now made this incorrect. My second edit was fixing what I put in that was incorrect, which is now there again (with your rollback). Please go ahead and roll back one more edit to have the original. Thanks. – ferventcoder Jul 11 '14 at 13:38
  • @ferventcoder I double checked your last edit, and rolled back to it. The OP can rollback again if he doesn't like it. Thanks. –  Jul 11 '14 at 16:15
  • @ferventcoder meh, just do whatever you want, I'm probably just being too anal `;)` –  Jul 29 '14 at 23:06
  • I'd suggest `git fetch -p --all` if you have several remotes – eddygeek May 06 '21 at 16:52
344
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
Alan Haggai Alavi
  • 72,802
  • 19
  • 102
  • 127
  • 7
    This helped me to remove a git-svn remote ghost branch. – Nick Sep 09 '11 at 14:03
  • 13
    `git branch -rd removed_remote/branch` worked for me, while the `git gc --prune=now` was worthless. – Romain Champourlier Nov 28 '11 at 08:14
  • 2
    I've been able to use `git prune` without any issues, but my co-worker who forked our main repo **COULD ONLY ** use the `git branch -rd public/master`-style solution to clean his environment up. – Abel Dec 01 '11 at 17:31
  • 3
    `git branch -rd public/master` was what I was missing. I had `heroku/master` and `herkou/master` ... lol woops – Aaron Aug 02 '12 at 19:59
  • @rchampourlier Not 100% worthless - if your git repo is big, removing the unused branches can free a lot of disk space in some situations. – peterh Mar 01 '17 at 10:04
  • `git branch -rd origin/` worked in my case. – Vagelis Prokopiou Apr 30 '17 at 12:29
  • @peterh he means it didn't work, hence worthless, not that the operation is not worth doing. – Baldrickk May 01 '18 at 10:42
  • After all the variations I tried, the only thing that worked was `git branch -rd origin/branch/name`. Thanks – dgo Jan 10 '19 at 16:31
179

All you need to do is

$ git branch -rd origin/whatever 

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

jpswain
  • 14,642
  • 8
  • 58
  • 63
  • 1
    how do you "push" that delete to github? – Thufir Aug 09 '12 at 04:40
  • 18
    @Thufir That's not what this question was about. This question was specifcally for situations when you have an invalid remote reference in the local repo, but that branch no longer exists on the remote server. The answer to your question is $ git push origin :whatever – jpswain Aug 09 '12 at 07:09
  • Yes, if something happens on the remote repo where a branch is deleted, but you still have reference to that remote branch on your local machine, then you would need to do what I put in my original answer to clean it up. – jpswain Aug 09 '12 at 07:54
  • 6
    If you have a big cleanup job (lots of dangling remotes), you could just delete all remote branches with something like `git branch -rd $(git branch -r)` then reestablish the valid ones by doing a fetch. – Brent Bradburn Sep 24 '12 at 03:03
  • My situation was that I had used `git config -e` to rename my remote. I renamed the remote `mine` to `origin`. Then, this was the solution that worked best for me: `git branch -rd $(git branch -r | grep 'mine/')` – Steven Lu Mar 01 '15 at 21:21
  • 1
    This is the actual and only solution to the OP question – arainone May 07 '18 at 22:30
76

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

yojimbo87
  • 65,684
  • 25
  • 123
  • 131
tongueroo
  • 1,167
  • 10
  • 5
  • 6
    @Casey $ git gc # does like a defragment for the git files to speed up the respository $ git remote prune origin # will clean up the delete the stale remote branches that show up with "git branch -r | grep origin". Thats what the question is asking I believe. So, the commands are totally different. – tongueroo Jul 14 '10 at 20:53
33

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
chris
  • 831
  • 1
  • 8
  • 13
  • Worked for me. Regular git branch -d was not working,returned error that branch does not exists, because I removed origin called "original", that was created by mistake, directly in .git/config file. – micrub Jun 18 '13 at 11:14
  • This is the method I had to use to remove branches that were inadvertently omitted from my .git/config (which had to be rebuilt due to an unrelated corruption). It's too bad this answer was so far down the chain that I didn't notice it until at long last I found the solution and went to add it to the accepted answer! – taranaki Dec 25 '16 at 20:02
  • This is what I needed after using svn2git. There were a lot of remotes/svn/* branches. Had to create a bogus 'svn' remote first. – Sam Mar 15 '17 at 10:53
10

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

Community
  • 1
  • 1
ThorSummoner
  • 16,657
  • 15
  • 135
  • 147
  • wow, this helped me. My colleague tried, restart VS, Restart his computer nothing worked, he deleted his local repo and pull everything to get rid of this :) – Esen May 20 '15 at 20:01
  • 2
    Today I leanred that this packed-refs file is created as part of `git gc`, when it packs your commits into a highly-compressed archive it also moved the references into a single plain text file, possibly for optimization purposes; I hope future versions of git can `git br -D ...` packed refs. – ThorSummoner May 20 '15 at 20:57
5
git push origin --delete <branch name>

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

kip2
  • 6,473
  • 4
  • 55
  • 72
3

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.

Keinstein
  • 347
  • 2
  • 11
3

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.

clacke
  • 7,688
  • 6
  • 46
  • 48
2

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

JGlass
  • 1,427
  • 2
  • 12
  • 26
0

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
>
Glenn Slayden
  • 17,543
  • 3
  • 114
  • 108
0

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>'

Marvin Thobejane
  • 2,010
  • 1
  • 19
  • 13