用Git推送两个分支的提交 [英] Push a commit in two branches with Git
问题描述
我如何在两个分支中推送提交?
我不能使用git push,因为这样它就会推到三个分支,我只想在其中两个分支中进行提交。 b
$ b我在分支B中尝试了git merge HEAD --commit id from branch A--,但是它接受了分支A的所有内容并且与分支B合并。我只想要最后一个提交,而不是所有其他与分支B合并。
任何人都知道该怎么办? / div> 简短回答
您可以使用 cherry-pick
命令,然后使用两个分支 git push origin branchA branchB
。
两个分支可能是有用的
假设您有一个具有这种结构的存储库:
A - B - C - D←master←HEAD
\- -E←v1-release
经过一些开发(提交 A
, B
, C
)项目已发布, v1- release
分支被创建(所以v1可以通过bug修复来支持,下一个版本可以在
master
中开发)。提交 E
用于指定版本信息(添加版本注释等)。提交 D
引入了新功能,该功能计划用于下一个版本,并且不应出现在 v1-release
中。
现在,如果在 v1-release
中发现了一个错误,它必须在两个分支中修复,以便用户可以继续使用v1,并且它不会出现在下一个版本中。
修复了 master
中的错误后,看起来像这样:
A - B - C - D - F←master←HEAD
\\ \\ - E←v1-release
现在提交 提交不能被完全复制(因为提交是一个目录保存状态),但是您可以将提交中所做的更改应用到另一个提交。 在此之后,资料库应该如下所示: 提交 F $ c
。
如何真正做到这一点 h1>
cherry-pick
命令完全如此。它将指定提交所做的更改应用到当前分支,创建新提交:
git checkout v1-release
git cherry-pick F
A - B - C - D - F←master
\\ - - E - G←v1-release←HEAD
G
引入了与<$相同的更改c $ c> F
您可能需要解决冲突(就像合并后一样)。
错误讯息
以前的樱桃选择现在是空的...
blockquote>
表示由cherry-pick提交的更改已经存在于当前分支中。您可能忘记检出正确的分支。
如果发生错误或冲突,可以使用
git cherry-pick --abort
。
最后,您可以返回到
master
分支并将两个分支推送到远程存储库:
git checkout master
git push origin master v1-release
最终版本库结构:
A - B --C - D - F←master←HEAD
\ - E - G←v1-release
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..
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.
Anyone know what to do?
解决方案Short answer
You can apply already existing commit to another branch using
cherry-pick
command, and then push both branches usinggit 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 andv1-release
branch was created (so that v1 can be supported with bugfixes and next version can be developed inmaster
). CommitE
was used to specify version information (added release notes, etc). CommitD
introduced new feature, which is planned for the next version and should not appear inv1-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.After fixing the bug in
master
, repository should look like this:A--B--C--D--F ← master ← HEAD \--E ← v1-release
Now commit
F
with a bugfix must be applied tov1-release
branch.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: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 asF
.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.
In case of errors or conflicts cherry-pick can be aborted using
git cherry-pick --abort
.Finally, you can return to
master
branch and push both branches to remote repository:git checkout master git push origin master v1-release
Final repository structure:
A--B--C--D--F ← master ← HEAD \--E--G ← v1-release
这篇关于用Git推送两个分支的提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!