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

git - apply a commit on another branch to the working copy

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

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

这可能吗?我该怎么做?

我想分享这与我之前的问题有关但特定于工作副本:

您仍然可以使用 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

Apply the change introduced by the commit(s) at the tip of the master branch and create a new commit(s) with this change.

... 的语法是一个提交范围。抓取从开始(排除)到最后一个的所有提交。如果您希望单个提交使用单个 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 documentation for all the options you can use