git - 将另一个分支上的提交应用到工作副本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36778375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git - apply a commit on another branch to the working copy
提问by Oliver Williams
so I have a commit that has a helpful code change, but on another branch.
所以我有一个提交,它有一个有用的代码更改,但在另一个分支上。
I would like to apply this commit in the other branch to my working copy on my current branch (not as another commit).
我想将另一个分支中的这个提交应用到我当前分支上的工作副本(而不是另一个提交)。
Is this possible? How would I do this?
这可能吗?我该怎么做?
Thought I'd share this is related to my previous question but specific to working copy: git cherry picking one commit to another branch
我想我会分享这与我之前的问题有关,但特定于工作副本: git cherry选择一个提交到另一个分支
回答by CodeWizard
How to add the desired commit(s) into different branches.
如何将所需的提交添加到不同的分支。
git cherry-pick <SHA-1>...<SHA-1> --no-commit
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.
在 master 分支的尖端应用由提交引入的更改,并使用此更改创建一个新的提交。
The syntax of the ...
is a commit range. grab all commits from start (exclude) to the last one. If you want a single commit to use a single SHA-1
的语法...
是提交范围。获取从开始(排除)到最后一个的所有提交。如果您希望一次提交使用单个 SHA-1
cherry-pick
without commiting
cherry-pick
没有承诺
By default git cherry-pick
commityour changes, so if you wish to cherry-pick without committing all the changes simply add the -n
flag
默认情况下git cherry-pick
提交您的更改,因此如果您希望在不提交所有更改的情况下进行挑选,只需添加-n
标志
This will allow you to review the changes and commit them manually if you wish or abort it if you run into too many conflicts.
这将允许您查看更改并根据需要手动提交更改,或者如果遇到太多冲突则中止更改。
git cherry-pick -n <hash>
cherry-pick
a merge commit
cherry-pick
合并提交
In case you needed to cherry-pick a merge instead of a commit, use the -m
flag
如果您需要选择合并而不是提交,请使用-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>
Read out the full git cherry-pick
documentation for all the options you can use
回答by larsks
You can still use the git cherry-pick
command. See git cherry-pick --help
:
您仍然可以使用该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.
So you can just git cherry-pick -n <commitid>
, and the changes will be applied to your working directory and staged in the index (like git -a
), but will not be committed.
因此,您可以git cherry-pick -n <commitid>
,并且更改将应用于您的工作目录并在索引中暂存(如git -a
),但不会提交。