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

标签 git

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

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

这可能吗?我该怎么做?

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

最佳答案

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

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

enter image description here


cherry-pick无需 promise

默认 git cherry-pick 提交您的更改,因此如果您希望挑选而不提交所有更改,只需添加 -n旗帜

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

git cherry-pick -n <hash>

cherry-pick merge 提交

如果您需要挑选 merge 而不是提交,请使用 -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 - 将另一个分支上的提交应用到工作副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36778375/

相关文章:

git - PowerShell Git diff 从参数传递到参数

git - 使用 Git 检查脏索引或未跟踪的文件

git - 无法添加到存储库

带通配符的 Git rm 找不到现有文件

git - SonarQube 无法从 Jenkins 项目获取 git Blame 信息

git - 删除 N 以外的 git stashes

git - 远程 : Forbidden fatal: unable to access

git - 使用git查找文件特定行首次引入token

git - 使用两个存储库,一个具有 "scrubbed"历史记录

git - 如何使用 Git 建立存储库层次结构?