Git apply --3way: don't abort if some hunks fail

Situation

I have a file named testing.txt

, committed to the repo, with the following content:

First line

      

I am trying to apply this patch, which changes two files, testing.txt

and nonexistent.txt

:

diff --git a/testing.txt b/testing.txt
index 5e867b1..e5c47f2 100644
--- a/testing.txt
+++ b/testing.txt
@@ -1 +1,2 @@
 Non-matching first line
+Second line from patch
diff --git a/nonexistent.txt b/nonexistent.txt
index 9649cde..8aab0eb 100644
--- a/nonexistent.txt
+++ b/nonexistent.txt
@@ -1 +1,2 @@
 First line
+Second line from patch

      

with this command:

git apply --verbose --3way patch.txt

      

During command execution, the hunk is testing.txt

successfully applied with a fallback three-way merge:

Checking patch testing.txt...
error: while searching for:
Non-matching first line

error: patch failed: testing.txt:1
Falling back to three-way merge...
Applied patch to 'testing.txt' with conflicts.

      

and (as expected) crashing nonexistent.txt

(since this file doesn't exist):

Checking patch nonexistent.txt...
error: nonexistent.txt: does not exist in index

      

Problem

The whole command apply

is aborted as the failure nonexistent.txt

does not work.

A three-way merge for a chunk is testing.txt

discarded even if it was successful.

This is stated in the docs :

For atomicity git apply

, the default does not remove the entire patch and does not touch the working tree when some are not applied.

Question

I want the successful testing.txt

hunk to be applied to the file and not discarded. How can i do this?

In other words, how can I change the fatal error nonexistent.txt

to a non-fatal warning so that the command apply

doesn't get interrupted?

(With this simple example, I can just remove a chunk nonexistent.txt

from the patch file, but this is not practical for a large patch where you might need to delete hundreds of chunks.)

What i tried

I have looked through the docs but I cannot find a way to do this.

The --reject

option "allows you to apply parts of the patch that are applicable and leave the discarded hunks in the corresponding * .rej files", but it is incompatible with the option --3way

.

I've also tried -C0

and --unidiff-zero

(ignore all context).

+3


source to share


1 answer


This worked for me:



git apply --verbose --3way --exclude=nonexistent.txt patch.txt

      

+1


source







All Articles