avp::ptr weblog

Everything that is in our mind.

Git: How to copy a range of commits from one branch to another?

with 6 comments

Sometimes it is useful to copy a series of commit to another branch. This post describes how to do this using git rebase. Two example illustrate different scenarios which should help to understand the command arguments in generell.

Example 1: The commit P, Q, R which are contained in branch feature should be copied to branch master. The commits should be appended to commit M.

git rebase --onto M O R && git rebase HEAD master

The first command git rebase --onto M O R copies the commits P, Q, R to the branch master. They won’t be visible if you check the visual graph in gitk. This is because there is no branch reference that points to one of the three commits. To update the branch master run the second command git rebase HEAD master which moves up the HEAD to commit R.

Example 2: The second scenario is similar. It concentrates on two things: 1) You can choose any destination commit as the root for the copied commits. In this case I have chosen commit L as the root commit. 2) As in the first example you need to apply a branch name to the last commit of the group after you did the actual copy operation in order to let the commits become visible.

git rebase --onto L O R && git branch topic

At the point when you understand the three arguments in general it will be much easier to use this command in various situations. Therefore, memorize the following:

$ git rebase --onto argument1 argument2 argument3
  • argument1 is the destination commit for the copy operation.
    It will be the predecessor of the commit subsequent to argument2.
  • argument2 is the predecessor of the first commit of the range that will be copied.
    argument2 itself will not be copied, though.
  • argument3 is the last commit of the range that will be copied.

Further information can be found on the man page of git rebase.

Written by tobi

September 28th, 2012 at 11:45 pm

Posted in Uncategorized

Tagged with , , , ,

6 Responses to 'Git: How to copy a range of commits from one branch to another?'

Subscribe to comments with RSS or TrackBack to 'Git: How to copy a range of commits from one branch to another?'.

  1. […] Git: How to copy a range of commits from one branch to another? […]

    git rebase a branch since it was created | Ngoding

    20 Nov 14 at 09:07

  2. Thanks a lot! This tutorial is very clear and easy to understand. And it really helps me.

    Mier Ta

    27 Jul 16 at 00:38

  3. This was very useful for me. Thanks for the article
    Before this I tried the following commands which didnt worked

    ## On feature branch
    git rebase –onto master ^

    note the ‘^’ at end of the command


    2 Aug 16 at 19:23

  4. My comment was modified on submission


    2 Aug 16 at 19:24

  5. There might be a mistake in the first example.
    git rebase HEAD master does not move HEAD up to commit R, but rather updates the master to poit at commit R, which HEAD is already doing.


    16 Feb 19 at 11:16

  6. Hi Bulb.

    I cannot confirm that there is a mistake in the first example. I tried it out just now. After the first command the master branch still points to the M commit. You need to shift it to the new R commit.

    Feel free to send your commands step by step – I can certainly take a look if you like.


    24 Feb 19 at 18:46

Leave a Reply