相关文章推荐
失落的皮蛋  ·  git ...·  2 周前    · 
大气的苹果  ·  解决ORA-02069: ...·  4 月前    · 
奔跑的小虾米  ·  Qt QTextEdit 设置 ...·  6 月前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to commit the changes to my repository but I receive the error below:

git -c diff.mnemonicprefix=false -c core.quotepath=false commit -q -F C:\Users\Contronym\AppData\Local\Temp\bkdweixb.mnu
fatal: cannot lock ref 'HEAD': unable to resolve reference HEAD: Invalid argument
Completed with errors, see above.

I'm using bitbucket and SourceTree.

What's the reason for this commit failing? I was able to commit just fine the last 3 commits over the past week. Then, all of a sudden, I receive this error.

I ran git gc and these are the results:

$ git gc
error: bad ref for HEAD
error: bad ref for HEAD
error: inflate: data stream error (unknown compression method)
fatal: loose object 53b65bd9b4fec7f6a7b0b3313c68199a18804327 (stored in .git/objects/53/b65bd9b4fec7f6a7b0b3313c68199a18804327) is corrupt
error: failed to run repack

I checked the directory .git/objects/53/b65bd9b4fec7f6a7b0b3313c68199a18804327 but that doesn't exist. There's two other files there, but b65bd9b4fec7f6a7b0b3313c68199a18804327 doesn't exist.

I had the same problem and the only solution that I found was to navigate to the head like so:

.git/refs/heads/branch_name 

And I deleted the head file. Then I went to the console and I used the command:

git reset

Then all the files were unstaged so add them and commit them afterwards.

Step 1.

  • go to .git\logs\refs\heads and open the Document named as YOUR_BRANCH, now copy the ID numbers in front of your user name and email
  • Step 2.

  • go to .git\refs\heads and open the document named as YOUR_BRANCH delete the line and paste the ID in.
  • Commit the changes (if necessary)

  • Run the following command for merging the remote repository branch

    git pull
    

    If it gives related to refusing to merge unrelated histories, run the following command in the terminal:

    git pull origin master --allow-unrelated-histories
    

    I had the same issue after calling git commands with the root user inside the working copy. So the owner and owner group of various files under .git/ were changed to "root".

    When I switched back to my user account, git could not handle this files anymore, because of the lacking permissions.

    It worked again, after resetting the permissions with

    sudo chown -R [MY_USER]:[MY_GROUP] .git
    

    When I run into this I just git clone the project into a new file directory and pull the heads folder from located at .git\refs\heads and replace the original heads file in the directory that your having the problem. Then just delete the new clone you created (since it obviously doesn't have the updates your trying to push).

    You want a quick, 100% working solution ?

    I tried a few of the ones suggested here (thanks Guys !), but to no fixing the case for me.

  • rename your directory with _old.
  • 2.git clone your project again, into a brand new directory.

    Then , move your files one by one / resume your work.

    That's garanteed to work ! - even if playing with git commands looked funnier in the beginning. :)

    Note: the spacetime between the "git clone" command and your files will lay on your files only, so beware if you're in the case of several commits and no push done : the commits list (and logic) will be lost.

    I had the same problem, today. I found a very easy workaround to this problem but has a few trade-offs. I am still not sure what caused it in the first place. I came across a lot of solutions, and unfortunately, nothing really worked for me.

    I could not initialize a GitHub repo during this error, which actually helped me find a solution. Apparently, just deleting the .git directory solves a lot of issues. Just when I deleted that directory, I was able to initialize a repo. You shall find the .git folder in your workspace. .git(dir)/config(file) . the config file may be broken and could be the source of the issue. (I am not sure of the cause, any explanations in laymen would be much appreciated)

    just when I deleted the folder, all the errors vanished. I was also able to commit from GitHub desktop (which was throwing me the error, previously, the same error while committing directly from the IDE)

    The only downside to this would be, all your staged changes might be lost, that is only it. And you may have to initialize a new repo because giving the same name would throw error(if the same repo already exists)

    So, apart from that, you shall be good to go. You can commit changes too now.

    But, all the changes in files will be tracked, no issues. Just the commit message will be the one you initially commit after initializing a repo, to dodge the error, For me, no commands worked, but this one worked fine. So, if you too are having trouble, follow this one , if commands dont work... – Dwaipayan Chakroborty May 14, 2021 at 20:52 TL;DR => delete the .git folder/initilaize a repo with a new name, that doesn't already exist in your Github – Dwaipayan Chakroborty May 14, 2021 at 20:53 Extremely dangerous if you don't know what you're doing -- this will remove all branches from your repository and potentially cause you to lose track of a large number of commits (which will later be deleted if you don't fix it soon). – Soren Bjornstad Oct 18, 2020 at 20:55 You should warn people about what happens when deleting the .git folder, no? I mean what if there's a long history on the repository? You know it will all be lost, right? – Scratte Sep 24, 2021 at 19:41

    check branch name
    git show-ref --head

    Firstly, just checked the error, here you will find which reference is broken

    Step 1: just remove this mentioned reference
    rm .git/refs/heads/branch_name

    Step 2: git fetch
    Step 3: git pull

    Will work 100% sure....

    Then I removed file /my_project_directory/./git/refs/heads/master and then I can use this command:

    git reset --hard <my_hash_of_last_commit_on_remote_branch>
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •