git status shows fatal: bad object HEAD

GitGithub

Git Problem Overview


I have a problem with git on Windows Azure. Git status shows - "fatal: bad object HEAD" and I tried running:

 git fsck --full
 git gc

But git fsck shows:

broken link from  commit 739df633f185ce5d1ab7eb97d619b28e7d81185a
              to    tree 2a6d4876d135c1fa7cbe1348c62570006e895fc5
broken link from  commit 9c7eae5ffed34dbfac977e515dee675626b59f93
              to    tree 400132d215ab9aced883a9971e648b82624b2032
broken link from  commit 9c7eae5ffed34dbfac977e515dee675626b59f93
              to  commit 4a49af0a0cb64a0a0415734b11772d6df18561fb
broken link from  commit bc3072f30e71c616a8196089e19a67a2c9c0a5ad
              to    tree 0aa813c183487d0a1b1f7ae81501ca7a1168283f
broken link from  commit d3bb4f8545e91ec8ace15ad31a3147d92a1d4242
              to    tree 4682108accd8e72fe68858232386dffe60f9f02d
broken link from  commit 6b34795c4b54286301bcdc0ed254a04c132cb2ad
              to    tree 5c57dd3222d11924dba841d3cae517bdc9220601
broken link from  commit d70172d855391b93bc1c5eeb9b4226df525dfc6e
              to  commit 390c8cbd527c8e707c51e25142e54421f4dd3948
broken link from  commit cc05e8d2e3733693ebb67d697ae4b65e51fea79a
              to  commit 32f081f8b901425fd1e8898478f0551970bee0f5
broken link from    tree 6a75ed6d0311d800078e77f43d427d128372d5bc
              to    blob 4a064d610c0e7207967d59934c8bc5f491f26dae
broken link from    tree 6a75ed6d0311d800078e77f43d427d128372d5bc
              to    tree 5c06ec964dcbade49287d0f36efe1f7b60f446e3
broken link from    tree b4855fa6734b5652a93a9b799eafe47fad0d13a0
              to    blob 3e1fb421613dc9066cbf9c95eddc61619a9f8eed
broken link from    tree b4855fa6734b5652a93a9b799eafe47fad0d13a0
              to    tree 556a50048d42346c283c94b78ea278ba1d57d251
broken link from    tree 289c03409370c4ca7c12266ce2822a2976bd032b
              to    blob 3abf3c48ada45f63404dcf4d675ddfdadcfa83c6
broken link from    tree 289c03409370c4ca7c12266ce2822a2976bd032b
              to    blob 3fa569892003b468ed1301426dd6d96d9644be3c
broken link from    tree 289c03409370c4ca7c12266ce2822a2976bd032b
              to    blob 0a9a54a51e84f3bc34122dbce1146d895fcbe22c
broken link from    tree 289c03409370c4ca7c12266ce2822a2976bd032b
              to    blob 3fa48873564361b4d95830803ae77f79eeafaf5b

git branch shows - *master

Git Solutions


Solution 1 - Git

try this; it worked for me (Warning: this destroys work that exists only in your local repo):

rm -rf .git

You can use mv instead of rm if you don't want to lose your stashed commits

then copy .git from other clone

cp <pathofotherrepository>/.git . -r

then do

git init

this should solve your problem , ALL THE BEST

Solution 2 - Git

Your repository is corrupt. That means data is lost that cannot be recovered by git itself. If you have another clone of this repository, you can recover the objects from there, or make a new clone.

fatal: bad object HEAD means the branch referenced from HEAD is pointing to a bad commit object, which can mean it's missing or corrupt.

From the output of git fsck, you can see there are a few tree, blob and commit objects missing.

Note that using git itself is not enough to keep data safe. You still need to back it up in cases of corruption.

Solution 3 - Git

This happened because by mistake I removed some core file of GIT. Try this its worked for me.

re-initialize git

git init

fetch data from remote

git fetch

Now check all your changes and git status by

git status

Solution 4 - Git

Running

git remote set-head origin --auto

followed by

git gc

Solution 5 - Git

In my case the error came out of nowhere, but didn't let me push to the remote branch.

git fetch origin

And that solved it.

I agree this may not solve the issue for everyone, but before trying a more complex approach give it a shot at this one, nothing to loose.

Solution 6 - Git

Your repository is broken. But you can probably fix it AND keep your edits:

  1. Back up first: cp your_repository your_repositry_bak
  2. Clone the broken repository (still works): git clone your_repository your_repository_clone
  3. Replace the broken .git folder with the one from the clone: rm -rf your_repository/.git && cp your_repository_clone/.git your_repository/ -r
  4. Delete clone & backup (if everything is fine): rm -r your_repository_*

Solution 7 - Git

I had a similar problem and what worked for me was to make a new clone from my original repository

Solution 8 - Git

I managed to fix a similar problem to this when some of git's files were corrupted:

https://stackoverflow.com/a/30871926/1737957

In my answer on that question, look for the part where I had the same error message as here:

fatal: bad object HEAD.

You could try following what I did from that point on. Make sure to back up the whole folder first.

Of course, your repository might be corrupted in a completely different way, and what I did won't solve your problem. But it might give you some ideas! Git internals seem like magic, but it's really just a bunch of files which can be edited, moved, deleted the same as any others. Once you have a good idea of what they do and how they fit together you have a good chance of success.

Solution 9 - Git

I solved this by doing git fetch. My error was because I moved my file from my main storage to my secondary storage on windows 10.

Solution 10 - Git

Make a copy of your git dir in your local host and run git init there once again. Push the project to a brand new reprository.

Solution 11 - Git

I solved this by copying the branch data (with the errors) to my apple laptop local git folder.

