Git rebase tricks

git log –oneline + git rebase –interactive

So there I was, wanting to insert a dozen or so commits from a particular branch on top of another.

Aside: this was the result of a long-running branch that had been rebased to master several times, resulting in branch.5, branch.6 and so on. So, I wanted a few changes from a branch that was based on branch.5 (say) on top of a newer branch based on branch.17 or so – but re-basing from branch.5 to branch.17 caused a bunch of conflicts. So, I could just git cherry-pick them onto the branch … but …

I think I first read about git rebase --interactive in this blog post where this mode is praised as “completely insane and quite dangerous but capable of exposing entirely new states of mind.”

When you run it, you are given an editor (which is in fact editing .git/rebase-merge/git-rebase-todo) which looks something like this:

pick 178e18a some commit mesage
pick 737bce0 another commit message
# Rebase a8aea9f onto 1d32b62
#
# Commands:
#  p, pick = use commit
..

If you do git log --oneline you get something pretty similar looking:

ea05563 another commit message
f8cb166 some commit message

(Note that one is in the reverse order of the other). In any case, it turns out you can just put in extra commits to the git-rebase-todo list! So, you can take the output of git log --oneline --reverse ..., add pick in front of each line and paste it into the editor you get from git rebase --interactive!

So, fun party trick! …and actually quite useful if you want to cherry-pick a bunch of commits from another branch but not type git cherry-pick .. a lot.

Update May 18, 2016: Andrew writes to point out:

Nice! To apply a series of commits from a different branch onto
the current one, I often do:

    git format-patch --stdout master..feature2  | git am -

Andrew’s solution is nice (and way simpler) if you want all the commits off another branch. In my case, I didn’t want all of them…