avp::ptr weblog

Everything that is in our mind.

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

without 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 , , , ,

Leave a Reply