# Moving commits from one branch to another

5 messages from 2013-01-23 to 2013-01-23. Participants: Stefan Schulze, John Keeping, Schulze, Stefan.
Thread: https://gitlist.dev/t/32701

## Stefan Schulze, 2013-01-23 12:04

Subject: Moving commits from one branch to another
Message-ID: <000a01cdf961$bcf773d0$36e65b70$@de>
URL: https://gitlist.dev/e/000a01cdf961%24bcf773d0%2436e65b70%24%40de

```
Hi,

my team uses a central git-repo since >1500 commits and now we have to sync
(only one-way is necessary for now) our repository every three weeks with an
external svn-repo.
I created the new base-directory (incl. trunk/tags/branches) in svn and
added it to my local repo using git svn init && git fetch.
Now I have two branches in my local repository (master and "svnbranch") and
cherry-picked the very first commit from master to svnbranch (it was
probably not necessary), tagged this commit as "publishedToSvn". Now I want
to add all commits publishedToSvn..master onto svnbranch. I didn't managed
to succeed using git-rebase (probably because of the missing common
commits?) and using git grafts / filter-branch modifies my already published
master.

Is there any way to move/copy commits from one branch to another without a
common base-commit and without a forced push of master?

Thanks in advance,
  Stefan Schulze

```

## John Keeping, 2013-01-23 12:12

Subject: Re: Moving commits from one branch to another
Message-ID: <20130123121203.GM7498@serenity.lan>
URL: https://gitlist.dev/e/20130123121203.GM7498%40serenity.lan
In-Reply-To: <000a01cdf961$bcf773d0$36e65b70$@de>

```
On Wed, Jan 23, 2013 at 01:04:03PM +0100, Stefan Schulze wrote:
> my team uses a central git-repo since >1500 commits and now we have to sync
> (only one-way is necessary for now) our repository every three weeks with an
> external svn-repo.
> I created the new base-directory (incl. trunk/tags/branches) in svn and
> added it to my local repo using git svn init && git fetch.
> Now I have two branches in my local repository (master and "svnbranch") and
> cherry-picked the very first commit from master to svnbranch (it was
> probably not necessary), tagged this commit as "publishedToSvn". Now I want
> to add all commits publishedToSvn..master onto svnbranch. I didn't managed
> to succeed using git-rebase (probably because of the missing common
> commits?) and using git grafts / filter-branch modifies my already published
> master.
> 
> Is there any way to move/copy commits from one branch to another without a
> common base-commit and without a forced push of master?

Did you try "git rebase" with "--onto"?  You probably want something
like this:

    git rebase --onto svnbranch publishedToSvn master


John

```

## Stefan Schulze, 2013-01-23 14:13

Subject: AW: Moving commits from one branch to another
Message-ID: <000b01cdf973$cc685fc0$65391f40$@de>
URL: https://gitlist.dev/e/000b01cdf973%24cc685fc0%2465391f40%24%40de
In-Reply-To: <20130123121203.GM7498@serenity.lan>

```
> > Is there any way to move/copy commits from one branch to another
> > without a common base-commit and without a forced push of master?
>
> Did you try "git rebase" with "--onto"?  You probably want something
> like this:
> 
>     git rebase --onto svnbranch publishedToSvn master

I already tried this some days ago, but wasn't sure about the result. The
resulting history looks exactly what I expected, but all the commits are on
master after executing this commands and svnbranch only contains the
original two commits (svn-commit creating the root directory and the
cherry-picked commit from master)

Does the current branch matter if I call git-rebase with the
<branch>-argument?

```

## John Keeping, 2013-01-23 14:49

Subject: Re: Moving commits from one branch to another
Message-ID: <20130123144941.GO7498@serenity.lan>
URL: https://gitlist.dev/e/20130123144941.GO7498%40serenity.lan
In-Reply-To: <000b01cdf973$cc685fc0$65391f40$@de>

```
On Wed, Jan 23, 2013 at 03:13:19PM +0100, Stefan Schulze wrote:
> > > Is there any way to move/copy commits from one branch to another
> > > without a common base-commit and without a forced push of master?
> >
> > Did you try "git rebase" with "--onto"?  You probably want something
> > like this:
> > 
> >     git rebase --onto svnbranch publishedToSvn master
> 
> I already tried this some days ago, but wasn't sure about the result. The
> resulting history looks exactly what I expected, but all the commits are on
> master after executing this commands and svnbranch only contains the
> original two commits (svn-commit creating the root directory and the
> cherry-picked commit from master)

Ah, I missed that you wanted to update svnbranch.  I don't think there's
a single command that will do that, but this should work:

    git rebase --onto svnbranch publishedToSvn master^0
    git checkout -B svnbranch HEAD

This uses a detached head to avoid modifying the wrong branch and then
updates "svnbranch" to point at that after the rebase.

> Does the current branch matter if I call git-rebase with the
> <branch>-argument?

No it will checkout that branch first.


John

```

## Schulze, Stefan, 2013-01-23 15:44

Subject: AW: Moving commits from one branch to another
Message-ID: <1897BAB9F1563C4BBBF69D9E554ECAA50A45C40559@horus>
URL: https://gitlist.dev/e/1897BAB9F1563C4BBBF69D9E554ECAA50A45C40559%40horus
In-Reply-To: <20130123144941.GO7498@serenity.lan>

```
> git rebase --onto svnbranch publishedToSvn master^0
> git checkout -B svnbranch HEAD

Great! This does exactly what I want!

Thanks for your support,
  Stefan Schulze

--------------------------
ckc ag
Sitz:
Am Alten Bahnhof 13
38122 Braunschweig

Telefon +49 (0)531 / 80 110 0
Telefax +49 (0)531 / 80 110 18444
http://www.ckc-group.de

Amtsgericht Braunschweig
HRB 5405

Vorstand:
H.-G. Christian Krentel
(Vorsitzender)
Karsten Kisser

Aufsichtsrat:
Dr. Heinz-Werner Weinrich
(Vorsitzender)
Jens Fokuhl
(stellv. Vorsitzender)
Prof. Dr. Reinhold Haux
Cäsar Jaworski
Dr. Rita Schulz
Thorsten Sponholz
--------------------------

```
