如何将 Git 补丁应用于具有不同名称和路径的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16526321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to apply a Git patch to a file with a different name and path?
提问by mart1n
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.
我有两个存储库。一方面,我对 file 进行了更改./hello.test
。我提交更改并使用git format-patch -1 HEAD
. 现在,我有第二个存储库,其中包含一个与 hello.test 具有相同内容但以不同名称放置在不同目录中的文件:./blue/red/hi.test
. 如何将上述补丁应用于hi.test
文件?我试过了,git am --directory='blue/red' < patch_file
但当然抱怨文件的名称不同(我认为 Git 不关心?)。我知道我可能可以编辑差异以应用于该特定文件,但我正在寻找命令解决方案。
采纳答案by georgebrock
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.
您可以使用创建补丁git diff
,然后使用该patch
实用程序应用它,该实用程序允许您指定要将差异应用到的文件。
For example:
例如:
cd first-repo
git diff HEAD^ -- hello.test > ~/patch_file
cd ../second-repo
patch -p1 blue/red/hi.test ~/patch_file
回答by magiraud
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, use -1
if you want to select only one commit) :
在第一个存储库中(这也可能导出一系列提交,-1
如果您只想选择一个提交,请使用):
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.
另一种解决方案不是使用--relative
in git format-patch
,而是使用-p<n>
选项 in从补丁路径中git am
去除n
目录,如对类似问题的回答中所述。
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
.
也可以在git format-patch --relative <committish>
没有 的情况下运行--stdout
,它会生成一组.patch
文件。这些文件可以被直接输送到git am
用git am --directory blue/red/ path/to/*.patch
。
回答by mart1n
Answering my own question with a script that does just this: https://github.com/mprpic/apply-patch-to-file
用一个脚本来回答我自己的问题:https: //github.com/mprpic/apply-patch-to-file
Rather than modifying the patch file manually, it prompts the user for the target file, modifies the patch, and applies it to the repo you're currently in.
它不是手动修改补丁文件,而是提示用户输入目标文件、修改补丁并将其应用到您当前所在的存储库。
回答by oliver
Building upon the answer by @georgebrock, here's a solution I used:
基于@georgebrock 的回答,这是我使用的解决方案:
First, create the patch files as usual (eg. git format-patch commitA..commitB
).
首先,像往常一样创建补丁文件(例如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:
对于每个补丁文件,您都会收到类似“错误:索引中不存在 XYZ”的错误消息。您现在可以手动应用此补丁文件:
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.
这将保留原始提交消息等,无需任何特殊git format-patch
命令或编辑补丁文件。
回答by Gqqnbig
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
.
说你修改过的文件A
,让我们来分别表示A~1
为以前的版本,并且要与应用差异A~1
来A
对文件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
.
打开一个三路合并工具,比如Beyond Compare,左面板的路径是A
,中间面板是共同的祖先所以路径是A~1
,右面板的路径是B
。然后,下图显示应用之间的差异的结果A~1
,以A
对文件B
。
The following figure illustrates the idea.
下图说明了这个想法。
回答by Mike Robinson
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).
仅供参考:我最近在尝试从 Github 下载补丁并将其应用到本地文件(这是新位置的“覆盖”)时遇到问题。
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 couldapply the patch. It did prompt me for the name of the file to be patched.
git am
不会应用补丁,因为文件“不在索引中”或“脏”。但是,我发现简单的patch
命令可以应用补丁。它确实提示我输入要修补的文件的名称。
Got the job done, anyway ...
完成任务,反正...