21

git rebase leaves the conflict markers inline in files; something like:

<<<<<<< HEAD
Whatever line + context is different from the previous commit
=======
Whatever line + context is different from the commit being applied
>>>>>>> new version, new branch:app/views/common/version.txt

When I use git apply to apply a patch created with git format-patch, it will fail to leave any files modified by default. I can give it --reject which will cause it to create .rej files for those that have unresolvable conflicts, but really, I want it to modify files and leave every in the state that git rebase does so I can just open the file, manually merge it, and then git add it and tell git apply to continue. Is there some way to do this that I just don't know of?

3 Answers 3

10

For me the following works:

git init
seq 1 30 > file
git add file
git commit -m 1-30
sed -i '13a13.4' file
git commit -m 'add 13.4' file
git format-patch -1
git reset --hard HEAD^
sed -i 13d file
git commit -m 'remove 13' file
git am -3 0001-add-13.4.patch

After that file has conflict markers. That is use git am -3 instead of git apply.

2
  • Hmmm - interesting that does indeed seem to do what I'm wanting. I thought that git am used git apply - perhaps it's uses an internal API that isn't accessible through git apply's command line options. Thanks!
    – kcstrom
    Commented Mar 17, 2012 at 20:18
  • 2
    TL;DR: use the -3 option to git am or git apply (whichever you happen to use). The option tells git to perform a merge instead of just aborting on conflict.
    – Hi-Angel
    Commented Oct 7, 2018 at 13:25
8

Use the option with three way merge:

git apply -3 0001-patch.patch
1
  • 1
    That does not work: error: patch failed: file.cpp:383 error: repository lacks the necessary blob to fall back on 3-way merge. error: file.cpp: patch does not apply Commented Nov 13, 2020 at 21:41
8

A different solution is to apply the patch using patch --merge instead of any git tool. This worked much better for me when bringing some changes from a completely different git repo, so for which I couldn't (easily) use git am -3 or git apply -3.

3
  • It can even do: patch --merge=diff3 Commented Nov 14, 2020 at 10:04
  • This resolves " That does not work: error: patch failed: file.cpp:383 error: repository lacks the necessary blob to fall back on 3-way merge. error: file.cpp: patch does not apply "
    – Kevin
    Commented Apr 7, 2022 at 1:38
  • +1 for teaching me about --merge, which was just what I was looking for. More details: man7.org/linux/man-pages/man1/patch.1.html (or just man patch if you have a Linux/GNU environment nearby. Unsure if --merge is part of POSIX or not.) Commented Apr 29, 2022 at 13:20

Your Answer

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

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