56

I tried doing

git diff 13.1_dev sale_edit > patch.diff

Then I tried doing git apply patch.diff in another branch, but I got patch does not apply. How do I create patch files from diffs that I can use with git apply?

Errors received:

$ git apply --ignore-space-change --ignore-whitespace diff.diff 
diff.diff:9: trailing whitespace.

diff.diff:10: trailing whitespace.
    function set_change_sale_date() 
diff.diff:12: space before tab in indent.
      $this->sale_lib->set_change_sale_date($this->input->post('change_sale_date'));
diff.diff:14: trailing whitespace.

diff.diff:15: trailing whitespace.
    function set_change_sale_date_enable() 
warning: application/controllers/sales.php has type 100755, expected 100644
error: patch failed: application/controllers/sales.php:520
error: application/controllers/sales.php: patch does not apply
warning: application/language/english/sales_lang.php has type 100755, expected 100644
error: patch failed: application/language/english/sales_lang.php:134
error: application/language/english/sales_lang.php: patch does not apply
warning: application/libraries/Sale_lib.php has type 100755, expected 100644
error: patch failed: application/models/sale.php:170
error: application/models/sale.php: patch does not apply
warning: application/views/sales/register.php has type 100755, expected 100644
error: patch failed: application/views/sales/register.php:361
error: application/views/sales/register.php: patch does not apply

I'm trying this on Mac

7ochem
  • 2,183
  • 1
  • 34
  • 42
Chris Muench
  • 17,444
  • 70
  • 209
  • 362
  • 3
    Git is the wrong tool for the job. You can spend hours trying to apply a patch because Git does not handle whitespace and line endings properly. I once spent over an hour trying to apply a three-line patch some sent to me by email (also see [Apply patch to file that's under Git without using Git?](http://stackoverflow.com/q/35353267)). Use the `patch` program. It handles whitespace and line endings without shitting all over itself. – jww Jan 05 '17 at 00:28

4 Answers4

36

Try a:

git apply --ignore-space-change --ignore-whitespace patch.diff

As mentioned in "git: patch does not apply", this can be caused by:

  • the line endings differing between the local file system and the remote repo.
    User core.eol in .gitattributes file is a good approach (see "git force file encoding on commit")
  • the execution bit ('x').
    That can lead you to set git config core.filemode false, followed by a git reset --hard HEAD (make sure you don't have uncommitted changes, or they would be lost).
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Can you give more info about the executuion bit? – stdcall Apr 14 '13 at 01:26
  • @ChrisMuench can you try the same command, but after a `git config core.filemode false`? – VonC Apr 14 '13 at 15:42
  • Still gives me mode errors. I even tried doing it via --global – Chris Muench Apr 14 '13 at 15:46
  • @ChrisMuench would http://stackoverflow.com/questions/4770177/git-patch-does-not-apply#comment5279478_4770223 help ? (a `git reset --hard HEAD` in order to apply the new option) – VonC Apr 14 '13 at 15:49
  • @ChrisMuench so the git apply still fails after the reset? Whet OS and version of git are you using? – VonC Apr 14 '13 at 15:51
  • It is Mac OS X 10.8.3 git version 1.7.12.4 (Apple Git-37) – Chris Muench Apr 14 '13 at 15:51
  • Is there a way to get this to work without modifying patch files all the time? This is going to be an ongoing process where we apply a diff to another repo. – Chris Muench Apr 14 '13 at 16:04
  • @ChrisMuench not sure, but I woud like to know if that modification work (just as a test, a quick workaround for this one instance) – VonC Apr 14 '13 at 16:04
  • That didn't work. I am not sure what it was asking me to do. I changed 0644 to 0755 in my patch file – Chris Muench Apr 14 '13 at 16:09
  • You should run `git diff --check` before creating patches instead of ignoring the whitespace errors – alternative Apr 14 '13 at 17:23
  • @ChrisMuench just to report that user named "alternative" has a suggestion: http://stackoverflow.com/questions/15993861/git-create-patch-with-diff/15994287#comment22818549_15994287 – VonC Apr 14 '13 at 17:30
  • Git diff --cached just returns the whitespace errors.. Is there a way to create patch files that ignore file permissions? – Chris Muench Apr 14 '13 at 19:23
  • @ChrisMuench all the different solutions are listed here: http://stackoverflow.com/questions/1580596/how-do-i-make-git-ignore-mode-changes-chmod. The `git update-index` is intriguing: http://stackoverflow.com/a/1580680/6309 – VonC Apr 14 '13 at 20:11
  • Note that this doesn't get untracked files. – phyatt Apr 02 '22 at 00:17
22

You can apply the patch as a 3-way merge:

git diff 13.1_dev sale_edit > patch.diff
git apply -3 patch.diff

It should bring up the conflict so that you can resolve manually. Or you could go with a one-liner, piping the patch to git-apply directly:

git diff 13.1_dev sale_edit | git apply -3

To reverse the patch:

git diff 13.1_dev sale_edit | git apply -3 -R

(note: this is same as the commands above, without the two-stage process of creating the patch file)

git help apply

-3, --3way           
When the patch does not apply cleanly, fall back on 3-way merge if 
the patch records the identity of blobs it is supposed to apply to, 
and we have those blobs available locally, possibly leaving 
the conflict markers in the files in the working tree for the user 
to resolve...
Jeff Kiiza
  • 221
  • 2
  • 4
1

Here you have to try it with the branch you have diff with.

git diff 13.1_dev sale_edit > patch.diff yourBranch()
om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
User123456
  • 2,492
  • 3
  • 30
  • 44
1

With git version 1.9.1, I am seeing similar complaints when use 'git apply' to apply the patch created using 'git diff'.

It seems 1.9.1 git is having problem dealing with mixture of spaces & tabs in the patch file.

warning: squelched 1 whitespace error warning: 6 lines add whitespace errors.

@VonC's answer does not help and I am still getting the same warnings.

The easiest solution is to simply use the 'patch' command which successfully applies all changes captured in 'git diff' output to the target git directory.

$ patch --version GNU patch 2.7.1

Tzunghsing David Wong
  • 1,271
  • 14
  • 10