A patch does not say where it belongs#
A patch has context lines and blob IDs, and no commit. If it fails on the receiver's branch, three explanations fit: the branch moved and the change conflicts, the receiver is on an unrelated branch, or an earlier patch in the series is missing. --base separates them:
git format-patch --base=main -o out/ main..topic
tail -4 out/0001-*.patch
base-commit: d1d79457fd7e0e7d76d8a9208f2f0dbe28a6531b
--
2.43.0
The git format-patch manual calls this base tree information. When the series rests on patches that are not public, it also lists each one's patch ID under prerequisite-patch-id.
Read the three failures#
The fixture changes line 2 of a three-line file, and the receiver's branch has changed line 3.
Plain git am fails because the context differs:
error: patch failed: a:1
error: a: patch does not apply
git am --3way falls back to a merge, because the receiver holds the original blob, and leaves ordinary conflict markers:
Falling back to patching base and 3-way merge...
CONFLICT (content): Merge conflict in a
In an unrelated repository, the same command cannot build a merge at all:
error: sha1 information is lacking or useless (a).
error: could not build fake ancestor
The last message means the receiver does not have the file version the patch was made from. That is a different problem from a conflict, and resolving text by hand will hide it. The manual for git am states the condition: the three-way merge needs the blobs the patch names to be available locally.
Decide with the base#
git cat-file -t BASE # "commit" when this clone holds it
git merge-base --is-ancestor BASE HEAD; echo $?
| Result | Next decision |
|---|---|
| The object is missing | Fetch, or ask for a bundle. Do not edit the patch |
Held, and an ancestor of HEAD (0) | The branch moved on. Apply with --3way and resolve |
| Held, and not an ancestor (1) | You may be on the wrong branch. Apply on the base first |
When the branch moved, applying on the base gives you the sender's tree exactly: git switch -c check BASE && git am out/*.patch. Run their test there. If it passes on the base and fails after a rebase, the rebase is the thing to look at.
Limits#
A shallow clone can fail the first check while the code is otherwise right. An ancestor check says nothing about whether the patch's behavior still holds on the newer code. A patch that applies cleanly can still be wrong, for example when a function it calls has changed meaning.
Worked example#
This example is fictional. Ben receives two patches and the second fails with could not build fake ancestor. The base line names a commit he does not have. Ana had branched from a colleague's unmerged work. She sends that commit's bundle first, and both patches apply on the base. The decision that remained was whether to wait for the colleague's branch, which is the question the bare patch had hidden.
A bundle carries the base itself. Other handoffs are listed in the collection's first guide.