6

Suppose I am locally on the branch master of our blessed repository. Someone has sent in a pull request.

How do I apply the commits of that pull request on top of my local branch - as if those commits were rebased on my branch - in a single command?

Note: the pull request is several days old, and my local branch has new commits since the pull request has been created.

CharlesB
  • 86,532
  • 28
  • 194
  • 218
Geoffrey De Smet
  • 26,223
  • 11
  • 73
  • 120
  • I also asked a [similar question here](http://stackoverflow.com/questions/17182624/contributing-to-project-on-github-how-to-rebase-my-pull-request-on-top-of-mast) which may help out someone with the same problem – fontno Sep 08 '13 at 22:18

3 Answers3

7

There's a great blog post on the subject, from Igor Zevaka

You first have to fetch the hidden pull request ref, and place it under the pr/NNN remote branch:

git fetch origin refs/pull/1234/head:refs/remotes/pr/1234

(replace 1234 by the pull request number)

After this it's just a classical rebase: checkout the pull request, and rebase it on your master branch:

git checkout pr/1234
git rebase master

or alternatively

git rebase master pr/1234

You might of course see conflicts.

Now the pull request is rebased on master, and is the current branch. If you want to update master just merge the pull request (we say --ff-only because we know it'll be a fast-forward merge):

git checkout master
git merge --ff-only pr/1234

To have a one-liner some shell aliasing is required, it's just a matter of scripting :)

CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • 1. when `pr_1234` is checked out, doing `git rebase master` will rebase `pr_1234` on `master` (see http://git-scm.com/docs/git-rebase) – CharlesB Jul 11 '13 at 16:52
  • 2. because I'm not sure if a remote branch with slashes can be checked out as-is, and the original blog post does it this way – CharlesB Jul 11 '13 at 16:52
  • I've been away from git for too much time! of course after rebase `master` is still an ancestor of the pull request... I prefer the merge command, more intuitive even if the result is the same (also I upvoted one of your answers) – CharlesB Jul 11 '13 at 18:49
  • 1
    That's how I do it now - manually. *It strikes me as a very common scenario. It's odd that there's no git command to do this out-of-the-box.* – Geoffrey De Smet Jul 15 '13 at 09:02
1

OK, this one-liner will rebase your commits on top of the pull request (which is the inverse of what you want; see my other answer):

git pull --rebase origin pull/NNN/head

Where NNN is the pull request number. This assumes origin is your Github remote on your local repo, and master is checked out.

Explanation here on Douglas Stuarts' blog: Github stores pull requests from other forks in your origin repo, under pull/NNN. This is what allows to run pull --rebase on it.

CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • 1
    hah (/me deletes answer) - I wasn't at a machine yesterday to verify that works. That'll put the "local branch has new commits" on top of the pr commits - it's not clear from the question if that's fine or not. +1. – AD7six Jul 12 '13 at 08:29
  • 1
    Yeah, I don't think it's what the OP was looking for, but it is a clever answer. – John Szakmeister Jul 12 '13 at 09:04
  • Indeed the inverse of what I want, but interesting side note none the less :) – Geoffrey De Smet Jul 15 '13 at 08:58
0

Install hub

# directly apply all commits from a pull request to the current branch
$ git am -3 https://github.com/github/hub/pull/134

TODO: need to figure out how to force a --rebase

Geoffrey De Smet
  • 26,223
  • 11
  • 73
  • 120