8

I want to understand a bit more on git rebase.

Assume I have this workflow, would git rebase be useful here? And if so, what would be the command to migrate commits X to Z (assuming not using cherry-pick) from MASTER to BRANCH.

enter image description here

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
artm
  • 17,291
  • 6
  • 38
  • 54

2 Answers2

3

Believe it or not, you are actually rebasing master on branch !

Here are the commands you could use to achieve this:

git checkout master                  # checkout the master branch
git checkout -b newbranch            # create new branch based on master
git rebase branch                    # rebase on 'branch'

Keep in mind I created a new branch called newbranch which will appear the way you want it. It is up to you as to what you want to do with the original branch. Now newbranch will look like this:

A--B--C--D--X--Y--Z

A more typical workflow would be to bring new changes from master into branch by rebasing the latter on the former, i.e.:

git checkout branch
git rebase master

This would leave branch looking like this:

A--X--Y--Z--B--C--D
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thanks Tim. Assume **branch** is threads1, **newbranch** is threads2. I applied your commands and got the following error - any advice? - sorry I don't know yet how to break the lines in the comment properly. `$ git checkout master Already on 'master' $ git checkout -b threads2 Switched to a new branch 'threads2' $ git rebase origin threads fatal: Needed a single revision invalid upstream origin ` – artm Jul 06 '15 at 07:47
  • Sorry threads is threads1 (original branch)> `$ git rebase origin threads1 fatal: Needed a single revision invalid upstream origin` – artm Jul 06 '15 at 07:51
  • 1
    Thanks Tim. I will confirm your answer soon – artm Jul 06 '15 at 08:35
  • 1
    `git rebase origin/branch` Not sure why, but I needed to add the slash to get it work (otherwise there would be a `fatal: Needed a single revision` error) – artm Jul 07 '15 at 00:05
  • Hi Tim I marked it - thanks. The only thing worth mentioned is that the last command `git rebase origin branch` needs to be changed to `git rebase origin/branch` to work for me. I'm not sure why, please update it you think that is correct. – artm Jul 07 '15 at 00:31
  • Right you are...I removed the reference to `origin`. Actually, you need to specify the name of a branch. So `git rebase branch` is OK and `git rebase origin/branch` is also OK. The former uses the _local_ branch while the latter uses the _remote_. And `git rebase origin branch` doesn't make sense. – Tim Biegeleisen Jul 07 '15 at 00:33
2

If you do a git rebase master (while you have checked out your Branch), you replay the commits of Branch on top of master:

A--x--y--z--b'--c'--d' (Branch)
         |
       (master)

That is helpful in order to make sure the local commit of your Branch are still compatible with the latest evolution from master.
Make sure you haven't pushed your Branch yet, as it changes its history.

See "git workflow and rebase vs merge questions".

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250