git 致命:无法将 HEAD 解析为有效的参考

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

fatal: Failed to resolve HEAD as a valid ref

gitatlassian-sourcetreesourcetree

提问by idurvesh

I am getting fatal: Failed to resolve HEAD as a valid ref. whenever I try to commit.

我变得致命:无法将 HEAD 解析为有效的参考。每当我尝试提交时。

I have tried

我试过了

echo ref: refs/heads/master >.git/HEAD

but its not working

但它不工作

Also tried

也试过

git commit

its not working either from below sources

它无法从以下来源工作

Git 'fatal: No such ref: HEAD'git tag: fatal: Failed to resolve 'HEAD' as a valid ref

Git'fatal:没有这样的引用:HEAD' git tag:fatal:无法将'HEAD'解析为有效的引用

Please help in..All my commit history is also gone

请帮忙......我所有的提交历史也消失了

回答by Pravesh Khatri

I have also encountered with same issue... and I have resolved by following

我也遇到过同样的问题......我已经通过以下方式解决了

  1. Clone your same project in some other folder
  2. Copied the .git folder of the Cloned project
  3. At last replace the original .git folder of the project with the folder you have copied.
  1. 在其他文件夹中克隆相同的项目
  2. 复制克隆项目的.git文件夹
  3. 最后用您复制的文件夹替换项目的原始 .git 文件夹。

Edit by Sudip Bhandari

苏迪普·班达里编辑

Why does this work?

为什么这样做?

Broken git repositories are often the result of file system corruption due to abrupt power failure or other abnormalities. Since git stores all the information inside .git folder and those are corrupted git no longer recognizes the repository.

损坏的 git 存储库通常是由于突然断电或其他异常导致文件系统损坏的结果。由于 git 将所有信息存储在 .git 文件夹中并且那些已损坏的 git 不再识别存储库。

Caveats

注意事项

Whatever is store in your previous .git folder will be gone. Configurational things like reference names for your remotes (if you have more than one) etc will have to be set up again.

之前 .git 文件夹中存储的任何内容都将消失。诸如遥控器的参考名称(如果您有多个)等配置内容必须重新设置。

回答by Michael Schmid

In my case, I ended up with two branches with the same nameafter a branch renaming. By removing one of them in .git/refs/headsit all went back to normal.

就我而言,在分支重命名后,我最终得到了两个同名的分支。通过删除.git/refs/heads 中的其中之一,一切都恢复了正常。

回答by Oscar Bravo

I had this problem after a Blue Screen of Deathincident - so it was similar to what Sudip Bhandari said above.

我在蓝屏死机事件后遇到了这个问题- 所以它类似于上面 Sudip Bhandari 所说的。

  • I looked in .git/refs/heads/<mybranch>and found that the entry was corrupted (illegible). This file is supposed to contain the full commit-id of the HEAD branch.
  • I cloned a new repository and copied .git/refs/heads/<mybranch>from the new checkout over the corrupted one (I guess I could've just repaired it by pasting in a recent commit-id from stash or whatever).
  • back in the original repository, I did git rm -r --cached .and git reset --hardto clean up and then found all was back to normal again.
  • 我查看.git/refs/heads/<mybranch>并发现条目已损坏(难以辨认)。该文件应该包含 HEAD 分支的完整提交 ID。
  • 我克隆了一个新的存储库,并.git/refs/heads/<mybranch>从新的结帐复制到损坏的存储库(我想我可以通过粘贴最近的来自 stash 或其他东西的提交 ID 来修复它)。
  • 回到原来的存储库,我做了git rm -r --cached .git reset --hard清理,然后发现一切又恢复正常了。

回答by hrvoj3e

My problem was with

我的问题是

git init
git add .

Tried

试过

git reset
  fatal: Failed to resolve 'HEAD' as a valid ref.
git reset --hard
  fatal: Failed to resolve 'HEAD' as a valid ref.

Solved with

解决了

git rm -r --cached .

Enviroment

环境

git version 1.7.5.4

Ubuntu 11.10

git 版本 1.7.5.4

Ubuntu 11.10

回答by Chiranjhivi Ghimire

The best solution for this problem:

此问题的最佳解决方案:

'fatal: Failed to resolve HEAD as a valid ref'

'致命:无法将 HEAD 解析为有效的引用'

  1. Open the project on Eclipse IDE

  2. Goto project Team menu and select Switch to, finally you can switch your current detached branch to your choice now.

  1. 在 Eclipse IDE 上打开项目

  2. 转到项目团队菜单并选择切换到,最后您现在可以将当前分离的分支切换到您的选择。

As attached screenshot below:

如下附截图:

enter image description here

在此处输入图片说明