0

Question

I have:

  • A Git Repository
  • A Patch (.diff) with several conflicts

I would like to apply the patch using a strategy (like theirs), similar to git merge -s. Can this be achieved using git and/or patch?

EDIT

The original question wasn't very clear of what I'm trying to do. I have many merge conflicts from patch --merge, and I'd like to resolve them the same way (similar to git merge --strategy ...)

Related

GooseDeveloper
  • 97
  • 1
  • 11
  • 1
    How did you produce the diff file? – eftshift0 Sep 05 '22 at 19:13
  • note that there is a difference between an actual merge (where git has the complete content of 3 versions : "base", "ours" and "theirs") and a version + a patch (which is the same as having "ours" version, the diff between "base" and "theirs", but not the content of "base" or even the diff between "base" and "ours"). Depending on how the patch applies, some cases may not fit the "use theirs" action you can do on a merge. Hence the question of eftshift0. – LeGEC Sep 05 '22 at 19:54
  • 1
    You need to apply the diff on the base used to create it, then you can use merge – Ôrel Sep 05 '22 at 20:15
  • @eftshift0 I'm patching [suckless software](https://dwm.suckless.org). The [patch](https://dwm.suckless.org/patches/single_tagset/) was downloaded from their website – GooseDeveloper Sep 05 '22 at 22:13
  • @Ôrel I didn't create the diff... I'd like to apply it using the `their` strategy, but `git apply` doesn't support `--strategy` – GooseDeveloper Sep 05 '22 at 22:15
  • @Ôrel can I convert a `diff` file into a git branch? – GooseDeveloper Sep 05 '22 at 23:03
  • @GooseDeveloper : which patch in particular ? – LeGEC Sep 06 '22 at 05:28
  • It seems a bit curious that they don't just provide a repository (e.g., on GitHub). – torek Sep 06 '22 at 11:37
  • @LeGEC https://dwm.suckless.org/patches/single_tagset/ – GooseDeveloper Sep 06 '22 at 19:07
  • @torek Suckless software is a bit odd... They do provide git repositories (like the [one I'm using](https://git.suckless.org/dwm/)), but the patches are provided in `diff` format, rather than as git branches – GooseDeveloper Sep 06 '22 at 19:10

2 Answers2

3

The patches listed on that page somehow mention what commit they should be applied on :

  • three of them mention they apply to version 6.2, 6.1 or 6.0,
  • the other ones mention a commit hash in their name.

So, to apply the suggestion "create a branch and merge" (suggested by @eftshift0 or @Ôrel) :

git checkout -b with-patch <target commit: tag or has>
git apply <patch>
git commit

then switch back to your own branch, and use either merge or cherry-pick :

git checkout my-branch

git merge with-patch
# or
git cherry-pick with-patch
LeGEC
  • 46,477
  • 5
  • 57
  • 104
2

Find the base revision for the patch and apply it there. There should be no conflicts, right?

git checkout -b temp the-base-revision
git apply the-diff-file
git commit-m 'the patch' -a

Then feel free to cherry-pifk, which should give you access to tbe options you are looking for.

git checkout blahblah # where I really wamt to apply it
git cherry-pick --someOption tenp

That should work.

eftshift0
  • 26,375
  • 3
  • 36
  • 60