Geoffrey De Smet
Geoffrey De Smet

Reputation: 27337

Rebase a GitHub pull request on top of my newer local commits

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.

Upvotes: 6

Views: 2138

Answers (3)

Geoffrey De Smet
Geoffrey De Smet

Reputation: 27337

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

Upvotes: 0

CharlesB
CharlesB

Reputation: 90496

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 :)

Upvotes: 7

CharlesB
CharlesB

Reputation: 90496

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.

Upvotes: 1

Related Questions