Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove lines from git patch without corrupting

Tags:

git

git-diff

diff

I have a large and complicated git diff. It has about 1200 cases where it removed a line containing the string "fubar" (with the quotes, if that matters). What I want to do is to apply the patch, but don't remove those lines.

I tried removing every line of the following form from the patch file. The problem is that then the line numbers are wrong, so the patch is corrupt. Short of editing the line numbers, I'm wondering if there is a sane way around the problem.

What I removed:

- (whatever here) "fubar" (whatever else here)
like image 868
William Jockusch Avatar asked May 25 '16 09:05

William Jockusch


People also ask

How to remove a line from patch file?

Basically, you have to copy the complete line, then «remove» the first character (by replacing the space at the begin of the line with a - ), and «add» the second one with the change (meaning replacing the by a + ). In a diff, the first column is an indicator about the line status (added, removed, or unchanged).

How do I amend a git patch?

git add --edit allows you to edit your changes in patch format. git commit --reuse-message=ORIG_HEAD : commit your staged changes with the commit message from ORIG_HEAD which is the pointer to the commit you made before git reset .

What is git diff patch?

GIT patch or GIT diff is used to share the changes made by you to others without pushing it to main branch of the repository.


1 Answers

If you want the patch to not remove a line, you have to replace the - at the beginning of the line by a space. In your patch you then have an unmodified line that stays there for context.

Replace:

- (whatever here) "fubar" (whatever else here)

with

  (whatever here) "fubar" (whatever else here)

(Notice how the text in the modified line is still aligned with the original, which confirms that the - was replaced with a space and not just deleted.)

To understand patches better, take a look at this example:

diff --git a/test.txt b/test.txt
index 67179f2..9c17455 100644
--- a/test.txt
+++ b/test.txt
@@ -2,6 +2,8 @@ Line 1
 Line 2
 Line 3
 Line 4
+Line 4.5
+Line 4.6
 Line 5
 Line 6
 Line 7
@@ -10,8 +12,6 @@ Line 9
 Line 10
 Line 11
 Line 12
-Line 13
-Line 14
 Line 15
 Line 16
 Line 17
@@ -19,7 +19,7 @@ Line 18
 Line 19
 Line 20
 Line 21
-Line 22
+Line 22 the cops
 Line 23
 Line 24
 Line 25

The first lines:

diff --git a/test.txt b/test.txt
index 67179f2..9c17455 100644
--- a/test.txt
+++ b/test.txt

tell us that:

  • the diff concerns file test.txt in a version we label a, and the same file test.txt in a version we label b;
  • the file in version a is stored under the hash 67179f2 and the file in version b under the hash 9c17455 (those are not commit hashes but object hashes, if I’m not mistaken);
  • - in the hunk lines means version a and + means version b.

Then we have a hunk:

@@ -2,6 +2,8 @@ Line 1
 Line 2
 Line 3
 Line 4
+Line 4.5
+Line 4.6
 Line 5
 Line 6
 Line 7
  • -2,6 means it represents 6 lines in version a, starting at line 2;
  • +2,8 means it represents 8 lines in version b, starting at line 2.

Indeed, we added two lines, Line 4.5 and Line 4.6, which we can see from the + lines:

+Line 4.5
+Line 4.6

Notice how the other, unmodified lines displayed for context start with a space to label them as context lines.

If you wanted to modify your patch to not add Line 4.6, you would have to delete the corresponding + line. But you would also have to correct the line counts in the hunk, because you only add one line, so you end up with 7 lines, not 8:

@@ -2,6 +2,7 @@ Line 1
 Line 2
 Line 3
 Line 4
+Line 4.5
 Line 5
 Line 6
 Line 7

In our second hunk:

@@ -10,8 +12,6 @@ Line 9
 Line 10
 Line 11
 Line 12
-Line 13
-Line 14
 Line 15
 Line 16
 Line 17
  • -10,8 means it represents 8 lines in version a, starting at line 10;
  • +12,6 means it represents 6 lines in version b, starting at line 12.

It starts at line 12 because of the two lines added by the previous hunk. Normally, you would have to adjust the 12 to an 11 because you now add only one line in the previous hunk, but in practice you don’t need to because the context lines help identify the actual lines that are modified.

Now if you wanted to not remove line 14, because you are only superstitious about the number 13, you have to keep line 14 there, but as a context line, since it’s still present after the patch is applied. So you change the - to a space, and correct the line count (you end up with 7 lines, not 6):

@@ -10,8 +12,7 @@ Line 9
 Line 10
 Line 11
 Line 12
-Line 13
 Line 14
 Line 15
 Line 16
 Line 17

Finally, the last hunk:

@@ -19,7 +19,7 @@ Line 18
 Line 19
 Line 20
 Line 21
-Line 22
+Line 22 the cops
 Line 23
 Line 24
 Line 25

It’s changing Line 22 to Line 22 the cops, by removing the old line and adding the modified one. It starts at line 19 before and after, because the previous hunks deleted the same number of lines that they added in total, and concerns 7 lines both ways, since it adds one and removes one.

You can alter the change in different ways. Change the content of the modified line:

@@ -19,7 +19,7 @@ Line 18
 Line 19
 Line 20
 Line 21
-Line 22
+Line 22 THE COPS
 Line 23
 Line 24
 Line 25

or add an extra line:

@@ -19,7 +19,8 @@ Line 18
 Line 19
 Line 20
 Line 21
-Line 22
+Line 22
+    the cops
 Line 23
 Line 24
 Line 25

which can be simplified, since it doesn’t modify line 22 anymore:

@@ -19,7 +19,8 @@ Line 18
 Line 19
 Line 20
 Line 21
 Line 22
+    the cops
 Line 23
 Line 24
 Line 25
like image 181
Olivier 'Ölbaum' Scherler Avatar answered Oct 07 '22 00:10

Olivier 'Ölbaum' Scherler