git 推送时Git无法解析引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23749886/
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 18:06:05  来源:igfitidea点击:

Git unable to resolve references when pushing

gitgithub

提问by cidthecoatrack

I am having an issue with Git. For unknown reasons, my master branch has somehow gotten corrupted. I have a local commit that I want to push up, but when I push, I get this:

我在使用 Git 时遇到问题。不知什么原因,我的主分支不知何故损坏了。我有一个想要推送的本地提交,但是当我推送时,我得到了这个:

git push origin master
error: unable to resolve reference refs/remotes/origin/master: No such file or directory
error: cannot lock the ref 'refs/remotes/origin/master'.
Everything up-to-date

I have seen the issue on other boards, but usually referring to pulls and not pushes. Nevertheless, I have tried their solutions, but to no avail:

我在其他板上看到过这个问题,但通常指的是拉而不是推。尽管如此,我已经尝试了他们的解决方案,但无济于事:

  1. Tried amending to my current commit and pushing
  2. cleaned my git repository with git gc --prune=now
  3. Tried rm .git/refs/removes/origin/master
  1. 尝试修改我当前的提交和推送
  2. 清理了我的 git 存储库 git gc --prune=now
  3. 试过 rm .git/refs/removes/origin/master

None have solved my issues. Any thoughts or ideas?

没有人解决了我的问题。有什么想法或想法吗?

回答by jthill

(a worthwhile tl;dr from @NeTeInStEiN:

(一个有价值的 tl;来自 @NeTeInStEiN 的博士:

The nuclear option is rm -rf .git/refs/remotes/origin, and that's what it took here)

核选项是rm -rf .git/refs/remotes/origin,这就是它在这里采取的措施)

edit:I've run across part of this behavior in one of my own repos, I can get git to reproduce the f-e-r failure without the rm .git/refs/remotes/origin/masterhack:

编辑:我在我自己的一个存储库中遇到了这种行为的一部分,我可以让 git 在没有rm .git/refs/remotes/origin/master黑客的情况下重现 fer 故障:

  • clone a repo
  • delete the origin's primary branch (the one its HEAD's attached to)
  • run git fetch --prunein the clone
  • 克隆一个 repo
  • 删除源的主分​​支(它HEAD的附加分支)
  • git fetch --prune在克隆中运行

The fetch will produce a dangling-ref warning, and git for-each-refwill fail with a familiar message:

fetch 将产生一个 dangling-ref 警告,并且git for-each-ref会失败并显示一条熟悉的消息:

~/sandbox/20/buddy$ git fetch --prune
From /home/jthill/sandbox/20/source/.
 x [deleted]         (none)     -> origin/master
   (refs/remotes/origin/HEAD has become dangling)
~/sandbox/20/buddy$ git f-e-r
fatal: missing object 0000000000000000000000000000000000000000 for refs/remotes/origin/HEAD

but that doesn't break the push, I've tried with every setting of push.default, nor does it break git update-ref -d.

但这并没有打破推动,我已经尝试了 的每一个设置push.default,也没有打破git update-ref -d

However, googling the push message did get me this:

但是,谷歌搜索推送消息确实让我知道:

I had just rebooted from a BSOD the other day [...] then git push. And that's when I got a complaint about “Unable to resolve reference refs/remotes/origin/master…”. [...] So, I opened up the master file and it was full of spaces! Well, that's no good. In order to fix it, I did this: [your rm, and then git fetch]

前几天我刚刚从 BSOD 重新启动 [...] 然后 git push。就在那时,我收到了关于“无法解析引用 refs/remotes/origin/master...”的投诉。[...] 所以,我打开了主文件,里面全是空格!嗯,那不好。为了修复它,我这样做了:[你的rm,然后git fetch]



See comments above for the blow-by-blow, tl;dr is, because these were remote refs, which git fetchcompletely refreshes, and because the damage was such that for-each-refand git update-reffailed to work at all, the nuclear option rm -rf refs/remotes/origin; git fetchwas guaranteed to restore the remote properly.

请参阅上面的评论以了解详细信息,tl; dr 是,因为这些是远程参考,git fetch完全刷新,并且由于损坏严重for-each-refgit update-ref根本无法正常工作,因此核选项可以rm -rf refs/remotes/origin; git fetch保证正确恢复远程.

In other circumstances, if there'd been no easy way to restore the damaged refs or for curiosity, find .git/refs/remotes/origin -type fto check for locks or using reflogs (those files are in .git/logs) to recover content would have helped but it wasn't necessary here. I think I missed a bet by not doing the findfirst, *.lockfiles from a kill -9ed earlier command look likely here, but I suspected an ambiguous ref and f-e-r is my first step for those.

