Broken branch in git, fatal: your current branch a

2020-05-19 04:09发布

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 master command my computer encountered blue screen of death and 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.

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

$ 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

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

7条回答
Lonely孤独者°
2楼-- · 2020-05-19 04:37

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.

Copy the SHA-1 key and type

$ git cat-file -t 5917fefd485f655ab369d4e9eeda3c157c03f514
commit

$ git cat-file -p 5917fefd485f655ab369d4e9eeda3c157c03f514
tree b75cab3c54b780075b312be3e878b389a2baf904
parent 8235189aa22169295243d295fb1cc2ff2f8f7cd5
author Ilker Cat <ilker.cat@blabla.com> 1495136738 +0200
committer Ilker Cat <ilker.cat@blabal.com> 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.

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.

查看更多
Evening l夕情丶
3楼-- · 2020-05-19 04:37

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

查看更多
放荡不羁爱自由
4楼-- · 2020-05-19 04:44

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.

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

查看更多
孤傲高冷的网名
5楼-- · 2020-05-19 04:48

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

查看更多
ゆ 、 Hurt°
6楼-- · 2020-05-19 04:58

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

  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 for a file named after your branch; if you see one, delete it
查看更多
淡お忘
7楼-- · 2020-05-19 05:00

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!

查看更多
登录 后发表回答