如何将Git补丁应用于具有不同名称和路径的文件? [英] How to apply a Git patch to a file with a different name and path?

查看:489
本文介绍了如何将Git补丁应用于具有不同名称和路径的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个存储库。其中之一是对文件 ./ hello.test 进行更改。我提交更改并使用 git format-patch -1 HEAD 从该提交中创建一个修补程序。现在,我有第二个存储库,其中包含一个文件,其内容与hello.test相同,但以不同的名称放在不同的目录中: ./ blue / red / hi.test 。我如何着手将上述补丁应用于 hi.test 文件?我试过 git am --directory ='blue / red'< patch_file 但当然抱怨这些文件的命名不一样(我认为Git不关心它?)。我知道我可以编辑diff来应用于特定的文件,但我正在寻找一个命令解决方案。

可以使用 git diff创建补丁 ,然后使用 patch 实用程序,它允许您指定要应用diff的文件。



例如:

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

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


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.

解决方案

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

这篇关于如何将Git补丁应用于具有不同名称和路径的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