在其他情况下,如果没有简单的方法来恢复损坏的引用或出于好奇,find .git/refs/remotes/origin -type f检查锁或使用引用日志(那些文件在 中.git/logs)来恢复内容会有所帮助,但在这里没有必要。我想我因为没有做第find一个而错过了一个赌注,*.lock来自kill -9ed 早期命令的文件在这里看起来很可能,但我怀疑一个模棱两可的 ref 和 fer 是我的第一步。



回答by VonC

Note: with Git 2.5 (July 2015), git for-each-refwill be a bit more precise when it fails on a "missing object".

注意:使用 Git 2.5(2015 年 7 月),git for-each-ref当它在“丢失的对象”上失败时会更加精确。

See commit 501cf47, commit f551707(03 Jun 2015), and commit 8afc493, commit c3e23dc(02 Jun 2015) by Michael Haggerty (mhagger).
(Merged by Junio C Hamano -- gitster--in commit 9d71c5f, 24 Jun 2015)

请参阅Michael Haggerty ( ) 的提交 501cf47提交 f551707(2015 年 6 月 3 日)和提交 8afc493提交 c3e23dc(2015 年 6 月 2 日(由Junio C Hamano合并-- --提交 9d71c5f 中,2015 年 6 月 24 日)mhagger
gitster

for-each-ref: report broken references correctly

If there is a loose reference file with invalid contents, "git for-each-ref" incorrectly reports the problem as being a missing object with name NULL_SHA1:

for-each-ref: 正确报告损坏的引用

如果存在内容无效的松散参考文件,“ git for-each-ref”会错误地将问题报告为缺少名称为 NULL_SHA1 的对象:

$ echo '12345678' >.git/refs/heads/nonsense
$ git for-each-ref
fatal: missing object 0000000000000000000000000000000000000000 for refs/heads/nonsense

With an explicit "--format" string, it can even report that the reference validly points at NULL_SHA1:

使用显式的 " --format" 字符串,它甚至可以报告引用有效地指向 NULL_SHA1:

$ git for-each-ref --format='%(objectname) %(refname)'
0000000000000000000000000000000000000000 refs/heads/nonsense
$ echo $?
0

NULL_SHA1is used to indicate an "invalid object name" throughout our code (and the code of other git implementations), so it is vastly more likely that an on-disk reference was set to this value due to a software bug than that NULL_SHA1is the legitimate SHA-1 of an actual object.
Therefore, if a loose reference has the value NULL_SHA1, consider it to be broken.

NULL_SHA1用于指示“ invalid object name”在我们的代码(和其他的git实现的代码),因此它是大大更有可能的是磁盘上的基准设定为这个值,由于软件错误比NULL_SHA1是合法的SHA-1一个实际的对象。
因此,如果松散引用具有值NULL_SHA1,则认为它已损坏。

回答by Richard Mascarini

rm .git/refs/remotes/origin/master

rm .git/refs/remotes/origin/master

run git fetch --prune

运行 git fetch --prune

回答by Prateek Agarwal

Remove the following directory from .git folder -> .git/refs/remotes/origin

从 .git 文件夹中删除以下目录 -> .git/refs/remotes/origin

This will remove all the remote branches and you need to fetch them again using -> git fetch. This should fix the issue.

这将删除所有远程分支,您需要使用 -> git fetch 再次获取它们。这应该可以解决问题。

回答by Ankit Chauhan

  1. just go to this dir. in your project and delete the origin folder from your project:
  1. 只需转到此目录。在您的项目中并从您的项目中删除 origin 文件夹:


.git/refs/remotes/origin
  1. now just run this commannd:
  1. 现在只需运行此命令:


git fetch --prune

or you may fetch all your branches by using this git fetch --all.

或者您可以使用 this 获取所有分支 git fetch --all

now pushor pullyour code with,
this works for me,

现在pushpull你的代码,
这对我有用,

回答by Jae Blaez

I am probably a day late and a dollar short, but I recently experienced the same error message while setting up my first repository for school. I have looked at the answers above and none of them solved my problem. What did solve my problem was : sudo git pushand entered admin password...

我可能迟到了一天,缺了一美元,但我最近在为学校设置第一个存储库时遇到了同样的错误消息。我已经查看了上面的答案,但没有一个解决了我的问题。确实解决了我的问题的是:sudo git push并输入了管理员密码...