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

发布于 2024-07-26 03:26:06 字数 663 浏览 13 评论 0原文

在我当前的存储库中,我有以下输出:

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

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

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

另外, git remote 的输出很奇怪,因为它未列出public

$ git remote show 
origin

如何从分支列表中删除'remotes/public/master'?

更新,尝试了git push命令:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

葬花如无物 2024-08-02 03:26:07

我在这里尝试了一切,但没有任何效果。 如果所有其他方法都失败,请从文本文件“.git/packed-refs”中删除有问题的分支,然后从“.git\refs\remotes\origin”中删除有问题的分支

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

—━☆沉默づ 2024-08-02 03:26:06

您可能需要清理:

git gc --prune=now

或者您可能需要修剪:

git remote prune public

修剪

删除下的所有过时跟踪分支。 这些过时分支已从引用的远程存储库中删除,但仍可在“remotes/”中本地使用。

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

然而,看来这些应该早点清理干净

git remote rm public 

rm

删除名为的远程。 所有远程跟踪分支和远程配置设置
已被删除。

因此,可能是您手动编辑了配置文件,但这种情况没有发生,或者您遇到了权限问题。

也许再次运行一下,看看会发生什么。


建议上下文

如果您查看修订日志,您会注意到我建议了更多“正确”的技术,这些技术对于不管出于什么原因不想在他们的存储库上工作。

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

通常 gitbranch -rd origin/badbranch 足以破坏本地跟踪分支,或者 git push origin :badbranch 足以破坏远程分支,并且通常你将永远需要调用git gc

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

你没皮卡萌 2024-08-02 03:26:06

您需要做的就是

git fetch -p

它将删除所有远程删除的本地分支。

如果你使用的是 git 1.8.5+,你可以自动设置

git config fetch.prune true

或者

git config --global fetch.prune true

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
空心↖ 2024-08-02 03:26:06
git push public :master

正如 Kent Fredric 所指出的,这将删除名为 master 的远程分支。

列出远程跟踪分支:

git branch -r

删除远程跟踪分支:

git branch -rd public/master
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
倒带 2024-08-02 03:26:06

您所需要做的就是

$ git branch -rd origin/whatever 

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

All you need to do is

$ git branch -rd origin/whatever 

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

提笔落墨 2024-08-02 03:26:06

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

git remote prune public

远程源

git Remote prune origin # 如果这就是您想要的

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

最笨的告白 2024-08-02 03:26:06

当裁判拥挤时,接受的答案对我不起作用。 然而,这确实:

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

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
明明#如月 2024-08-02 03:26:06

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

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

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

︶ ̄淡然 2024-08-02 03:26:06
git push origin --delete <branch name>

引用自:http://www.gitguys.com/topics/adding -并删除远程分支/

git push origin --delete <branch name>

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

苍白女子 2024-08-02 03:26:06

我不知道 gitbranch-rd,所以我自己解决此类问题的方法是将我的存储库视为远程存储库并进行远程删除。 git 推送 . :refs/remotes/public/master。 如果其他方法不起作用,并且您想要删除一些奇怪的参考,那么这种原始方法肯定会成功。 它为您提供了删除(或创建!)任何类型参考的精确度。

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.

〃安静 2024-08-02 03:26:06

我有类似的问题。 所有答案都没有帮助。 就我而言,我永久显示了两个已删除的远程存储库。

我的最后一个想法是手动删除所有对它的引用。

假设存储库称为“Repo”。 我这样做了:

find .git -name Repo 

所以,我从 .git 文件夹中删除了相应的文件和目录(这个文件夹可以在你的 Rails 应用程序或计算机上找到 https://stackoverflow.com/a/19538763/6638513)。

然后我做了:

grep Repo -r .git

这找到了一些文本文件,我在其中删除了相应的行。
现在,一切似乎都很好。

通常,你应该把这项工作留给 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.

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

但我们相信什么也没有发生。做了腐败的事情。 NFS 会进行快照,因此我查看了每个“以前的版本”,发现三天前,存储库的大小(以 MB 为单位)已从 282MB 变为 33MB,并且现在存在大约 1,403 个新文件和 300 个文件夹。 我询问了我的同事,其中一位同事那天尝试过推送 - 然后取消了。

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

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

杰伊

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

烟酉 2024-08-02 03:26:06

不知道我是如何陷入困境的,但我的错误消息略有不同:

<前>> git 远程
> git分支
警告:忽略损坏的引用 refs/remotes/origin/HEAD
* 主要
>

但我能够通过稍微重新解释此页面上其他地方的修复来修复它。 我的意思是我用关键字 HEAD 替换了 / 中的分支名称。 人们提到的其他建议都不起作用(gcprune 等),所以我已经没有想法了,希望得到最好的结果。 不管怎样,它就像一个魅力:

<前>> git 远程
> git 分支 -rd origin/HEAD
删除了远程跟踪分支 origin/HEAD(原为 refs/remotes/origin/main)。

> git 分支
* main
>

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
>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文