fatal: bad object xxx

Git

Git Problem Overview


I tried reverting to a previous git commit with:

git revert xxx

I'm now receiving this error as a response:

fatal: bad object xxx

What am I doing wrong? How do I fix this?

Git Solutions


Solution 1 - Git

I don't know the exact reason why that happens. For me, it's because I forget to pull the entire repository to my local. I have 2 or more path, and each path pulls from different branch

/path/branch_a/ -> pulled from branch A
/path/branch_b/ -> pulled from branch B

on branch A, I made a few modification, and commit as usual. I want that commit (for example the commit ID is abcdef123) appears on branch B, so I use

$ cd /path/branch_b/
$ git branch
  master
  branch_a
* branch_b 

$ git cherry-pick abcdef123

This gives me that kind of error. So I need to pull the entire repository before getting that commit

$ git pull
remote: Counting objects: 257, done.
remote: Compressing objects: 100% (58/58), done.
remote: Total 216 (delta 187), reused 186 (delta 158)
Receiving objects: 100% (216/216), 53.13 KiB | 43 KiB/s, done.
Resolving deltas: 100% (187/187), completed with 38 local objects.
From github.com:username/my_repo
   abcdef3..80c0d68  branch_a    -> origin/branch_a
Already up-to-date.

$ git cherry-pick abcdef123 
[branch_b ccddeef] Some commit message
1 file changed, 1 insertion(+), 1 deletion(-)

Solution 2 - Git

[Edit 1, 19 Nov 2016] While this would sometimes indicate repository corruption, it turns out to occur on Windows when some command—usually, another Git in another task—is holding internal files open and locked. In this case, terminating the other task should fix it. Original answer is below.

[Edit 2, 6 May 2020] Suppose xxx above resembles, e.g., b34789c0b0d3b137f0bb516b417bd8d75e0cb305 (a raw hash ID). If you got this from cut-and-paste, be sure that it's for this repository (it's easy to grab a hash ID from some other repository without realizing it, especially if you have multiple windows open). If you retyped it yourself, be sure there aren't any typos. If this involves submodules, make sure the submodule is up to date. See the comments on the question and some of the answers, including this one.


bad object with some hexadecimal number tends to mean that a tag has an invalid reference number in it, but can also occur for a few other strange cases. For instance, if I do a:

$ git tag foo
$ vi .git/refs/tags/foo

and change the last character (in this case from 6 to 5) and write that out:

$ git log foo
fatal: bad object foo

What exactly is the xxx here and where did it come from?

Solution 3 - Git

In my case I was cherry-picking from another branch which I didn't pull, but I tried to copy commit's IDs from GH (I haven't got this on my local where I was cherry-picking).

Hope it helps ;-D

Solution 4 - Git

I just ran into the same error (bad object [hash]) while attempting to merge from a branch my client didn't know about. (Similar to PrzeoR's case, but instead of needing a pull I needed to fetch)

In my case I needed to run git fetch to re-sync my client to the state of the server. Posting this here in case anyone reaches this thread the same way I did and could benefit from this insight.

git pull
git cherry-pick [hash]
fatal: bad object [hash]
git fetch
remote: Counting objects: 8, done. (etc.)
From github.com:repo/branch
 * [new branch] branchname

git cherry-pick [hash]
[success]

Solution 5 - Git

git fetch --all

The git fetch command downloads commits, files, and refs from a remote repository into your local repository.

Solution 6 - Git

> git pull

OR

> git fetch origin

Reason: If the commit id which you are trying to cherry pick is not available in your local git, then there is a possible of this error.

Doing a git pull will fix this. If this hasn't been fixed, ask the person who has shared the commit id to push the change to origin and do a git pull

Solution 7 - Git

Objects that don't exist in the repository give that error message

E.g.:

 git init
 touch a
 git add a
 git commit -m 0
 # This object is not in the repository.
 git show 1111111111111111111111111111111111111111

As for what causes the problem, it is hard to say without a minimal reproducible example.

Submodule woes have given me that error once.

Solution 8 - Git

you need to do git fetch to get the latest commits in sync with local

git fetch

then do

git revert

Solution 9 - Git

I got this error while trying to cherry-pick a commit whose hash I had copied from GitHub. This commit was on a branch that I hadn't pulled, just like PrzeoR.

Unlike PrzeoR a git fetch didn't help at first because the branch had been deleted on GitHub. Fortunately I was able to find the corresponding (closed) Pull Request and to restore the branch on GitHub.

Solution 10 - Git

I'm not sure how i got this error, This is the error i got.

fatal: bad object refs/remotes/origin/{branchname}
fatal: failed to run repack

Tried pruning the git repo via git gc --aggressive --prune=now. It did not help.

This branch was stale and it was not important for me so i deleted the branch folder

rm -rf .git/refs/remotes/origin/{branchname}

ran git gc

It did object Enumeration and clean successfully.

Solution 11 - Git

This issue can arise when there's an outdated or corrupted branch stored locally.

Deleting the file .git/refs/remotes/origin/xxx (after making a backup!) then fetching a fresh copy from the server worked for me.

More detail at GitHub.

Solution 12 - Git

I ran into the same error when trying to cherry-pick (on A) the commit of another branch ( B). Issue was stupid, simply forgot to git push the commit (B).

Solution 13 - Git

The reason I ran into it was simple. I was switching back and forth between the main repository and a submodule. I tried to do a diff between one hash (in the main repository) and another that I copied from SourceTree, thinking it was an easy to get the old HEAD (I had regressed one revision to track down a regression). The old HEAD hash I grabbed was that of a submodule, and git diff was put out with me for feeding it garbage. That's how I ended up here, and that's when I realized it was operator error. If your hash is from a different repository git will scold you with this message. However, if you do feed it garbage, would it not be better to report "XXX not a revision in this repository"? It is every bit as general an error message as "bad object" and quite a bit less likely to send someone to stack overflow for answers. I wonder if the folks in the git community would accept that pull request...

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
QuestionJames A. AndersonView Question on Stackoverflow
Solution 1 - GitPrabowo MurtiView Answer on Stackoverflow
Solution 2 - GittorekView Answer on Stackoverflow
Solution 3 - GitPrzeoRView Answer on Stackoverflow
Solution 4 - GitjorfusView Answer on Stackoverflow
Solution 5 - GitSharanView Answer on Stackoverflow
Solution 6 - GitunknownerrorView Answer on Stackoverflow
Solution 7 - GitCiro Santilli Путлер Капут 六四事View Answer on Stackoverflow
Solution 8 - GitpatrickView Answer on Stackoverflow
Solution 9 - GitquinaoView Answer on Stackoverflow
Solution 10 - GitsmsivaprakaashView Answer on Stackoverflow
Solution 11 - GitMartin SmithView Answer on Stackoverflow
Solution 12 - GitMatth CView Answer on Stackoverflow
Solution 13 - GitcycollinsView Answer on Stackoverflow