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

13

76

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.

Competitive answered 8/10, 2015 at 10:15 Comment(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?Mendie
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 logPaigepaik
I
97

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)

Intellectualism answered 5/7, 2016 at 5:2 Comment(10)
.git folder was hidden in my case, see for yourself.Exo
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)Heidelberg
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.Humid
Thanks a lot, this worked well. Someday I wil look into the internals of git closelyAdrial
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\xxxYokum
Just wanted to add a thanks to this! This most definitely worked!Condyloid
Thank you, this worked - even though I had to read the comments for clarificationGalle
This worked for me for the most part as well, except git status returned fatal: .git/index: index file smaller than expected. Simply rebuilding the index (git read-tree master) fixes that last problem.Pavlov
Thanks it work for me too, Any reason why it suddenly happened?Koah
Aaaand just like that I now call myself a git "expert" in interviews. "I fix corrupted repos by hand ;). No, literally." Thanks!Moue
M
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 <[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.

Moynahan answered 26/5, 2017 at 19:50 Comment(0)
G
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>.

Guerdon answered 10/2, 2018 at 2:3 Comment(2)
Can't thank you much. Almost had a mini-heart attack!Saul
I wish I understood why this worked, but it did. (I didn't try to rename my branch, my pc crashed during a pull)Sizzle
H
6

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!

Halakah answered 12/10, 2018 at 21:34 Comment(0)
V
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.

Vertical answered 28/6, 2017 at 12:6 Comment(1)
Yeah correct but in this case , the only user I am; having all the rights !Competitive
P
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
Paramo answered 5/7, 2016 at 5:9 Comment(1)
It seemed to only treat mine as if "all files were new" after doing this :\Paulsen
K
2

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

Knot answered 28/1, 2018 at 7:58 Comment(0)
C
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.

Catarrhine answered 26/6, 2020 at 10:54 Comment(0)
S
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.

Something answered 16/10, 2020 at 9:38 Comment(0)
V
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.

Varini answered 17/6, 2021 at 9:33 Comment(0)
D
0

I just did

git checkout some_branch_name
git checkout branch_i_needed

and luckily it started working.

Detruncate answered 13/4, 2022 at 11:14 Comment(0)
C
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

Cinnamon answered 24/8, 2022 at 9:38 Comment(0)
R
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.

Ruthy answered 19/7, 2023 at 20:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.