问题描述
如果我想合并到一个Git分支中,所做的更改只针对在特定提交中更改的某些文件,其中包括对多个文件的更改,如何实现?
If I want to merge into a Git branch the changes made only to some of the files changed in a particular commit which includes changes to multiple files, how can this be achieved?
假设名为 stuff
的Git提交文件变为 A
, B
, C
和 D
但我只想合并东西
对文件 A
和 B
的更改。这听起来像是一个工作,只是知道如何合并整个提交,而不是一个合并的提交,而不是一个 git cherry-pick
但是> cherry-pick
子文件。
Suppose the Git commit called stuff
has changes to files A
, B
, C
, and D
but I want to merge only stuff
's changes to files A
and B
. It sounds like a job for git cherry-pick
but cherry-pick
only knows how to merge entire commits, not a subset of the files.
推荐答案
我会用 cherry-pick -n
( - no-commit
)它可以让你在提交之前检查(和修改)结果:
I'd do it with cherry-pick -n
(--no-commit
) which lets you inspect (and modify) the result before committing:
git cherry-pick -n <commit>
# unstage modifications you don't want to keep, and remove the
# modifications from the work tree as well.
# this does work recursively!
git checkout HEAD <path>
# commit; the message will have been stored for you by cherry-pick
git commit
如果广阔的大部分修改都是你不想要的东西,而不是检出个别路径(中间步骤),你可以重置所有东西,然后添加你想要的东西:
If the vast majority of modifications are things you don't want, instead of checking out individual paths (the middle step), you could reset everything back, then add in what you want:
# unstage everything
git reset HEAD
# stage the modifications you do want
git add <path>
# make the work tree match the index
# (do this from the top level of the repo)
git checkout .
这篇关于如何git-cherry-pick只更改某些文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!