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


77

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

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

这可能吗?我该怎么做?

以为我愿意与他人分享这与我之前的问题有关,但特定于工作副本: git cherry选择一个提交到另一个分支


Answers:


127

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

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

在master分支的顶端应用由提交引入的更改,并使用此更改创建一个新的提交。

的语法为...提交范围。抓取从开始(排除)到最后一个的所有提交。如果您希望一次提交使用单个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可以使用的所有选项


3
谢谢!用于清晰,格式正确的文本以及迷人的图表。如果您写书,我会买的;-)
Spike0xff

1
该问题明确提到“不是另一个提交”,而您的答案明确提到“使用此更改创建新的提交”。我想念什么?
AMN

3
您缺少无提交标志
CodeWizard

只要您的本地存储库了解远程提交(执行agit fetch remote刷新),它甚至可以用于远程提交
sberder

23

您仍然可以使用该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),但不会提交。


2
我仅因为该图和其他信息而接受了另一个答案,但是您的帖子仍然非常有帮助。谢谢!
奥利弗·威廉姆斯
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.