使用 Git 在两个分支中推送提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4024095/
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
Push a commit in two branches with Git
提问by Madoc
how do I push a commit in two branches?
如何在两个分支中推送提交?
I can't use "git push", because then it pushes to three branches, and i just want the commit in two of them..
我不能使用“git push”,因为它会推送到三个分支,而我只想在其中两个分支中提交..
Ive tried a "git merge HEAD --commit id from branch A--" in branch B, but then it takes everything from branch A and merges with branch B. I just want the last commit and not everything else merged with branch B.
我在分支 B 中尝试了“git merge HEAD --commit id from branch A--”,但是它从分支 A 中获取所有内容并与分支 B 合并。我只想要最后一次提交,而不是其他所有内容与分支 B 合并。
Anyone know what to do?
有谁知道该怎么办?
回答by max
Short answer
简答
You can apply already existing commit to another branch using cherry-pick
command, and then push both branches using git push origin branchA branchB
.
您可以使用cherry-pick
命令将现有提交应用到另一个分支,然后使用git push origin branchA branchB
.
Why pushing a commit in two branches might be useful
为什么在两个分支中推送提交可能有用
Assume you have a repository with this structure:
假设您有一个具有以下结构的存储库:
A--B--C--D ← master ← HEAD
\--E ← v1-release
After some development (commits A
, B
, C
) project was released and v1-release
branch was created (so that v1 can be supported with bugfixes and next version can be developed in master
). Commit E
was used to specify version information (added release notes, etc). Commit D
introduced new feature, which is planned for the next version and should not appear in v1-release
.
经过一番发展(提交A
,B
,C
)项目发布和v1-release
分支产生(使V1可以与错误修正得到支持和下一个版本可以在开发master
)。CommitE
用于指定版本信息(添加的发行说明等)。CommitD
引入了新功能,计划用于下一个版本,不应出现在v1-release
.
Now, if a bug is found in v1-release
, it must be fixed in both branches, so that users can continue using v1 and it does not appear in the next version.
现在,如果在 中发现错误v1-release
,必须在两个分支中修复它,以便用户可以继续使用 v1,并且它不会出现在下一个版本中。
After fixing the bug in master
, repository should look like this:
修复中的错误后master
,存储库应如下所示:
A--B--C--D--F ← master ← HEAD
\--E ← v1-release
Now commit F
with a bugfix must be applied to v1-release
branch.
现在F
必须将带有错误修复的提交应用于v1-release
分支。
How to actually do it
如何实际操作
Commits cannot be exactly copied (since commit is a directory saved state), but you can apply changes made in commit to another commit.
不能完全复制提交(因为提交是目录保存状态),但您可以将提交中所做的更改应用到另一个提交。
cherry-pick
command does exactly that. It applies changes made by a specified commit to the current branch, creating new commit:
cherry-pick
命令正是这样做的。它将指定提交所做的更改应用到当前分支,创建新的提交:
git checkout v1-release
git cherry-pick F
After this, repository should look like this:
在此之后,存储库应如下所示:
A--B--C--D--F ← master
\--E--G ← v1-release ← HEAD
Commit G
introduces same changes as F
.
CommitG
引入了与F
.
You may have to resolve conflicts (exactly like after merge).
您可能需要解决冲突(就像合并之后一样)。
Error message
错误信息
The previous cherry pick was now empty ...
以前的樱桃挑选现在是空的......
means that changes made by cherry-picked commit are already present in current branch. You probably forgot to checkout correct branch.
意味着由cherry-picked commit所做的更改已经存在于当前分支中。您可能忘记签出正确的分支。
In case of errors or conflicts cherry-pick can be aborted using git cherry-pick --abort
.
如果出现错误或冲突,可以使用git cherry-pick --abort
.
Finally, you can return to master
branch and push both branches to remote repository:
最后,您可以返回master
分支并将两个分支推送到远程存储库:
git checkout master
git push origin master v1-release
Final repository structure:
最终存储库结构:
A--B--C--D--F ← master ← HEAD
\--E--G ← v1-release
回答by dakangz
Try this:
尝试这个:
git push origin <commitId>:<brancnName_1>
git push origin <commitId>:<brancnName_2>
It works on my side.
它在我身边工作。