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

前端 未结 9 1282
时光说笑
时光说笑 2021-02-01 12:57

Here is my case:

  • I was working on one branch.
  • Pushed new commits to the remote.
  • Switched back to the master branch.

But suddenly

相关标签:
9条回答
  • 2021-02-01 13:30

    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 讨论(0)
  • 2021-02-01 13:32

    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!

    0 讨论(0)
  • 2021-02-01 13:33

    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 讨论(0)
  • 2021-02-01 13:38

    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
    0 讨论(0)
  • 2021-02-01 13:43

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

    0 讨论(0)
  • 2021-02-01 13:49

    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.

    0 讨论(0)
提交回复
热议问题