如果我想将仅对特定提交中更改的某些文件所做的更改合并到 Git 分支中,其中包括对多个文件的更改,那么如何实现?
假设调用的 Git 提交stuff对文件A, B,进行了更改C,D但我只想合并stuff对文件A和B. 这听起来像是一项工作,git cherry-pick但cherry-pick只知道如何合并整个提交,而不是文件的子集。
stuff
A
B
C
D
git cherry-pick
cherry-pick
我会用cherry-pick -n( --no-commit) 来做,它可以让你在提交之前检查(和修改)结果:
cherry-pick -n
--no-commit
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
如果绝大多数修改是您不想要的,而不是检查单个路径(中间步骤),您可以重置所有内容,然后添加您想要的内容:
# 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 .