简体   繁体   中英

Git - Rebasing command when remote branch has been updated

I am trying to understand what's the correct method to rebase my changes in Git

1) I have a master branch

2) From master, I've created a featureX branch - created from JIRA.

3) I checked out that branch using git checkout featureX . It says that it's set up to track the remote branch featureX - which is correct. Any push I do on this branch will be tracked @ my remote branch.

   x1-----x2------x3-----x4   master
   |
   |
   |
   y1-----y2------y3          featureX

4) My feature branch has y1, y2, and y3 commits (not pushed to remote yet). But since I checked out featureX , my master branch has had othe commits pushed/merged into it from whoever else is working. So I want to rebase my changes by pulling all latest stuff from master into my featureX branch.

This is where I need to get the clarification.

1) if I do git checkout featureX followed by git rebase master , does that automatically pull the latest changes from master ? I think it only pulls the changes which are on my local master branch ie the remote master updates won't be there (unless i've updated my local master )

git checkout featureX
git rebase master   # which master? my local `master` or remote `master`?

2) If my assumption is correct, should I always do the following?

git checkout master
git pull --rebase
git checkout featureX
git rebase master

git rebase master is for local master .

if you want to rebase to remote master, use origin/master where origin is remote name, and origin/master is your local snapshot from remote, you should check that it is synced with remote before you do rebase by using git pull/fetch , no need to check out to master and then check back to featureX though.

So your assumption is correct.

Actually you can rebase to any ref rather than branch name, like commit id or tags.

git checkout featureX
git rebase master   # which master? my local `master` or remote `master`?

--> bring x2, x3, x4 of branch master to y1 of branch featureX

git checkout master
git pull --rebase
git checkout featureX
git rebase master

--> You shouldn't do like this. git pull --rebase . you try to bring changed points from A to B, then try to bring changed points from B to A. It doesn't like good practices.

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