如何樱桃只选择只有一个文件的变化,而不是整个提交 [英] How to cherry pick only changes for only one file, not the whole commit
问题描述
我需要将一个分支中引入的更改应用到另一个分支。
我可以使用樱桃选择来做到这一点。但是,在我的情况下,我想要应用只与一个文件相关的更改,我不需要选择整个提交。
如何做到这一点?
基于您想实现的目标,您有不同的选择:
如果您希望文件内容与目标分支上的内容相同,可以使用 git checkout< branch> - < filename>
。然而,这将不会挑选在单个提交中发生的更改,而只是获取所述文件的结果状态。因此,如果您在提交中添加了一行,但之前的提交更改了更多内容,并且您只希望在没有其他更改的情况下添加该行,那么结账不是您想要的。
否则,如果您想要将提交中引入的修补程序应用于单个文件,那么您有多个选项。你可以运行
git cherry-pick -n
,即不需要提交,编辑提交(例如使用 git reset - 。
,并且只使用 git add< filename>
)添加您实际想要更改的文件。或者你可以为文件创建diff,然后应用diff: git diff< branch> ^ ..<分支> - <文件名> | git apply
I need to apply changes introduced in one branch to another branch. I can use cherry pick to do that. However, in my case I want to apply changes which are relevant only for one file, I don't need to cherry pick whole commit. How to do that?
You have different options based on what you want to achieve:
If you want the contents of the file to be the same as on the target branch, you can use git checkout <branch> -- <filename>
. This will however not "cherry-pick" the changes that happened in a single commit, but just take the resulting state of said file. So if you added a line in a commit, but previous commits changed more, and you only want to add that line without those other changes, then a checkout is not what you want.
Otherwise if you want to apply the patch introduced in a commit to only a single file, you have multiple options. You could run git cherry-pick -n
, i.e. without committing it, edit the commit (for example reset all files using git reset -- .
and only add the file you actually want to change using git add <filename>
). Or you could create the diff for the file and apply the diff then:
git diff <branch>^..<branch> -- <filename> | git apply
这篇关于如何樱桃只选择只有一个文件的变化,而不是整个提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!