Search code examples
gitrebase

Pull with rebase up to a specific commit


This is a variation on an older question with a rebase twist.

I want to do a git pull --rebase but only till a specific commit. This is not pulling a specific commit, this is pulling upto a specific commit. The remote master looks like the following.

A<-B<-C<-D<-E<-F<-HEAD (Remote master HEAD)

Suppose my local feature branch HEAD points to G which points to D:

A<-B<-C<-D<-G<-HEAD (Current local feature branch HEAD).

I want to pull up to E with a rebase so that my branch ends up looking like:

A<-B<-C<-D<-E<-G<-HEAD (local feature branch end goal).

However, this is just a special case. I want to pick any eligible commit hash, not just the second to last one as in the example above.

Naturally, I would like the hash for commit E to match remote master at the end of the operation. I belabor that point because certain types of interactive rebase editing would cause that property to disappear.

What should I do?


Solution

  • Fetch the changes from the remote:

    git fetch origin
    

    Rebase onto the remote version of master, ignoring some number of commits:

    git rebase origin/master~<n>
    

    where <n> is the number of commits from the tip of master you want to ignore.

    If you have the id of the commit you want to rebase onto, you can use that instead:

    git rebase <commit-id>