git runtime_error ai_generated true

error: patch failed: file.txt:1 error: file.txt: patch does not apply

ID: git/am-failed-patch

Also available as: JSON · Markdown · 中文
80%Fix Rate
82%Confidence
1Evidence
2023-09-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
git 2.41.0 active
git 2.42.0 active
git 2.43.0 active

Root Cause

A patch file (from git format-patch or email) cannot be applied because the target file has diverged from the patch's base.

generic

中文

补丁文件(来自git format-patch或邮件)无法应用,因为目标文件与补丁的基础版本存在差异

Official Documentation

https://git-scm.com/docs/git-am

Workarounds

  1. 85% success Apply the patch with 'git apply --reject' to see which hunks fail, then manually fix: git apply --reject patch.diff && vim file.txt
    Apply the patch with 'git apply --reject' to see which hunks fail, then manually fix: git apply --reject patch.diff && vim file.txt
  2. 75% success Use 'git am --3way --ignore-whitespace' to apply with more tolerance: git am --3way --ignore-whitespace patch.mbox
    Use 'git am --3way --ignore-whitespace' to apply with more tolerance: git am --3way --ignore-whitespace patch.mbox

中文步骤

  1. Apply the patch with 'git apply --reject' to see which hunks fail, then manually fix: git apply --reject patch.diff && vim file.txt
  2. Use 'git am --3way --ignore-whitespace' to apply with more tolerance: git am --3way --ignore-whitespace patch.mbox

Dead Ends

Common approaches that don't work:

  1. Using 'git am --3way' without checking for conflicts first 70% fail

    3-way merge may still fail if the patch context is too old

  2. Manually editing the patch file to change line numbers 90% fail

    Patch context (surrounding lines) must match exactly; changing line numbers alone doesn't fix context mismatches