git - 将另一个分支上的提交应用到工作副本/在分支和存储库之间共享内容

问题描述 投票:0回答:2

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

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

这可能吗?我该怎么做?

我想分享这与我之前的问题有关,但特定于工作副本: git 挑选一个提交到另一个分支

git
2个回答
353
投票

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

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

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

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


cherry-pick
无需承诺

默认情况下

git cherry-pick
提交您的更改,因此,如果您希望精挑细选而不提交所有更改,只需添加
-n
标志

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

git cherry-pick -n <hash>

cherry-pick
合并提交

如果您需要挑选合并而不是提交,请使用

-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> 

阅读完整的

git cherry-pick
文档,了解您可以使用的所有选项


50
投票

您仍然可以使用

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
),但不会提交。

© www.soinside.com 2019 - 2024. All rights reserved.