Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 983
Suresh Karia Avatar asked Oct 08 '15 10:10

Suresh Karia


People also ask

How do I terminate a branch?

If you want to delete a branch completely, you can just delete it in all your repositories (typically local and remote). git will then clean it up next time it garbage collects.


2 Answers

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

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

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)

like image 76
David Zhang Avatar answered Sep 18 '22 17:09

David Zhang


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

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.

like image 41
Ilker Cat Avatar answered Sep 17 '22 17:09

Ilker Cat