173

I am on windows.

For various reasons we have multiple git instances of different svn branches.

Many times I want to fix an issue in repository A, generate a patch, and apply it to repository B. This works fine except if there are conflicts.

When rebasing I just right click the folder and use tortioseGit and select the resolve option. This brings up a nice gui to let me work through my conflicts.

Is there any way to accomplish this with rejected patch chunks?

Here is my current approach to creating/applying the patches

git format-patch master --stdout > c:\\patch\\file.patch
git apply --reject --ignore-space-change --ignore-whitespace c:\\patch\\file.patch
4
  • 3
    I usually do it by hand when all patching options fail...
    – stdcall
    Jun 6, 2013 at 13:58
  • If the merge fails, it is because the program can't figure out how to un-ambiguously do the merge. You should get a file with <<<<, ===, >>>> sets and you have to go in and resolve them by hand.
    – tacaswell
    Jun 6, 2013 at 14:50
  • Yeah doing it by hand is a real pita when your talking a few 100 rej hunks.
    – Kenoyer130
    Jun 6, 2013 at 19:23
  • 1
    That's when you'd want to use git mergetool and do a 3-way merge with the gui of your choice (i'm partial to kdiff on windows)...
    – g19fanatic
    Jun 9, 2013 at 22:27

5 Answers 5

349
+100

To generate your patch do the following:

git format-patch --stdout first_commit^..last_commit > changes.patch

Now when you are ready to apply the patches:

git am -3 < changes.patch

the -3 will do a three-way merge if there are conflicts. At this point you can do a git mergetool if you want to go to a gui or just manually merge the files using vim (the standard <<<<<<, ||||||, >>>>>> conflict resolution).

11
  • 9
    It may be useful to add --ignore-whitespace --ignore-space-change to git am too. I had trivial merges that did not go through without it.
    – angularsen
    Aug 27, 2015 at 6:33
  • 26
    git apply -3 changes.patch seems to work for me as well
    – peterflynn
    Oct 14, 2015 at 21:44
  • 3
    Even though the patch doesn't apply cleanly I still get "No files need merging" from git mergetool. Instead I had to find the base commit the original patch used, apply on top of that (luckily my repo had this) and then rebase.
    – jozxyqk
    Sep 11, 2018 at 19:01
  • 7
    I'm having the same problem as @jozxyqk. Neither git am -3 nor git apply -3 will actually drop conflict markers into my files, even though I get messages like Applied patch to 'configure.ac' with conflicts. and error: patch failed: .... This is on git 2.17.1. Perhaps when some files can't be patched at all, git rolls back?
    – nh2
    Nov 13, 2018 at 23:35
  • 2
    I got the same issue as @nh2, did you ever find the problem?
    – Eridanis
    Mar 1, 2019 at 12:06
16

If you are frequently running into the same conflict set when applying patches, rebasing or merging then you can use git rerere (reuse recorded resolution) function. This allows you to pre-define how conflicts should be resolved based on how you resolved them in the past. See http://git-scm.com/blog/2010/03/08/rerere.html for details of how this works.

10

Just use

git apply -3 <patch_name>

it will allow you to fix conflicts

5

TortoiseGit has a merge feature that can open patch files.

There's a picture of it here.

4
  • 1
    Actually the merge option might be what I'm looking for.
    – Kenoyer130
    Jun 6, 2013 at 19:28
  • Really? I've not used tortoise in a long time, but the linked page has the text "TortoiseMerge can open Git patch file directly, you review it and patch to working copy.", so it seems like it should!
    – ams
    Jun 7, 2013 at 19:48
  • it does open patch files... however, sometimes the patch file format breaks tortoisegitmerge. i've never been successful with diff -u, but rather diff -c output. Aug 19, 2014 at 17:46
  • You can also right drag patches on a working tree folder and select "Apply patch serial" (for patches like 0001-xxx.patch, ... 0002-xxy.patch) or "Apply single patch file".
    – MrTux
    Sep 6, 2015 at 19:24
4

My approach is:

  • Create an "Integration"-Branch where the files are identical
  • Apply the patch to this Integration-Branch
  • Merge or rebase it to master (don't know if rebase is useful here, because I don't know what will happen when applying further patches)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.