Vikas Gupta: Software architect

Git Rebase Explained

Posted by Vikas Gupta on November 19, 2012

Using git rebase, you can rewrite the history of a repository in a variety of ways.

rebase takes a series of commits (normally a branch) and replays them on top of another commit (normally the last commit in another branch). The parent commit changes so all the commit IDs are recalculated. This can cause problems for other developers who have your code because the IDs don’t match up.

There’s a simple rule of thumb with git rebase: use it as much as you want on local commits. Once you’ve shared changes with another developer, the headache is generally not worth the trouble.

git rebase takes a branch (the most common use), a tag, or a commit ID to rebase on top of.You can also pass the –rebase option to git pull, causing it to perform a rebase instead of merging the upstream changes into your local branch.

git rebase requires a clean working tree—that is, a working tree with no modified files. If you have changes that you’re not ready to commit, you can stash them until you’re done.

A conflict might arise during the replaying of commits. Like a conflict during a regular merge, a conflict happens when two commits modify the same line of code. git rebase stops when this happens and asks you to fix the conflict and then continue.

You tell Git you’re ready with git rebase –continue.

You can also skip a commit that’s causing a conflict by calling git rebase –skip. That could lead to further conflicts, however.You can abort the rebase too with git rebase –abort.

There’s always a safety net if you need to undo a rebase after it’s completed. Git points ORIG_HEAD at the commit before major changes like git rebase are run.You can use git reset to reset your repository back to that original.

To rebase your current branch against master, use

git rebase master

The following image shows the organization of commits before and after a rebase.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: