Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the advantages of a rebase over a merge in git?

Tags:

In this article, the author explains rebasing with this diagram:

enter image description here

Rebase: If you have not yet published your branch, or have clearly communicated that others should not base their work on it, you have an alternative. You can rebase your branch, where instead of merging, your commit is replaced by another commit with a different parent, and your branch is moved there.

while a normal merge would have looked like this:

enter image description here

So, if you rebase, you are just losing a history state (which would be garbage collected sometime in the future). So, why would someone want to do a rebase at all? What am I missing here?

like image 258
Lazer Avatar asked May 20 '10 17:05

Lazer


People also ask

Which is better rebase or merge?

For individuals, rebasing makes a lot of sense. If you want to see the history completely same as it happened, you should use merge. Merge preserves history whereas rebase rewrites it . Rebasing is better to streamline a complex history, you are able to change the commit history by interactive rebase.

What is the difference between Git rebase and Git merge?

Git Merge Vs Git Rebase: Git merge is a command that allows you to merge branches from Git. Git rebase is a command that allows developers to integrate changes from one branch to another. In Git Merge logs will be showing the complete history of the merging of commits.

Why we use rebase in Git?

The primary reason for rebasing is to maintain a linear project history. For example, consider a situation where the main branch has progressed since you started working on a feature branch.

What is the difference between a Git rebase and merge and when would you do one over the other?

Git rebase and merge both integrate changes from one branch into another. Where they differ is how it's done. Git rebase moves a feature branch into a master. Git merge adds a new commit, preserving the history.


1 Answers

There are variety of situations in which you might want to rebase.

  • You develop a few parts of a feature on separate branches, then realize they're in reality a linear progression of ideas. Rebase them into that configuration.

  • You fork a topic from the wrong place. Maybe it's too early (you need something from later), maybe it's too late (it actually applies to previous versions as well). Move it to the right place. The "too late" case actually can't be fixed by a merge, so rebase is critical.

  • You want to test the interaction of a branch with another branch, but for some reason don't want to merge. For example, you might want to see what conflicts crop up commit-by-commit, instead of all at once.

The general theme here is that excessive merging clutters up the history, and rebasing is a way to avoid it if you didn't get your branch/merge plan right at first. Too many merges can make it hard for a human to follow the history, and also can make it harder to use tools like git-bisect.

There are also all the many cases which prompt an interactive rebase:

  • Multiple commits should've been one commit.

  • A commit (not the current one) should've been multiple commits.

  • A commit (not the current one) had a mistake in it or its message.

  • A commit (not the current one) should be removed.

  • Commits should be reordered (e.g. to flow more logically).

While it's true that you "lose history" doing these things, the reality is that you want to only publish clean work. If something is still unpublished, it's okay to rebase it in order to transform it to the way you should have committed it. This means that the final version in the public repository will be logical and easy to follow, not preserving any of the hiccups a developer had along the way.

like image 179
Cascabel Avatar answered Oct 30 '22 20:10

Cascabel