Jun 1

`git rebase —interactive` to fix merging branch

Consider this scenario: you want to merge a pull request from github, but some of the commits and edits are not related at all.  You are not interested merging them together, so you can’t simply click on the merge button.

I (godfat) ran into this situation.  To fix this, I first checked out to a merging branch:

    git co -b merging

Then I pulled the request as usual:

    git pull git://github.com/bruchu/rest-graph.git

We can start rebasing to fix it:

    git rebase -i master

It’ll open your $EDITOR and showing some nice messages asking you what would you want to do with those commits. Then I put an `e’ in front of the commit which I wanted to edit, and put an `s’ in front of the commit I don’t care at all, but it would be nice if I can preserve the commit message. `s’ means I want to squash this commit into the previous one.

You can also use `edit’ and `squash’ respectively instead of a single letter. But I don’t know why, my $EDITOR, which is vim, would fill the same or similar colors for foreground and background for those words, so that I can’t read them. I would be appreciated if anyone could tell me how to prevent this even when spell checking is turned on. Then I entered `:wq’ to write and quit vim. After closing the $EDITOR, git would show a nice message:

    Stopped at fee1dead... some commit message
    You can amend the commit now, with

        git commit --amend

    Once you are satisfied with your changes, run

        git rebase --continue

Now I can start editing the commit. In effect, just discarded some changes.

    git co master Gemfile
    git co master rest-graph.gemspec

Then follow the instructions:

    git commit --amend
    git rebase --continue

That’s it! Two commits were squashed into one, and unrelated changes were discarded. Actually, I would like to revert this squashed commit, too, because I don’t like this fix, I’d rather fix it by myself, and definitely I want to give the author of the pull request some credits. That’s why I go through all this troubles.

    git revert HEAD

Now add my fixes, add a test case for it, etc, etc… finally merge back to master!

    git co master
    git merge merging
    git push

The downside is that, this won’t be recorded as a merge on github, but this might be a limit given how currently git rebase works. I am not sure though.

Thanks Bruce Chu for the patch!


Lin Jen-Shin (godfat)