Rebase is one of the most awesome commands in git. Use it to keep the local branch up to date with what’s going on in trunk. A rebase a day keeps merge conflicts away!
In the form advocated here, rebase takes your branch’s local changes and applies them to the most recent code in the master branch. This makes git look as if you made your changes to the latest code, instead of to older code. Everyone else’s changes are incorporated into yours without an explicit merge commit.
Frequent rebasing reduces merge pain by catching conflicts early and in small quantities. Nothing can eliminate merge pain entirely. When conflicts do come up during rebase, git doesn’t make it clear what is going on. To help with this, I turn to my favorite IDE, IntelliJ IDEA. Unfortunately IntelliJ doesn’t make it clear either.
This post steps through rebasing a working branch to bring in the latest changes from master, inside IntelliJ. The merge-conflicts support in IntelliJ is good, but beware! It is not quite what you expect, so watch out, and read on.
Before starting the rebase:
1. Commit all your changes to the local branch. If you’re in the middle of something, commit it anyway. You can tweak that commit history later.
2. Bring the central repo changes into your master branch. (Most people use “git pull.”)
3. Check out your local branch.
In IntelliJ, look in the Changes view, at the Log tab, to see the status of your repository. If it looks like this, with commits on both master and your current local branch, then this post is for you:
To start the rebase within IntelliJ, go to the VCS menu; select Git and then Rebase…
If there are no conflicts, well, lucky you, you’re done. It gets interesting when the “Rebase Suspended” box appears.
Conflicts! Time to resolve them. Note that “(0/0)” here has no meaning. I’ve seen that piece work in an interactive rebase, but not this one.
To resolve the conflict, right-click on the file, choose Git, and then Resolve Conflicts. (Anybody know if there’s a handy button for this?)
Now that you know which changes are which, merge them together and then save. You might see this “File Cache Conflict” dialog pop up. The changes IntelliJ is making through git and the changes it’s making internally are not quite synchronized. You must choose “Keep Memory Changes” in order to retain your merge!
Repeat this process with any other files in conflict. IntelliJ’s conflict resolution automatically adds the file to the git staging area, which is necessary to continue the rebase. Once all conflicting files are merged and in the staging area, continue the rebase. VCS -> Git -> Continue Rebasing.
That’s it for rebasing inside of IntelliJ, as of v11 of the IDE and the bundled Git Integration. If I had infinite time available, I’d love to write another plugin just for rebase, because it’s a very important and very cryptic operation in Git.