Get error message ''fatal: sha1 information is lacking or useless“ when apply a patch using ”git am -3"

给你一囗甜甜゛ 提交于 2019-11-27 23:00:58

问题


I am trying to apply a series of patches from 1 git repository to another git repository using git am -3 "path to a patch". I apply them in order, from patch 1-4, it works fine.

But when I come to 5th patch,I get the error saying "fatal: sha1 information is lacking or useless". I go the to git repository where I apply the patch, I do see the file 'dev/afile'. So I wonder why git is complaining about "sha1 information is lacking or useless (dev/afile.c)" and how can I fix my problem?

 $ git am -3 ~/Tmp/mypatches/0005-fifth.patch
Applying: rpmsg: Allow devices to use custom buffer allocator
fatal: sha1 information is lacking or useless (dev/afile.c).
Repository lacks necessary blobs to fall back on 3-way merge.
Cannot fall back to three-way merge.
Patch failed at 0001 first patch
When you have resolved this problem run "git am --resolved".
If you would prefer to skip this patch, instead run "git am --skip".
To restore the original branch and stop patching run "git am --abort".

And why it said "Patch failed at 0001 first patch", when I do "git am -3 ~/Tmp/mypatches/0005-fifth.patch", it completes with no error.

Thank you.


回答1:


The patch file starting with 0001- cannot be applied cleanly - there is some conflict.

Git wanted to resolve that conflict by looking at commits this patch has been based on, but you don't have those commits in your repository.

Probably the patch has been created from a branch that had commits that were never shared, or either your or submitter's branch has been rebased.

It doesn't matter that patch 0005- can be applied with no error. The error is about 0001- specifically.




回答2:


Are you using submodules in your project?

There was a bug in git 1.7.12 to 1.8.1.2 where an updated submodule would cause a rebase (or patch) to fail with the error message:

fatal: sha1 information is lacking or useless

leaving the commit empty if applied.

More info here.

Updating git to version 1.8.4 solves this problem




回答3:


Just did the following and was able to solve this issue:

patch -p1 < example.patch



回答4:


I ran into this issue when I tried to create the patch while on the wrong branch.

I thought "git format-patch ..." would be able to determine what I wanted because you can specify the main branch and the branch you want to patch in the format-patch call. I realized it was wrong because it was mentioning commits that were part of a branch that didn't exist on the site I was patching to.

Long story short, make sure you are on the branch you want to patch when you create the patch.




回答5:


I had this when trying to apply patches from one repository into one which had unrelated history (the same project but with rebuilt git history). The reason you get the message fatal: sha1 information is lacking or useless (dev/afile.c) is that when git is trying to do a 3 way merge it needs to access the state of that file. Those files are pointed to by the hashes in the format patch output (e.g.)

diff --git a/dev/afile.c b/dev/afile.c
index ebbd50fc0b7..ef1ca87ead0 100644
--- a/dev/afile.c
+++ b/dev/afile.c

ebbd50fc0b7 and ef1ca87ead0 refer to hashes of the content of the files, not commit hashes.

If you try:

git cat-file blob <hash from patch>

Git will report:

fatal: Not a valid object name <hash from patch>

Git can't find them because those versions of the file are not available in your local repository (hence the message Repository lacks necessary blobs to fall back on 3-way merge.). You can make those objects available in your local repository with:

git remote add old_repo <url>
git fetch old_repo

Now, when you run:

git cat-file blob <hash from patch>

You should be given the contents of that file. Now try your git am command again and it should be able to do 3 way merges.



来源:https://stackoverflow.com/questions/16572024/get-error-message-fatal-sha1-information-is-lacking-or-useless-when-apply-a

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!