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

时间:2021-04-09 16:27:32

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:

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

$ 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的输出很奇怪,因为它没有列出public:

$ git remote show 
origin

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

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

Update, tried the git push command:

更新,尝试了git push命令:

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

11 个解决方案

#1


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>".

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

With --dry-run option, report what branches will be pruned, but do no actually prune them.

使用--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.

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

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.

我怀疑OP已经做了一些让他们的树处于不一致状态的东西,导致它表现得有点奇怪,并且需要git gc来修复左后方残骸。

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

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

#2


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

#3


git push public :master

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

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

To list remote-tracking branches:

列出远程跟踪分支:

git branch -r

To delete a remote-tracking branch:

要删除远程跟踪分支:

git branch -rd public/master

#4


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。

#5


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

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

git remote prune public

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

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

is what you want

是你想要的

#6


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

#7


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).

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

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

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

#8


git push origin --delete <branch name>

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

参考自:http://www.gitguys.com/topics/adding-and-removing-remote-branches/

#9


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.

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

Lets say the repository is called “Repo”. I did:

假设存储库名为“Repo”。我做了:

find .git -name Repo

and deleted the corresponding files and directories

并删除了相应的文件和目录

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.

通常你应该把这份工作留给git。

#10


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,所以我自己解决这个问题的方法是将我的repo视为远程仓库并进行远程删除。 git push。 :裁判/遥控器/公共/主。如果其他方法不起作用,并且你想要摆脱一些奇怪的参考,这种原始的方式是肯定的。它为您提供了删除(或创建!)任何类型参考的精确精度。

#11


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"

只是略微相关,但仍然可能在我们遇到的相同情况下有用 - 我们使用远程存储库的网络文件共享。上周事情正在发生,本周我们收到错误“远程来源没有为分支引用/头/主人做广告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

#1


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>".

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

With --dry-run option, report what branches will be pruned, but do no actually prune them.

使用--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.

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

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.

我怀疑OP已经做了一些让他们的树处于不一致状态的东西,导致它表现得有点奇怪,并且需要git gc来修复左后方残骸。

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

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

#2


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

#3


git push public :master

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

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

To list remote-tracking branches:

列出远程跟踪分支:

git branch -r

To delete a remote-tracking branch:

要删除远程跟踪分支:

git branch -rd public/master

#4


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。

#5


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

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

git remote prune public

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

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

is what you want

是你想要的

#6


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

#7


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).

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

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

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

#8


git push origin --delete <branch name>

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

参考自:http://www.gitguys.com/topics/adding-and-removing-remote-branches/

#9


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.

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

Lets say the repository is called “Repo”. I did:

假设存储库名为“Repo”。我做了:

find .git -name Repo

and deleted the corresponding files and directories

并删除了相应的文件和目录

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.

通常你应该把这份工作留给git。

#10


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,所以我自己解决这个问题的方法是将我的repo视为远程仓库并进行远程删除。 git push。 :裁判/遥控器/公共/主。如果其他方法不起作用,并且你想要摆脱一些奇怪的参考,这种原始的方式是肯定的。它为您提供了删除(或创建!)任何类型参考的精确精度。

#11


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"

只是略微相关,但仍然可能在我们遇到的相同情况下有用 - 我们使用远程存储库的网络文件共享。上周事情正在发生,本周我们收到错误“远程来源没有为分支引用/头/主人做广告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