简体   繁体   English

如何将新分支移至master分支中的最新提交?

[英]How to move my new branch to the latest commit in master branch?

I created a new branch from master branch two days ago. 我两天前从master分支创建了一个新分支。 One of my team mates checked in few changes to master branch, instead of the new branch (it should be in both master and the new branch). 我的队友之一对master分支(而不是新分支)进行了少量更改(它既在master分支中又在新分支中)。 Now, I want to move the origin of my new branch to the latest commit in master. 现在,我想将新分支的来源移至master中的最新提交。

I have commits and branches as shown below: 我有提交和分支,如下所示:

                 C4      <-- New branch
                /
C0 - C1 - C2 - C3 - C5   <-- Master

What I want is: 我想要的是:

                     C4  <-- New branch
                     /
C0 - C1 - C2 - C3 - C5   <-- Master

I tried git merge and it didn't work. 我尝试了git merge,但是没有用。

You need git rebase 您需要git rebase

#On New Branch do:
git fetch origin master
git rebase origin/master

An alternate way is to merge master branch into new branch. 另一种方法是将master分支合并到新分支中。

#On New Branch do:
git fetch origin master
git merge origin/master

Rebasing moves a branch from one commit to another. 变基将分支从一个提交移动到另一个提交。 Internally, git creates new commits and applies them to the specified base (it rewrites your project history). 在内部,git创建新的提交并将其应用于指定的基础(它将重写您的项目历史记录)。

git checkout C4
git rebase master

When you'll finish working on your feature, you'll need to merge: 完成功能使用后,您需要合并:

git checkout master
git merge C4

I suggest checking also the interactive rebase, using the -i parameter: 我建议也使用-i参数检查交互式变基:

git checkout feature
git rebase -i master

I generally like to use it to polish a feature branch before merging it into the main code base. 我通常喜欢在将其合并到主代码库之前使用它来完善功能分支。 You can really be sure that everything is in order before committing to the official project history. 在提交正式的项目历史记录之前,您可以真正确保一切正常。 And to other eyes, they will think that you worked on your feature in a wise way. 在其他人看来,他们会认为您以一种明智的方式来完成自己的功能。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM