【问题标题】:git - apply a commit on another branch to the working copygit - 将另一个分支上的提交应用到工作副本
【发布时间】:2016-08-15 04:08:40
【问题描述】:

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

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

这可能吗?我该怎么做?

我想分享这与我之前的问题有关,但特定于工作副本: git cherry picking one commit to another branch

【问题讨论】:

标签: git atlassian-sourcetree


【解决方案1】:

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

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 documentation for all the options you can use

【讨论】:

  • 谢谢!对于清晰和格式良好的文本,以及迷人的图表。如果你写一本书,我会买它;-)
  • 问题明确提到“不是另一个提交”,而您的回答明确提到“使用此更改创建新提交”。我错过了什么?
  • 您缺少无提交标志
  • 这甚至适用于远程提交,只要您的本地存储库知道它们(执行 git fetch remote 刷新)
  • 这个比官方文档好。谢谢!
【解决方案2】:

您仍然可以使用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 &lt;commitid&gt;,更改将应用​​到您的工作目录并暂存在索引中(如git -a),但不会提交。

【讨论】:

  • 我只是因为图表和附加信息而接受了另一个答案,但您的帖子仍然非常有帮助。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-08
  • 2016-06-04
  • 2016-08-25
  • 2011-02-23
  • 2016-03-23
  • 1970-01-01
相关资源
最近更新 更多