git 中的分支损坏,致命:您当前的分支似乎已损坏

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

Broken branch in git, fatal: your current branch appears to be broken

gitgit-branchfatal-errorgit-logblue-screen-of-death

提问by eirenaios

Here is my case:

这是我的情况:

  • I was working on one branch.
  • Pushed new commits to the remote.
  • Switched back to the master branch.
  • 我在一个分支上工作。
  • 将新提交推送到远程。
  • 切换回主分支。

But suddenly after typing git checkout mastercommand my computer encountered blue screen of deathand an unexpected force shut down happened. After starting back my computer I have checked the status of my current branch and as a result I got each and every file as marked new files.

但是突然在输入git checkout master命令后,我的电脑遇到了蓝屏死机,并且发生了意外的强制关机。重新启动计算机后,我检查了当前分支的状态,结果我将每个文件都标记为新文件。

Now, I am stuck at this point and after git logcommand I am getting error

现在,我被困在这一点上,git log命令后我收到错误

$ git log
fatal: your current branch appears to be broken

How to solve this problem and recover my branch?.

如何解决这个问题并恢复我的分支?

I am working with windows 7 and git bash latest version

我正在使用 Windows 7 和 git bash 最新版本

Edit: I don't want to delete this branch.

编辑:我不想删除这个分支。

回答by David Zhang

I meet similar issue on Windows 7. In my case,the current branch file (refer by ./git/HEAD) under \.git\refs\headswas broken.

我在 Windows 7 上遇到了类似的问题。在我的情况下,当前分支文件(引用由./git/HEAD\.git\refs\heads已损坏。

I found the hash code of broken current branch on .git\logs\refs\headswith same branch name.

我在.git\logs\refs\heads相同的分支名称上找到了损坏的当前分支的哈希码。

And I fixed the issue by opening that file (.git\logs\refs\heads\xxx) via notepad and copy the 4th number (the hash code) to (.git\refs\heads\xxx)

.git\logs\refs\heads\xxx通过记事本打开该文件 ( ) 并将第 4 个数字(哈希码)复制到 ( .git\refs\heads\xxx)

回答by Ilker Cat

The files in .git\refs\heads directory are your branches. Check those files. They should contain only a single commit objects SHA-1 hash. This hash is your latest commits SHA-1 key and your HEAD at the same time.

.git\refs\heads 目录中的文件是你的分支。检查那些文件。它们应该只包含一个提交对象 SHA-1 哈希。这个哈希是你最近一次提交的 SHA-1 密钥和你的 HEAD。

Copy the SHA-1 key and type

复制 SHA-1 密钥并键入

$ git cat-file -t 5917fefd485f655ab369d4e9eeda3c157c03f514
commit

$ git cat-file -p 5917fefd485f655ab369d4e9eeda3c157c03f514
tree b75cab3c54b780075b312be3e878b389a2baf904
parent 8235189aa22169295243d295fb1cc2ff2f8f7cd5
author Ilker Cat <[email protected]> 1495136738 +0200
committer Ilker Cat <[email protected]> 1495136738 +0200

The second output is what a commit object basically contains. Try to check whether the commit object in your master branch under .git\refs\heads\master and its tree and parent SHA-1 keys are not corrupted.

第二个输出是提交对象基本上包含的内容。尝试检查 .git\refs\heads\master 下主分支中的提交对象及其树和父 SHA-1 密钥是否未损坏。

Even some apostrophes inside your master branches file will lead into a "broken branch". It must contain only the lastest commits object SHA-1 hash and nothing else.

甚至主分支文件中的一些撇号也会导致“损坏的分支”。它必须只包含最新的提交对象 SHA-1 哈希值,不包含其他任何内容。

回答by wafs

You might encounter this error if you try to rename a branch into a namespaced (or folder) branch.

如果您尝试将分支重命名为命名空间(或文件夹)分支,则可能会遇到此错误。

If it happens, go to the directories .git/logs/refs/heads/<name>and .git/refs/heads/<name>, and you'll see your branch is now a folder with a file inside it.

如果发生这种情况,请转到目录.git/logs/refs/heads/<name>.git/refs/heads/<name>,您将看到您的分支现在是一个文件夹,其中包含一个文件。

In both folders, move the file out to the folder's level, checkout that branch, delete the now empty folders and now you should be able to perform git checkout -b <name>/<subname>without error, or git branch -M <name>/<subname>.

在这两个文件夹中,将文件移出文件夹级别,签出该分支,删除现在为空的文件夹,现在您应该能够正确执行 git checkout -b <name>/<subname>,或者git branch -M <name>/<subname>.

回答by Amit P

Some time it may also occur due to file permission problems, check if you have appropriate permissions on all the files under the repository.

有的时候也可能是因为文件权限问题,检查一下你是否对仓库下的所有文件都有合适的权限。

回答by Maksym Semenykhin

You branch name now probably contains some special characters or something like that.

你的分支名称现在可能包含一些特殊字符或类似的东西。

You should go to the root-directory of your check-out (where the .git/directory is) and

您应该转到结帐的根目录(.git/目录所在的位置)和

  1. List item edit .git/packed-refs; if you see a line with your branch name then delete it
  2. look in .git/refs/heads fora file named after your branch; if you see one, delete it
  1. 列表项编辑.git/packed-refs;如果您看到一行包含您的分支名称,则将其删除
  2. 查看.git/refs/heads for以您的分支命名的文件;如果你看到一个,删除它

回答by Hike Nalbandyan

Had the same problem. Removing .git\refs\heads\ fixed the problem for me.

有同样的问题。删除 .git\refs\heads\ 为我解决了这个问题。

回答by reverie_ss

I solved it by cloning the repo to a new folder and then replacing the changed files. Doesn't seems to be a good solution, but it's safe!

我通过将 repo 克隆到一个新文件夹然后替换更改的文件来解决它。似乎不是一个好的解决方案,但它是安全的!