Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When would one need git-rebase?

Everytime I read the git-rebase documentation, I get lost. It feels to me like a kind of a low-level operation (read: dark magic).

Quoting the docs:

Assume the following history exists and the current branch is "topic":

       A---B---C topic
      /
 D---E---F---G master 

From this point, the result of either of the following commands:

git rebase master 
git rebase master topic 

would be:

               A'--B'--C' topic
              /
 D---E---F---G master

The question is: Why would anyone want to do such a thing?

For one thing, it seems to "re-write" history, as if the branch started at a different point; essentially the commit history will be "a bunch of lies".

Another point, it doesn't feel safe. I tried it once, got a ton of conflicts, and all hell broke loose. I don't remember exactly how I resolved that hell, but if I recall correctly, it was on a temporary test branch or something like that.

The other question: Am I missing some really cool/time-saving set of features by not knowing how to utilize git-rebase?

EDIT:

Related question: Undoing a git rebase

like image 429
hasen Avatar asked May 28 '09 20:05

hasen


People also ask

When should we use Git rebase?

In summary, when looking to incorporate changes from one Git branch into another: Use merge in cases where you want a set of commits to be clearly grouped together in history. Use rebase when you want to keep a linear commit history.

Why do I need rebase?

Use rebase to catch up with the commits on another branch as you work with a local feature branch. This is especially useful when working in long-running feature branches to check how your changes work with the latest updates on the master branch.


2 Answers

First, there are no unsafe operations in git. rebase has an abort operation, and all operations make it to the reflog, so you can undo anything. It is, in fact, quite the opposite.

It allows you to feel free to commit any time you want without having to have a "good" build while you're on the path to making one. The revisions you publish can be clean by squashing all of the steps you took along the way into a single commit.

I use rebase all the time (quite often via pull which I generally have configured to rebase after the fetch phase). Don't think of it as rewriting history -- think of it as providing a tool for you to clean up your rough draft before you publish it.

In a year from now, will it be important to anyone in your project to know that you really started this feature against revision E and not revision G?

Unnecessary recursive merges obscure the more important parts of history.

like image 113
Dustin Avatar answered Sep 26 '22 03:09

Dustin


You need to use it for instance when you want to submit a patch to code that someone else's modified. For example if you branched from revision 1.56 of a software and in the meantime the maintainer moved to revision 1.57, he/she would probably accept patches only on revision 1.57.

You would rebase your branch to revision 1.57, correct all conflicts, verify and resubmit the patch.

like image 35
Metiu Avatar answered Sep 27 '22 03:09

Metiu