I'm brand-new to rebasing. I'm used to the git-flow method, whereby I branch off our main/develop branch, do my work, open a pull request on github, and it's merged in and we move on. Works well.
Others are doing rebasing however, and I'd like to try it out. I've read up on it (here for instance) and while I get the idea, one bit confuses me.
In git-flow, when I make my changes to my branch, I push that branch to the server and open a pull request. In a rebase, per the link above, I'm told to Periodically rebase your feature branch onto the current state of the destination branch
..
...and if I do that, what do I then push to github? The branch I've based onto, (develop
in my case)? As a git-flow guy that feels weird to me but maybe that's because I'm not used to it.
If there are other ideas on how to mentally shift from git-flow to doing rebasing I'm glad to hear of it as well.
What is git rebase? From a content perspective, rebasing is changing the base of your branch from one commit to another making it appear as if you'd created your branch from a different commit. Internally, Git accomplishes this by creating new commits and applying them to the specified base.
Rebase is one of several Git commands that integrates changes from one branch onto another. (Another command is merge.) Rebase can be a very destructive operation. It literally rewrites Git commit history, which is a big no-no in most cases.
Say your feature branch looks like this
* -- * -- A (master)
\
* -- * -- * (feature)
After some time, others have done work on master
so that the history becomes
* -- * -- A -- B -- C -- D -- E (master)
\
* -- * -- * (feature)
The only thing that rebasing feature
on to master
does is to change when it appears you created the feature
branch:
* -- * -- A -- B -- C -- D -- E (master)
\
* -- * -- * (feature)
By doing this periodically, you resolve any potential merge conflicts before they can pile up. If you merge master into feature periodically, you end up with lots of spurious merge commits in feature
. With rebase, you avoid them, with the price being that you lose sight of when you initially branched feature
from master
. Keep in mind that you should only rebase if you haven't already pushed feature
; once you do, you have shared the history of feature
with others and should not change it.
To periodically rebase your feature branch onto the current state of the destination branch (I'm assuming origin/master in this scenario), just do this from your feature branch:
git fetch origin # Updates origin/master
git rebase origin/master # Rebases current branch onto origin/master
An equivalent alternative:
git pull --rebase origin master
By doing so, you will keep your feature branch up to date with your origin/master branch. You can either merge it into your local master and push into origin/master or you can just push your rebased feature branch to the remote repository and just do a pull request. It really depends on your git workflow.
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