73

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.

2
  • What the content of the file .git/HEAD? Does it point to a valid branch name? Does the branch name then point to a valid commit hash?
    – knittl
    Nov 3, 2015 at 6:37
  • This issue happened for me when the computer was closed in the middle of a git push and then had to restart upon opening it again. After the restart I get the same error when doing git log
    – Olov
    Dec 29, 2022 at 12:15

13 Answers 13

91

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)

9
  • .git folder was hidden in my case, see for yourself. Oct 17, 2017 at 14:47
  • 2
    Thanks! I found it also useful to check out the commit hash to see if it is the state I want to restore using git checkout <hash> (before copying it to git/refs/heads/xxx)
    – ph_0
    Dec 17, 2019 at 8:13
  • 3
    Thanks, it worked. I copied the last commit hash form .git\logs\refs\heads\xxx to .git\refs\heads\xxx. There were two columns of hash codes in the log, and the last entry of the second column was my last commit. Nov 27, 2020 at 10:12
  • Thanks a lot, this worked well. Someday I wil look into the internals of git closely
    – devsaw
    Feb 11, 2021 at 16:45
  • 2
    Thanks man, it worked. Actually I'm just copy the second hash code from the last row of .git\logs\refs\heads\xxx to .git\refs\heads\xxx Aug 26, 2021 at 3:28
14

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.

7

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

2
  • 1
    Can't thank you much. Almost had a mini-heart attack! Feb 19, 2018 at 6:25
  • 3
    I wish I understood why this worked, but it did. (I didn't try to rename my branch, my pc crashed during a pull)
    – mushcraft
    Sep 4, 2018 at 15:13
5

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!

4

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

1
  • Yeah correct but in this case , the only user I am; having all the rights ! Jun 29, 2017 at 5:10
2

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
1
  • It seemed to only treat mine as if "all files were new" after doing this :\
    – rogerdpack
    Dec 19, 2017 at 15:21
2

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

2

In may case after using Notepad to open the file named after my branch name found at .git\logs\refs\heads\<MY-CORRUPTED-BRANCH> was empty. So I deleted it.

and run to get latest commit

git reflog

4404dd7 HEAD@{0}: commit: update README

and then I run

git reset --hard 4404dd7 

HEAD is now at 4404dd7 update README

and branch was back. Note

This may diverge your branch. So you may need to fix them later.

4404dd7 was my latest commit in that branch and I don't know if this is proper solution or not but it was what worked or me.

0

I had the same problem. I just deleted all the files inside .git/refs/heads, then I tried to edit one of my files in order for git to accept commit. Then, when I pushed my files, I got this error:

10:47 Push rejected Push has been cancelled, because there were conflicts during update. Check that conflicts were resolved correctly, and invoke push again.

However, I solved it by accepting the rebase option when merging the files, giving no conflicts.

0

Okay for this problem, I found out that, If you go back and clone your repo, what you have to do is simply copy .git folder from your new repo and replace your .git folder in your old repo.

The rest is history: Run git add . and then git commit and push.

0

I just did

git checkout some_branch_name
git checkout branch_i_needed

and luckily it started working.

0

this happened to me too when i closed IDE while commit is still running, when i opened .git/refs/heads/branch-name the file was empty, so as suggested above i copied the last commit hash from .git/logs/refs/heads/branch-name and pasted it in the empty file this solved the problem

0

if your git branch and git log are blank

I had an issue where git log and git branch just showed nothing. I think it was from a botched git commit -m " where I tried to do a multi-line commit without opening the editor.

Oddly enough, my fix was to close the terminal and re-open it, and then git log and git branch magically worked again. It must have been something botched in my bash environment, not in the git repo itself.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.