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
.
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.
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.
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.
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
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With