How to apply a very old patch in Git?
Asked Answered
P

2

7

I have a git repository, and a very old patch (made in 2007) that I want to apply to it. The contents of the repository have changed significantly since then, and when I try to apply the patch, it fails due to conflicts.

I would like to deal with the conflicts inside git, by adding the patch as a new branch and merging it back to master. But to do that I need to locate the earliest commit that the patch can apply onto cleanly. Is there an easy way of doing this in git?

Primatology answered 20/2, 2012 at 18:36 Comment(0)
C
13

You should be able to do this with git bisect: start the bisection as normal, and then use:

git bisect run git apply --check ${patch}

That will use that command - "can this patch apply" - to determine if a revision is good or bad, automatically. You should shortly afterwards have the right location turned out.

That will give you the most recent commit where the patch will apply.

If you want the earliest, run a second bisect with "does not apply" as the test, between the start of the repository and the place that it does apply successfully.

Chang answered 20/2, 2012 at 18:40 Comment(3)
git bisect needs a good revision, and if I knew that, I wouldn't have this problem. :(Rickirickie
This approach doesn't seem to be that easily usable while generating patches using git format-patch. It requires quiet a bit of work to find good and bad commits. We can't just give first commit in the repo as good commit and latest commit as bad commit. For more details consider below scenario: git bisect finds the first bad commit. Let's say there were 10 commits in the beginning in the develop branch and "fileA" was added in one of the 8th commit. Then a feature branch was created after 10th commit and lets say 3 commits were done modifying fileA in the feature branch.Edmee
Meanwhile in develop branch, after feature branch got created, lets say 2 more commits were done and then another commit was done where contents of fileA were changed too much such that applying patch fails due to context mismatch. Now if you look at develop branch, the latest commit will be a bad commit, but 2nd and 3rd latest commit will be good commit. The first commit in the develop branch will be a bad commit because git apply would fail telling that fileA does not exist. Only two or three commits in the middle are good commitsEdmee
P
1

If the patch records the identity of blobs it is supposed to apply to, you can use git apply --3way to fallback on three-way merge if patches does not cleanly apply.

From man git apply:

-3, --3way

When the patch does not apply cleanly, fall back on 3-way merge if the patch records the identity of blob 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. This option implies the --index option, and is incompatible with the --reject and the --cached options.

Pyrrhotite answered 23/7, 2016 at 21:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.