3

I have read the git plain merge fits some cases while git rebase fits other cases when doing merges in Git.

But I don't see how a rebase can be a substitute for a merge. After rebasing a branch it still needs to followed by a merge which is simply more likely to be a fast forward merge. So as I understand Rebase is simply used to "guarantee" a fast forward merge - it is not a substitute for git merge. Correct?

3
  • Relevant: stackoverflow.com/questions/457927/…
    – tcooc
    Commented Feb 18, 2015 at 23:36
  • As I understand from that post it says that a merge needs to be followed by a rebase if you want to merge your feature - so a rebase does not replace a merge.
    – u123
    Commented Feb 18, 2015 at 23:41
  • "Rebase is simply used to "guarantee" a fast forward merge..." Incorrect. It allows for that but also does much more including rewriting history. Commented Feb 19, 2015 at 1:16

4 Answers 4

3

That's basically it (or, at least, one reason to use rebase). It guarantees a fast-forward merge. (I also find it easier to fix conflicts when rebasing as opposing to merging, especially when using git rerere.)

1
  • I does not guarantee "fast-forward merge", but a fast-forward push: in both cases (rebase or merge), you end up with a commit which is a strict descendant of the commit you're merging (or rebasing onto), but if you want your commits to appear upstream you need to push them. If you try to use git merge after a git rebase, it won't be a fast-forward, but a no-op merge. Commented May 7, 2015 at 8:24
1

Note that the snapshot pointed to by the final commit you end up with, whether it’s the last of the rebased commits for a rebase or the final merge commit after a merge, is the same snapshotit’s only the history that is different. Rebasing replays changes from one line of work onto another in the order they were introduced, whereas merging takes the endpoints and merges them together.

The above is a nice summary!

1
  • Exactly. I'd add that git imerge plays on this and allow you to fix conflicts for individual commits like rebase but record the end result like merge for example. Commented May 7, 2015 at 8:22
0

I would say the two things are fundamentally different in their operations and achieve a similar thing.

git rebase puts your list of commits in a branch on top of another branch. So conflict resolution is done per commit in the order you define (if use --interactive) and if you don't have them cleanly orthogonal each your commits can create conflicts (i.e.: commits might not be applicable because each of them is applied individually). Note that you can also merge multiple commits into a single one - so called squashing - while rebasing.

git merge has different strategies and it combines multiple branches. See also the merge help to see that you can choose an automatic conflict resolution strategy (I'll pick a few and paste partial summaries):

  • ours: This option forces conflicting hunks to be auto-resolved cleanly by favoring our version.
  • octopus: This resolves cases with more than two heads, but refuses to do a complex merge that needs manual resolution.
  • subtree: This is a modified recursive strategy. When merging trees A and B, if B corresponds to a subtree of A, B is first adjusted to match the tree structure of A, instead of reading the trees at the same level.

That being said, a rebase is only an option when nobody has seen your changes so far as it will change commit hashes and merging is always applicable. Also note, that if you have a review process rebasing gives you the option to alter your change history to reflect your intentions in the logs.

0

I think this essay from Git Bucket will give you the answer.

Merging vs. Rebasing

The first thing to understand about git rebase is that it solves the same problem as git merge. Both of these commands are designed to integrate changes from one branch into another branch—they just do it in very different ways.

Merging is nice because it’s a non-destructive operation. The existing branches are not changed in any way. This avoids all of the potential pitfalls of rebasing (discussed below).

On the other hand, this also means that the feature branch will have an extraneous merge commit every time you need to incorporate upstream changes. If master is very active, this can pollute your feature branch’s history quite a bit. While it’s possible to mitigate this issue with advanced git log options, it can make it hard for other developers to understand the history of the project.

The major benefit of rebasing is that you get a much cleaner project history. First, it eliminates the unnecessary merge commits required by git merge. Second, as you can see in the above diagram, rebasing also results in a perfectly linear project history—you can follow the tip of feature all the way to the beginning of the project without any forks. This makes it easier to navigate your project with commands like git log, git bisect, and gitk.

But, there are two trade-offs for this pristine commit history: safety and traceability. If you don’t follow the Golden Rule of Rebasing, re-writing project history can be potentially catastrophic for your collaboration workflow. And, less importantly, rebasing loses the context provided by a merge commit—you can’t see when upstream changes were incorporated into the feature.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.