本文介绍了如何 git-cherry-pick 只更改某些文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想将仅对特定提交中更改的某些文件所做的更改合并到 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 提交对文件 ABC 进行了更改>D 但我只想将 stuff 的更改合并到文件 AB.这听起来像是 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 只更改某些文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 19:54