Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git rebase for migrating several commits?

Tags:

git

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

like image 899
artm Avatar asked Jul 06 '15 07:07

artm


People also ask

Can I rebase multiple times?

Yes, you can rebase more than once. After rebasing, you get a fresh set of commits. These commits are exactly like all other commits and hold no record of having been rebased. The main thing you need to be careful for is the possibility of rebase conflicts.

How can you move from 4 commits to one commit?

You can use git merge --squash to squash the commits into a single one while merging into the branch. All the commits in original-branch will be merged into a single one, and applied to the target-branch . You will still need to do a git commit after the squash merge.

When should you avoid rebase?

If you use pull requests as part of your code review process, you need to avoid using git rebase after creating the pull request. As soon as you make the pull request, other developers will be looking at your commits, which means that it's a public branch.


2 Answers

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
like image 119
Tim Biegeleisen Avatar answered Oct 01 '22 16:10

Tim Biegeleisen


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".

like image 40
VonC Avatar answered Oct 01 '22 15:10

VonC