如何从 Git 中删除无效的远程分支引用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1072171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:40:26  来源:igfitidea点击:

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

gitbranchremote-branch

提问by cmcginty

In my current repo I have the following output:

在我当前的 repo 中,我有以下输出:

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

I want to delete remotes/public/masterfrom the branch list:

我想remotes/public/master从分支列表中删除:

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

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

此外,输出git remote很奇怪,因为它没有列出public

$ git remote show 
origin

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

如何从分支列表中删除“远程/公共/主”?

Update, tried the git pushcommand:

更新,尝试git push命令:

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

回答by Kent Fredric

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.

修剪

删除 <name> 下的所有陈旧跟踪分支。这些陈旧的分支已经从 <name> 引用的远程存储库中删除,但在“remotes/<name>”中仍然可以在本地使用。

使用 --dry-run 选项,报告将修剪哪些分支,但实际上不修剪它们。

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.

R M

删除名为 <name> 的遥控器。删除远程的所有远程跟踪分支和配置设置。

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 gcwas required to fix up the left behind cruft.

我怀疑 OP 做了一些让他们的树处于不一致状态的事情,导致它的行为有点奇怪,并且git gc需要修复留下的残骸。

Usuallygit branch -rd origin/badbranchis sufficient for nuking a local tracking branch , or git push origin :badbranchfor nuking a remote branch, and usuallyyou will neverneed to call git gc

通常git branch -rd origin/badbranch足以破坏本地跟踪分支或git push origin :badbranch破坏远程分支,通常永远不需要调用git gc

回答by Pawan Maheshwari

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 1.8.5+,则可以自动设置

git config fetch.prune true

or

或者

git config --global fetch.prune true

回答by Alan Haggai Alavi

git push public :master

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

这将删除名为masterKent Fredric 指出的远程分支。

To list remote-tracking branches:

列出远程跟踪分支:

git branch -r

To delete a remote-tracking branch:

要删除远程跟踪分支:

git branch -rd public/master

回答by jpswain

All you need to do is

你需要做的就是

$ git branch -rd origin/whatever 

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

就这么简单。没有理由在这里调用 gc。

回答by tongueroo

git gc --prune=nowis not what you want.

git gc --prune=now不是你想要的。

git remote prune public

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

git remote prune origin# 如果那是远程源

is what you want

是你想要的

回答by chris

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

回答by ThorSummoner

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 -Ddoesn't know how to touch (At least in ver 1.7.9.5).

就我而言,我试图删除保存在.git/packed-refs. 您可以编辑此纯文本文件并从中删除git br -D不知道如何触摸的条目(至少在 1.7.9.5 版中)。

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

我在这里找到了这个解决方案:https: //stackoverflow.com/a/11050880/1695680

回答by clacke

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.

我不知道git branch -rd,所以我为自己解决这样的问题的方法是将我的回购视为远程回购并进行远程删除。git push . :refs/remotes/public/master. 如果其他方法不起作用,并且您有一些奇怪的参考要摆脱,那么这种原始方法是万无一失的。它为您提供移除(或创建!)任何类型引用的精确精度。

回答by JGlass

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"

只是稍微相关,但在与我们相同的情况下仍然可能有帮助 - 我们为远程存储库使用网络文件共享。上周一切正常,本周我们收到错误消息“远程源没有为分支 refs/heads/master 通告 Ref。此 Ref 可能不存在于远程或可能被权限设置隐藏”

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.

但我们相信没有采取任何措施来破坏事物。NFS 会做快照,所以我查看了每个“以前的版本”,并且在三天前看到,存储库的大小(以 MB 为单位)从 282MB 增加到 33MB,现在大约有 1,403 个新文件和 300 个文件夹。我询问了我的同事,那天有人试图推动 - 然后取消了它。

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.

我使用 NFS“恢复”功能将其恢复到该日期之前,现在一切正常。我以前确实尝试过修剪,似乎没有帮助。也许更严厉的清理会奏效。

Hope this might help someone else one day!

希望有一天这可以帮助别人!

Jay