Continuing from my last
article
about git merge vs. rebase, one area merge and rebase greatly differ:
combining work done at the same time using merge and rebase functions.
Basically, merge interleaves work done at the same time. rebase takes
the current branch’s changes and puts them on top.
If you want to follow along or check my work, repository used for this
article is available
here
From the master branch, let’s make a feature_ branch and put
changes to the important file.
Let’s do the same for a fix_1 branch: From the master branch,
create a new branch, but add changes to a file: fix_file.txt
Now let’s add commits to both branches, alternating commits, so they
will be:
commit change 2 to feature_1
commit change 2 to fix_1
commit change 3 to feature_1
commit change 3 to fix_1
Whew, that’s a lot of work! Fixing and writing features at the same
time. :-)
Log: feature_1
This is the log entries for the feature_1 branch:
Yup, pretty straight forward.
Log: fix_1
This is the log entries for fix_1 branch:
Again, straight forward log.
Now, let’s combine the fix_1 branch onto the feature_1 branch. We
want to make sure our feature has all the fixes, right?
Merge fix onto feature
Let’s use the merge method to combine the fix_1 branch onto the
feature_1 branch.
Let’s do this by creating a new branch from the feature_1 branch. By
doing this change on another branch, name: merge_fix_to_feature, we
can analyze the differences better:
log results
Looking at the log results:
So the feature_1 commits and the fix_1 commits are in order with
time.
Let’s see what happens when using a rebase strategy to combine the
branches.
Rebase fix onto feature
Similar to the merge, let’s make a new branch: rebase_fix_to_feature
so we can analyze differences:
Now let’s rebase!
Oh, not even a commit message needed? That was fast & easy.
Let’s look at the log results:
This time, all the feature_1 commits are on top of the fix_1
commits. All the feature_1 commit values are different from the
original:
So, git created new commits for all the work on the feature_1 when
putting the fix_1 work underneath to incorporate the changes.
Graph
One more thing: let’s look at the differences of the log graph
rebase_fix_to_feature
merge_fix_to_feature
Huh?? What’s going on?? The commits look like they are in order.
A merge commit, 02dd, points to fix_1 and feature_1, which shows
their commits together, but in reality, interleaved with each other
when reviewing linearly:
Conclusion
When combining branches, I highlight the difference between the merge
and rebase funtions when making commits simultaneously on branches.
The merge branch function preserves the original commits and order.
The rebase branch function takes changes from the current branch and
puts them on top of the changed branch.
Both methods are valid way to combine changes between branches in
git. Merge produces a history that is accurate with respect to
work. Rebase produces a linear history based on the branch.
I prefer a linear history, so I tend to use rebase over merge when
combining branches.