简体   繁体   English

git format-patch为提交的特定文件部分生成补丁

[英]git format-patch to generate a patch for a specific file part of a commit

I am trying to merge some files that were added for a feature in a branch called dev . 我试图合并,加入了一个分支叫功能,它的一些文件dev Some of the files are added and some are changed. 有些文件已添加,有些已更改。 Is there a way to generate a patch for selected files part of a commit so that they can be applied on master? 有没有一种方法可以为提交中的选定文件生成补丁,以便可以将其应用于主文件? I am looking to then apply the patch to a master. 我希望将补丁应用到母版。

git format-patch -1 SHA1 --stdout is generating a patch for the whole commit but I need to generate the patch only specific files in the commit. git format-patch -1 SHA1 --stdout正在为整个提交生成补丁,但是我只需要在提交中生成特定补丁即可。

Is there a way to generate a patch for selected files part of a commit so that they can be applied on master? 有没有一种方法可以为提交中的选定文件生成补丁,以便可以将其应用于主文件?

Maybe (I'm not sure precisely what you are asking for). 也许(我不确定您要的是什么)。 Let's start with the basic fact that git diff provides, as output, instructions for changing commit X into commit Y . 让我们从一个基本事实开始,即git diff作为输出提供了将提交X更改为提交Y的指令。 Meanwhile, git format-patch essentially just runs git diff to get the diffs for the patch (as do pretty much all other Git commands that need diffs). 同时, git format-patch本质上只是运行git diff来获取补丁的diff(几乎所有其他需要diff的Git命令也是如此)。

If you run git diff manually , you can add additional parameters. 如果您手动运行git diff ,则可以添加其他参数。 In particular, git diff accepts a list of pathspecs (basically, file names) after the options and the two commits to compare: 特别是, git diff在选项和两个提交进行比较之后接受pathspec列表(基本上是文件名):

git diff X Y -- path/to/file

This limits the "how to change the files" instructions output to show just the given file(s). 这限制了“如何更改文件”指令的输出以仅显示给定的文件。 Hence if "for selected files" means "all of the instructions for files A, B, and C, but nothing for files D and E", then a simple: 因此,如果“用于选定文件”的意思是“关于文件A,B和C的所有说明,但对于文件D和E则没有任何说明”,则简单地:

git diff X Y -- A B C

will do the trick. 会成功的

There is no way to get git format-patch to run git diff like this, but you can simply cut up and re-paste the formatted patch, ie, use git format-patch as usual, then remove the diff from it and replace it with one generated by your custom git diff . 没有办法让git format-patch这样运行git diff ,但是您可以简单地剪切并重新粘贴格式化的补丁,即,像往常一样使用git format-patch ,然后从中删除diff并替换它由您的自定义git diff生成的一个。

A better way 更好的方法

What if "for selected files" is not quite the right phrase, though? 但是,如果“对于选定的文件”不是很正确的短语怎么办? What if you need the patch for files A, B, and C, except that the patch for file B needs to be somewhat different ? 如果您需要文件A,B和C的补丁程序, 文件B的补丁程序需要有所不同,该怎么办? There is a better, more flexible way to deal with this, which can be summarized as: Don't try to fake it, just do it for real. 有一种更好,更灵活的方式来处理此问题,可以总结为: 不要试图伪造它,而要真正地做到这一点。

That is, start by checking out the same commit you wish to patch. 也就是说,首先签出您希望修补的同一提交。 (Update your own repository if necessary, by running git fetch , so that you have their master as origin/master or another_remote/master . You can add their repository as another remote, eg, git remote add another_remote url , if necessary.) Then make a new branch of your own, pointing to this particular commit: (如果需要,可以通过运行git fetch更新自己的存储库,以便将 master存储库作为origin/masteranother_remote/master 。您可以将其存储库添加为另一个远程存储库,例如git remote add another_remote url 。)自己创建一个分支,指向此特定提交:

git checkout -b for-patching-them origin/master

(This will also make your branch for-patching-them have the remote-tracking branch as its upstream.) (这还将使您for-patching-them分支将远程跟踪分支作为其上游。)

Now that you have a branch of your own , you can git cherry-pick the commit, perhaps with -n if you like. 现在您有了自己分支 ,可以使用git cherry-pick提交,如果愿意,可以使用-n You can then alter the changes as much as you want and/or need, test them, and commit. 然后,您可以更改的变化就像你想和/或需要,对其进行测试,并提交。 You can commit with --amend , or make many commits and eventually do an interactive rebase and squash the commits, or use whatever Git tools you like. 您可以使用--amend进行提交,或者进行多次提交,最终进行交互式的变基处理并--amend提交,或者使用您喜欢的任何Git工具。

Eventually, you will have one or more commits you are ready to send to them, and now you can run: 最终,您将可以准备将​​一个或多个提交发送给他们,现在您可以运行:

git format-patch origin/master

to get the precise patch to send them, without having to edit that patch and hope that it applies. 以获得精确的补丁程序发送给他们,而无需编辑该补丁程序并希望它适用。 Moreover, if they have updated their branch since you started all this work, you can run git fetch again to pick up their updates, and then git rebase your patch branch to adjust it based on their changes, so that you are again ready to just run git format-patch . 此外,如果自从您开始所有这些工作以来他们已经更新了分支,则可以再次运行git fetchgit fetch他们的更新,然后git rebase您的补丁分支以根据他们的更改进行调整,以便您再次准备好运行git format-patch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM