How to apply a Git patch to a file with a different name and path?

GitFilenamesPatchGit PatchGit Am

Git Problem Overview

I have two repositories. In one, I make changes to file ./hello.test. I commit the changes and create a patch from that commit with git format-patch -1 HEAD. Now, I have a second repository that contains a file that has the same contents as hello.test but is placed in a different directory under a different name: ./blue/red/hi.test. How do I go about applying the aforementioned patch to the hi.test file? I tried git am --directory='blue/red' < patch_file but that of course complains that the files are not named the same (which I thought Git didn't care about?). I know I could probably edit the diff to apply to that specific file but I'm looking for a command solution.

Git Solutions

Solution 1 - Git

You could create the patch using git diff and then apply it using the patch utility, which allows you to specify the file you want to apply the diff to.

For example:

cd first-repo
git diff HEAD^ -- hello.test > ~/patch_file

cd ../second-repo
patch -p1 blue/red/hi.test ~/patch_file

Solution 2 - Git

There is a simple solution that does not involve manual patch editing nor external script.

In the first repository (this may also export a range of commit, add the -1 flag if you want to select only one commit) :

git format-patch --relative <committish> --stdout > ~/patch

In the second repository :

git am --directory blue/red/ ~/patch

Instead of using --relative in git format-patch, another solution is to use -p<n> option in git am to strip n directories from the path of the patches, as mentioned in a answer to a similar question.

It is also possible to run git format-patch --relative <committish> without the --stdout, and it will generate a set of .patch files. These files can then be fed directly to git am with git am --directory blue/red/ path/to/*.patch.

Solution 3 - Git

Building upon the answer by @georgebrock, here's a solution I used:

First, create the patch files as usual (eg. git format-patch commitA..commitB).

Then make sure that your target repository is clean (there should be no changed or untracked files) and apply the patches like this:

cd second-repo
git am ~/00*.patch

For every patch file you will get an error like "error: XYZ does not exist in index". You can now apply this patch file manually:

patch --directory blue/red < ~/0001-*.patch
git add -a
git am --continue

You have to do these three steps for each patch file.

This will preserve the original commit message etc. without requiring any special git format-patch command or editing the patch files.

Solution 4 - Git

I understand the two files are exactly the same in your situation, thus the patch is likely to succeed.

However, in case you want to apply a patch to a similar, but not exactly the same file, or you want to do an interactive patching, you will use three way merge.

Say you modified File A, let's denote A~1 as the previous version, and you want to apply the diff between A~1 to A to File B.

Open a three way merge tool, for instance Beyond Compare, the path of left panel is A, middle panel is the common ancestor so the path is A~1, the path of right panel is B. Then, the lower panel shows the result of applying the diff between A~1 to A to File B.

The following figure illustrates the idea.

enter image description here

Solution 5 - Git

FYI: I recently had problems trying to download a patch from Github and applying it to a local file (which was an "override" in a new location).

git am wouldn't apply the patch either because the file was "not in index" or "dirty." But, I found that the simple patch command could apply the patch. It did prompt me for the name of the file to be patched.

Got the job done, anyway ...


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmart1nView Question on Stackoverflow
Solution 1 - GitgeorgebrockView Answer on Stackoverflow
Solution 2 - GitmagiraudView Answer on Stackoverflow
Solution 3 - GitoliverView Answer on Stackoverflow
Solution 4 - GitGqqnbigView Answer on Stackoverflow
Solution 5 - GitMike RobinsonView Answer on Stackoverflow