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?
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(-)
[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?
git fetch to have the remote commits in local as well.
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]
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
git fetch --all
The git fetch command downloads commits, files, and refs from a remote repository into your local repository.
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.
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.
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
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.
git rev-list).
Commented
Feb 28, 2017 at 11:09
Fetches the shallowest commit history possible. Do not use if your build depends on full repository history.
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.
you need to do git fetch to get the latest commits in sync with local
git fetch
then do
git revert
As @jxramos wrote in a comment, this could be due to using a shallow clone in which case the hash you are refering might correspond to a commit which was not fetched by the shallow clone.
We got this message when migrating tests to a Gitlab CI which by default uses a shallow clone with a depth of 20.
git rev-list --missing=<missing OID> can also trigger a "fatal: bad object <oid>" error
git rev-list --missing=print --allow-missing-tips allows Git to handle missing objects more gracefully by printing them out instead of failing outright.
That can help identifying a missing commit.
With Git 2.45 (Q2 2024), batch 5, "git rev-list --missing=print"(man) has learned to optionally take --allow-missing-tips, which allows the objects at the starting points to be missing.
See commit a4324ba (28 Feb 2024), and commit 7b644c8, commit 686101f, commit eaf07b7, commit 3ff56af (14 Feb 2024) by Christian Couder (chriscool).
(Merged by Junio C Hamano -- gitster -- in commit 76d1cd8, 07 Mar 2024)
rev-list: allow missing tips with --missing=[print|allow*]Helped-by: Linus Arver
Signed-off-by: Christian Couder
In 9830926 ("
rev-list: add commit object support in--missingoption", 2023-10-27, Git v2.43.0-rc1 -- merge) we fixed the--missingoption ingit rev-list(man) so that it works with with missing commits, not just blobs/trees.Unfortunately, such a command would still fail with a "fatal: bad object " if it is passed a missing commit, blob or tree as an argument (before the rev walking even begins).
When such a command is used to find the dependencies of some objects, for example the dependencies of quarantined objects (see the "QUARANTINE ENVIRONMENT" section in the
git-receive-pack(1)documentation), it would be better if the command would instead consider such missing objects, especially commits, in the same way as other missing objects.If, for example
--missing=printis used, it would be nice for some use cases if the missing tips passed as arguments were reported in the same way as other missing objects instead of the command just failing.We could introduce a new option to make it work like this, but most users are likely to prefer the command to have this behavior as the default one.
Introducing a new option would require another dumb loop to look for that option early, which isn't nice.
rev-list-options now includes in its man page:
If some tips passed to the traversal are missing, they will be considered as missing too, and the traversal will ignore them. In case we cannot get their Object ID though, an error will be raised.
revision: fix --missing=[print|allow*] for annotated tagsSigned-off-by: Christian Couder
That fix still doesn't work when an argument passed to the command is an annotated tag pointing to a missing commit though.
In that casegit rev-list --missing=...(man) still errors out with a "fatal: bad object <oid>" error where<oid>is the object ID of the missing commit.Let's fix this issue, and also, while at it, let's add tests not just for annotated tags but also for regular tags and branches.
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).
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...
Had similar issue, finally had to remove bad object sha entry from pack-refs file
Location : ./git/pack-refs
in my case this was caused by git filter-repo which by default replaces commit hashes in commit messages. then parsing hashes from these patched messages gives the error fatal: bad object xxx. fix: git filter-repo --preserve-commit-hashes
Well, I have the stupidest of reasons, which is being on the wrong repo. So, probably double check that as well...
Got this error today, it seemed like my repo was corrupted as a lot of git commands were giving me errors (pull, fetch, gc, etc).
Git log was working, so I was able to determine that I was only one commit behind. So I decided to "nuke" the .git directory.
cp -R repo repo.bakgit remote -v to get the url of the remote repository..git/ directory with git clone --bare <REMOTE URL> .git/ and removing the line bare = true in .git/config.git add . fixed everything.This was hacky, but it worked pretty well.
git reset --HARD commitId?git gcbefore execute the command