[英]How to merge changes to both branches
Right now I have a develop
branch, and I create a new branch feature-keyboard
using git checkout -b feature-keyboard develop
as a new version. 现在,我有一个
develop
分支,并使用git checkout -b feature-keyboard develop
作为新版本git checkout -b feature-keyboard develop
了一个新的分支feature-keyboard
。 Right now I need to do changes to both branches. 现在,我需要对两个分支进行更改。 What I am doing right now is I do so:
git checkout -b feature-ui-changes develop
and then add some changes commit them. 我现在正在做的是:
git checkout -b feature-ui-changes develop
然后添加一些更改来提交它们。 then git checkout develop
and git merge --no-ff feature-ui-changes
. 然后
git checkout develop
和git merge --no-ff feature-ui-changes
。 But if I do the same first checkout feature-keyboard
and then merge changes to feature-ui-changes
. 但是,如果我先执行相同的结帐
feature-keyboard
,然后将更改合并到feature-ui-changes
。 It says conflict. 它说冲突。 And It's suppose to.
而且应该如此。 But how do I update changes to both
develop
and feature-keyboard
branches after I do some changes to the app? 但是,在对应用程序进行一些更改之后,如何更新对
develop
和feature-keyboard
分支的更改?
You can do the changes on one branch, commit them. 您可以在一个分支上进行更改,然后将其提交。 Then switch to the other branch and cherry-pick your changes:
然后切换到另一个分支并选择您的更改:
git cherry-pick <commit-hash>
If you find yourself needing to "double commit" numerous changes between two branches, then that is a sign that there is possibly something wrong with your process. 如果您发现自己需要“两次提交”两个分支之间的众多更改,则表明您的过程可能存在问题。 Perhaps the code was branched prematurely.
也许代码过早地分支了。
Git has a nice workflow for creating a "topic" branch where you develop something, while keeping up with the changes from that topic's upstream. Git有一个不错的工作流,可以在您开发某些东西的同时创建一个“主题”分支,同时跟上该主题上游的更改。 Namely, you can use
git rebase
to rewrite the branch history, and migrate the changes to a newer version of upstream. 即,您可以使用
git rebase
重写分支历史记录,并将更改迁移到上游的较新版本。
This saves you the pain of doing double commits, and also prevents you from forking two copies of each upstream commit. 这为您减轻了两次提交的麻烦,也避免了您为每个上游提交分叉两个副本。
$ git checkout -b topic
# ... hack, commit, hack, commit, ...
$ git checkout master
# ... pull, hack, commit, pull, ...
Now there is all kinds of new stuff on master
not reflected in topic
: changes you have made, plus possibly upstream changes pulled in from another repo. 现在,
master
上出现了各种各样的新东西,它们没有反映在topic
:您所做的更改,以及可能从另一个存储库引入的上游更改。 You'd like to return to the work on topic
, but have that work based on the new master
. 您想回到
topic
上的工作,但可以根据新的master
。 That's what rebasing is about: 这就是重新定级的意义:
$ git checkout topic
$ git rebase master
Git will figure out the ancestor point between the current branch, topic
, and master
. Git会找出当前分支,
topic
和master
之间的祖先点。 It will take the topic
changes from that point, and cherry pick them on top of the current master
. 从那时起,将进行
topic
更改,并从当前的master
挑选它们。 The resulting picks will then be installed as the topic
branch. 然后,将产生的选择安装为
topic
分支。 Thereby, the topic
branch is rewritten: it is replaced with a whole new version of that branch. 因此,
topic
分支被重写:被该分支的全新版本替换。 (Along the way, you may have to resolve conflicts, of course.) (当然,一路上,您可能必须解决冲突。)
If you have two or more such topics, you can treat them independently: rebase each one of them as you return to it, keeping it up to date with the master changes. 如果您有两个或两个以上这样的主题,则可以独立对待它们:在返回主题时将它们中的每个主题重新设置基础,并与主要更改保持最新。
The nice thing after rebasing is that after topic
is rebased, it contains the upstream branch (such as master
) as a suffix: it has exactly all the commits that are in master
, plus some new commits. 变基后的好处是,
topic
重新设置后,它包含后缀(例如master
)作为后缀:它具有master
所有提交,以及一些新的提交。 At that point, you can do: 此时,您可以执行以下操作:
git checkout master
git reset --hard topic # fast-forward master to the topic
Now master
has all the topic
changes: in fact, master
and topic
point to the same commit object: they are identical. 现在
master
拥有所有topic
更改:实际上, master
和topic
指向同一个提交对象:它们是相同的。 We can do this safely because master
doesn't have any commits which are not already in topic
, thanks to the recent rebase. 我们可以安全地进行此操作,因为由于最近的重新部署,
master
没有任何不在topic
提交。 So we are not throwing away anything from master
: it just jumps forward. 因此,我们没有抛弃任何
master
东西:它只是向前跳。
If master
does have some new commits, you can also do an "other-way rebase": 如果
master
确实有一些新的提交,您还可以进行“其他方式的变基”:
# on master
git rebase topic # same as git reset --hard topic if master has no new commits!
The new changes on master
are rewound, then the topic
changes are brought in, and the new changes are re-played (cherry picked) on top. 放回
master
上的新更改,然后引入topic
更改,然后在顶部重新播放(选择樱桃)新更改。 It's a mirror image of rebasing topic
to master
: rebase
doesn't care which branch is the trunk and which is the topic. 这是重新
master
topic
的镜像: rebase
不在乎哪个分支是主干,哪个主题。
However, if those new master
commits are public, you are writing public history by rebasing them over topic
changes. 但是,如果这些新的
master
提交是公开的,那么您将通过基于topic
更改重新定义公共历史来编写公开历史。 You can get away with this if all the new master
commits not on topic
are your own and unpublished (you made them locally and haven't git push
-ed them to another repo), or you have some other justification for rewriting master
history even if it is published. 你可以摆脱这一点,如果所有的新
master
承诺不会在topic
是你自己的和未发表的(你让他们在本地并没有git push
他们-ed到另一个回购),或者你有一些其他的理由重写master
的历史甚至如果已发布。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.