简体   繁体   English

如何将更改合并到两个分支

[英]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 developgit 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? 但是,在对应用程序进行一些更改之后,如何更新对developfeature-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会找出当前分支, topicmaster之间的祖先点。 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更改:实际上, mastertopic指向同一个提交对象:它们是相同的。 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.

相关问题 如何使 Git 合并操作忽略对两个分支所做的相同更改? - How to make a Git merge operation ignore identical changes made to both branches? 如何在2个分支上工作,然后将更改合并为一个? - How to work on 2 branches an then merge changes into one? 如何合并很少变化的两个分支 - how to merge two branches with few changes not all 我有两个不同的历史分支。 如何从功能分支获取更改并合并到两者? - I have two separate branches with different histories. How do I take changes from a feature branch and merge to both? 手动将相同的更改应用于两个分支时,如何强制变基? - How to force rebase when same changes applied to both branches manually? 合并分支而没有未提交的更改 - Merge branches without uncommitted changes 如何在不合并先前分支的更改的情况下合并分支 - How to merge a branch without also merging changes from previous branches 如何将工作项的更改合并到源分支以外的分支? - How to merge the changes of a Work Item to branches other than the source branch? 如何将具有相关更改的两个分支合并到master中? - How do merge two branches with related changes into master? 如何在git中合并2个分支,仅保留一个分支的更改 - How to merge 2 branches in git keeping the changes only from one branch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM