Git'致命:没有这样的参考:HEAD'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4848607/
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
Git 'fatal: No such ref: HEAD'
提问by szaman
A weird thing happend to my git repository. When I try to commit something in tortoisegit window I receive all files from project. I cannot revert them, when I pull from server I receive fatal: No such ref: HEAD
and fatal: Cannot lock the ref 'HEAD'
. All my local branches are missing. Is there any way to resolve the problem?
我的 git 存储库发生了一件奇怪的事情。当我尝试在 tortoisegit 窗口中提交某些内容时,我收到了来自项目的所有文件。我不能回复他们,当我从服务器获取我收到fatal: No such ref: HEAD
和fatal: Cannot lock the ref 'HEAD'
。我所有的本地分支机构都不见了。有什么办法可以解决问题吗?
This is not first commit or something. This thing happend suddenly.
这不是第一次提交什么的。这件事发生得很突然。
EDIT:
编辑:
git branch -a
says: Failed to resolve HEAD as a valid ref
git branch -a
说: Failed to resolve HEAD as a valid ref
git status
prints all project files marked as new file.
git status
打印所有标记为新文件的项目文件。
I changed repository folder name for a while, and when I changed it back things were not correct.
我改变了一段时间的存储库文件夹名称,当我把它改回来时,事情就不正确了。
回答by CB Bailey
You've lost your HEAD
so you'll need to recreate it. The simplest thing to do is this.
你已经失去了你的HEAD
所以你需要重新创造它。最简单的事情就是这样。
echo ref: refs/heads/master >.git/HEAD
Now you should be able to run other git commands and see where you're at.
现在您应该能够运行其他 git 命令并查看您所在的位置。
(Although, in theory, you could attempt to do git symbolic-ref HEAD refs/heads/master
newer git versions don't recognize a .git
as a git repository unless it already contains a HEAD
so this won't work to create a new one.)
(尽管,理论上,您可以尝试使git symbolic-ref HEAD refs/heads/master
较新的 git 版本不将 a 识别.git
为 git 存储库,除非它已经包含 a,HEAD
因此这无法创建新版本。)
回答by Simon Richter
HEAD
is usually a reference to a particular branch; in your case, it seems the branch pointers have gone missing, so the HEAD reference cannot be resolved.
HEAD
通常是对特定分支的引用;在您的情况下,分支指针似乎丢失了,因此无法解析 HEAD 引用。
You can use git fsck --lost-found
to scan the object cache for unreachable objects; specifically, you are interested in commits, which can then be found below .git/lost-found/commit/
; these are pointers to your branches, all you need to do then is find out which is which, and create new references using git branch
.
您可以使用git fsck --lost-found
扫描对象缓存以查找无法访问的对象;具体来说,您对提交感兴趣,然后可以在下面找到.git/lost-found/commit/
;这些是指向您的分支的指针,然后您需要做的就是找出哪个是哪个,并使用git branch
.
回答by szaman
I think this answer maybe helpful for someone. I resolved this problem nearly. First what I did was, like Charles Bailey wrote, use
我认为这个答案可能对某人有帮助。我几乎解决了这个问题。首先我所做的是,就像查尔斯贝利写的那样,使用
echo ref: refs/heads/master >.git/HEAD
Then my branch changed to master. I commited changes and was able to switch to my main branch. The problem was that I wasn`t able to use any of my local branches. Especially I wanted to work on branch 812. So I found last commit to branch 812 (create message when commit is very helpful ;)) and switched to it. Next I created branch 812 based on the one I switched to. Unfortunately some files were missing. Luckily I had them on the broken repo which I copied before 'echo'
然后我的分支变成了master。我提交了更改并能够切换到我的主分支。问题是我无法使用任何本地分支机构。特别是我想在分支 812 上工作。所以我找到了对分支 812 的最后一次提交(在提交时创建消息非常有用;))并切换到它。接下来,我根据切换到的分支创建了分支 812。不幸的是,一些文件丢失了。幸运的是我把它们放在了我在“回声”之前复制的损坏的仓库中
回答by Hans-Christoph Steiner
For me the problem was that on Mac OS X either the 'uchg' or 'uappnd' flag was set, locking some git files regardless of the perms. I reset the chflags like this and it solved it for me:
对我来说,问题是在 Mac OS X 上设置了 'uchg' 或 'uappnd' 标志,无论权限如何,都会锁定一些 git 文件。我像这样重置了 chflags,它为我解决了这个问题:
sudo chflags -R 0000 .