简体   繁体   中英

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

I created a new branch from master branch two days ago. 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). Now, I want to move the origin of my new branch to the latest commit in 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.

You need 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.

#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 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:

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. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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