Somehow in the terminal and when running: git status, tells me more specific data where the error occurs. If you look under the errors, hopefully you see a list of folders with error. In my case GIT showed the folder which was responsible for the error. Deleting that folder and commiting the branche, I succeeded. git status was working again the other devices updating by git pull; everything working again on every machine.

Hopefully this will work for you also.

Solution 12 - Git

This is unlikely to be the source of your problem - but if you happen to be working in .NET you'll end up with a bunch of obj/ folders. Sometimes it is helpful to delete all of these obj/ folders in order to resolve a pesky build issue.

I received the same fatal: bad object HEAD on my current branch (master) and was unable to run git status or to checkout any other branch (I always got an error refs/remote/[branch] does not point to a valid object).

If you want to delete all of your obj folders, don't get lazy and allow .git/objects into the mix. That folder is where all of the actual contents of your git commits go.

After being close to giving up I decided to look at which files were in my recycle bin, and there it was. Restored the file and my local repository was like new.

Solution 13 - Git

This happened to me on a old simple project without branches. I made a lot of changes and when I was done, I couldn't commit. Nothing above worked so I ended up with:

  1. Copied all the code with my latest changes to a Backup-folder.
  2. Git clone to download the latest working code along with the working .git folder.
  3. Copied my code with latest changes from the backup and replaced the cloned code (not the .git folder).
  4. git add and commit works again and I have all my recent changes.

Solution 14 - Git

In my case I tried cloning from the remote repository to a new TEST local repository. Then used the new .git folder to replace the "corrupt" .git folder. Ran git status and could see the latest differences.

enter image description here

BEWARE: Check differences between your local folder and repo using git add . -v. To undo changes before commit use git reset. You may need to bring "deleted" files from the TEST local repository cloned in the beginning of this operation.

Solution 15 - Git

The solution by @jan-glx should always work. I am adding here step 3., which is needed in case you were working on a branch other than master/main:

  1. Back up first: cp your_repository your_repositry_bak
  2. Clone the broken repository: git clone your_repository your_repository_clone
  3. (if needed) Switch to your working branch in the cloned repo cd your_repository_clone && git checkout your_working_branch
  4. Replace the broken .git folder with the one from the clone: rm -rf your_repository/.git && cp your_repository_clone/.git your_repository/ -r
  5. (if everything is fine) Delete clone & backup: rm -r your_repository_*

Solution 16 - Git

I came across this issue when I used submodules

The file structure

-- Python.Hot
-----.git/
-----Python.OpticalTweezers/
-----Python.OpticalTweezersGUI/
-----.gitmodules

Question

when I checked the log of any submodule, I came across this issue.The waring message is "Could not open log. libgit reports:bad object xxxxxxx"

This issue doesn't exist in the several versions at the very beginning.

snapshot

enter image description here

enter image description here

Solution 17 - Git

Problem

I had the problem where I had local changes preventing me from checking out the main branch, git status would give me fatal: bad object HEAD, and git stash would give me BUG: diff-lib.c:607: run_diff_index must be passed exactly one tree.

Solution

I found that I probably was on a pruned branch (I believe). Force checking out the main branch and then deleting that branch did the trick for me:

  1. git checkout main -f
  2. git branch -D <name_of_branch>

Note: I did do some juggling with a backup of my .git-folder, re-cloning the original repository somewhere else, and overwriting the .git folder temporarily to try to restore git functionality, but in the end the commands above was what saved me with my git history intact!

Solution 18 - Git

In my case, this happened because of a bad commit object.

For example,

ubuntu@server41:~/proj31$ git status -s | grep M
fatal: bad object HEAD

I had to copy the .git directory from the newly cloned in another directory(same repo) to the git repo that had this problem. (see below)

ubuntu@server41:~/proj31$ cd ..
ubuntu@server41:~$ mkdir newr
ubuntu@server41:~$ cd newr
ubuntu@server41:~/newr$ git clone https://github.com/account434/proj31.git
ubuntu@server41:~/newr$ cd
ubuntu@server41:~/newr$ cd proj31

# copy .git directory from newly clone(same repo) to the one which has this problem.

ubuntu@server41:~/proj31$ cp -r ../newr/proj31/.git .

And it worked.

Solution 19 - Git

I solved this by renaming the branch in the file .git/refs/remotes/origin/HEAD.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondotnetrocksView Question on Stackoverflow
Solution 1 - GitganeshView Answer on Stackoverflow
Solution 2 - GitIkkeView Answer on Stackoverflow
Solution 3 - GitMahesh GarejaView Answer on Stackoverflow
Solution 4 - GitBillView Answer on Stackoverflow
Solution 5 - GitFelipe PereiraView Answer on Stackoverflow
Solution 6 - Gitjan-glxView Answer on Stackoverflow
Solution 7 - GitserupView Answer on Stackoverflow
Solution 8 - GitjwgView Answer on Stackoverflow
Solution 9 - GitRoyer AdamesView Answer on Stackoverflow
Solution 10 - Gitulas korpeView Answer on Stackoverflow
Solution 11 - GitRicardoView Answer on Stackoverflow
Solution 12 - GitwdonahoeView Answer on Stackoverflow
Solution 13 - GitAndreasView Answer on Stackoverflow
Solution 14 - GitDavidView Answer on Stackoverflow
Solution 15 - GitLolloView Answer on Stackoverflow
Solution 16 - Git笑先生View Answer on Stackoverflow
Solution 17 - GitmlunoeView Answer on Stackoverflow
Solution 18 - GitSuperNovaView Answer on Stackoverflow
Solution 19 - GitcemsbrView Answer on Stackoverflow