git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Incorrect behavior from git checkout --patch

From
DKDave Kaminski <davekaminski@gmail.com>
Date
Aug 10, 2019, 20:12 UTC
Message-ID
<CA+zjYZsojm0QgHBC3GFb2NeZR1aK3Uh7vO4ojBzsMKVvP6X+Tg@mail.gmail.com>
I am observing git checkout --patch making changes to the wrong lines of a file.

This is with a clean install of git version 2.20.1 on a debian docker container (image tag 10.0 which is also "latest" as of this writing).

With a diff that looks like the following:
diff --git a/file.txt b/file.txt
index 868aa22..ea4d786 100644
--- a/file.txt
+++ b/file.txt
@@ -1,35 +1,51 @@
 block_one {
 line 1
+line 1.5
 line 2
+line 2.5
 line 3
 2 4 6 8
 line 4
+line 4.5
 line 5
+line 5.5
 line 6
 2 4 6 8
 line 7
+line 7.5
 line 8
+line 8.5
 line 9
 2 4 6 8
 line 10
+line 10.5
 line 11
+line 11.5
 line 12
 }

 block_two {
 line 1
+line 1.5
 line 2
+line 2.5
 line 3
-2 5 6 9
+2 4 6 8
 line 4
+line 4.5
 line 5
+line 5.5
 line 6
-2 5 6 9
+2 4 6 8
 line 7
+line 7.5
 line 8
+line 8.5
 line 9
-2 5 6 9
+2 4 6 8
 line 10
+line 10.5
 line 11
+line 11.5
 line 12
 }

doing a `git checkout --patch -- ./file.txt`, splitting the diff into
hunks, and discarding all of the hunks that begin with numbers, e.g.

@@ -22,3 +32,3 @@
 line 3
-2 5 6 9
+2 4 6 8
 line 4

the expected state of the file in the working directory is this:

diff --git a/file.txt b/file.txt
index 868aa22..9ab67a1 100644
--- a/file.txt
+++ b/file.txt
@@ -1,35 +1,51 @@
 block_one {
 line 1
+line 1.5
 line 2
+line 2.5
 line 3
 2 4 6 8
 line 4
+line 4.5
 line 5
+line 5.5
 line 6
 2 4 6 8
 line 7
+line 7.5
 line 8
+line 8.5
 line 9
 2 4 6 8
 line 10
+line 10.5
 line 11
+line 11.5
 line 12
 }

 block_two {
 line 1
+line 1.5
 line 2
+line 2.5
 line 3
 2 5 6 9
 line 4
+line 4.5
 line 5
+line 5.5
 line 6
 2 5 6 9
 line 7
+line 7.5
 line 8
+line 8.5
 line 9
 2 5 6 9
 line 10
+line 10.5
 line 11
+line 11.5
 line 12
 }

but instead the actual state of the file is this:

diff --git a/file.txt b/file.txt
index 868aa22..76fe65d 100644
--- a/file.txt
+++ b/file.txt
@@ -1,35 +1,51 @@
 block_one {
 line 1
+line 1.5
 line 2
+line 2.5
 line 3
 2 4 6 8
 line 4
+line 4.5
 line 5
+line 5.5
 line 6
 2 4 6 8
 line 7
+line 7.5
 line 8
+line 8.5
 line 9
-2 4 6 8
+2 5 6 9
 line 10
+line 10.5
 line 11
+line 11.5
 line 12
 }

 block_two {
 line 1
+line 1.5
 line 2
+line 2.5
 line 3
 2 5 6 9
 line 4
+line 4.5
 line 5
+line 5.5
 line 6
 2 5 6 9
 line 7
+line 7.5
 line 8
+line 8.5
 line 9
-2 5 6 9
+2 4 6 8
 line 10
+line 10.5
 line 11
+line 11.5
 line 12
 }

See the changes between "line 9" and "line 10" in both blocks that are
not correct.
Next: Phillip Wood
Message 1 of 3 in “Incorrect behavior from git checkout --patch”
  1. Dave KaminskiAug 10, 2019
  2. Phillip WoodAug 11, 2019
  3. Phillip WoodAug 11, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.