What is GIT Rebase

git ready

Introduction to Rebase

registered on 31 Jan 2009

Git's rebase command is difficult to understand for beginners and the description of the man page doesn't help in the least:

git-rebase - Forward-port local commits to the updated upstream head

Uh, please? That might help if you know how Git works. But not in the slightest if you're just starting out with Git. Thanks to Travis Swicegood, we have a better metaphor:

A hatchet. Rebase helps break up commits and slice them up in the way you want them to be served, and places them exactly where you think they belong. With the command, you can actually rewrite the history of your repository, be it rearranging commits, merging them into larger commits, or ignoring commits if you so choose.

Why is that helpful? One of the most common use cases is working on a specific feature or bug in a separate branch. Instead of creating ugly merge commits for every change that gets pushed into the master branch, you could create a big change and let Rebase handle it all. Another common case for rebase is maintaining your own changes for a project. With merges the repository history is mixed up with upstream commits and own commits. Rebase prevents this and thus keeps the repository in a balanced state. The end of Git for Computer Scientists or James Bowes ’contribution on the topic shows how this looks graphically.

A simple example shows how Rebase works. I create a new branch for my feature, develop it and commit my changes.

$ git checkout -b newfeature Switched to a new branch "newfeature" [.. changed some files, made a few commits ..] $ git checkout master Switched to branch "master" [.. changed one file, committed ..]

The history in the repository looks like this (screenshot from gitx):

Now I want to bring the changes back to the master branch. From there I could merge the changes. If I did that, the commit history would look like this:

Our history would look like this instead:

You can see how everything locks up much cleaner and more compact. The changes were relatively easy, Merge was really easy and didn't require any work. Future tips will cover solving merge problems, the different merge algorithms available and of course the interactive mode of Rebase. This post first explains the basics of the command itself. If you know useful tricks that you can do with Rebase, submit a tip!