Git-将另一个分支上的提交应用于工作副本

所以我有一个提交,它有一个有用的代码更改,但它在另一个分支上。

我希望将另一个分支中的提交应用于当前分支上的工作副本(而不是作为另一个提交)。

这可能吗,我要怎么做?

我想我应该分享一下这个和我之前的问题有关,但是是关于工作文本的: 从一个树枝摘到另一个树枝

93595 次浏览

您仍然可以使用 git cherry-pick命令。参见 git cherry-pick --help:

   -n, --no-commit
Usually the command automatically creates a sequence of
commits. This flag applies the changes necessary to
cherry-pick each named commit to your working tree and the
index, without making any commit. In addition, when this
option is used, your index does not have to match the HEAD
commit. The cherry-pick is done against the beginning state
of your index.

所以你可以仅仅使用 git cherry-pick -n <commitid>,这些改变会被应用到你的工作目录中,然后在索引中暂存(比如 git -a) ,但是不会被提交。

如何将所需的提交添加到不同的分支中。

git cherry-pick <SHA-1>...<SHA-1> --no-commit

应用主分支顶端的提交引入的更改,并使用此更改创建新的提交。

...的语法是提交范围。抓取从开始(排除)到最后一次的所有提交。如果您希望单个提交使用单个 SHA-1

enter image description here


没有承诺的 cherry-pick

默认情况下,git cherry-pick承诺,因此如果您希望在不提交所有更改的情况下进行筛选,只需添加 -n标志即可

这将允许您检查更改,并在需要时手动提交更改,或者在遇到太多冲突时中止更改。

git cherry-pick -n <hash>

合并提交

如果您需要选择合并而不是提交,可以使用 -m标志

#
# In this case, we select the [1] first parent in the commit
# Use git show <hash> to see a list of available parents
#
git cherry-pick -m 1 <hash>

读出完整的 对于所有可以使用的选项,< em > < strong > git cherry-pick 文档