59

After cloning an SVN repository using git-svn with the -s option (git svn clone http://server/repo -s), how does one create a branch or tag and have pushed to the relevant branch/tag directory in the repository when dcommiting?

For instance; if I were to use git to create a foobar branch locally (git checkout -b foobar) how can I have git-svn create the branch on the server (http://server/repo/branches/foobar)?

I'm using Git 1.5.5.6.


Please Note:

The accepted method below does not work with Git 1.5.5.6 as there is no git svn branch method. I'm still looking for a solution to this that doesn't involve resolving to working with svn directly.

Phillip B Oldham
  • 18,807
  • 20
  • 94
  • 134

3 Answers3

74

You can read all the nitty-gritty details in this tutorial, but the gist is basically the following:

$ git svn branch -m "Topic branch" my_topic            # Create SVN branch called "my_topic"
$ git checkout --track -b my-topic remotes/my_topic    # Create the Git branch for "my_topic"
# Hack hack hack...
$ git svn dcommit --dry-run    # Make sure you're committing to the right SVN branch
$ git svn dcommit              # Commit changes to "my_topic" branch in SVN
mipadi
  • 398,885
  • 90
  • 523
  • 479
  • This **doesn't** work with my git version of 1.5.5.6 - there is no such command `git svn branch`. – Phillip B Oldham Apr 15 '10 at 11:31
  • 10
    `git svn branch` was added in v1.6.1. I'd recommend upgrading, as 1.5.5.6 is about a year and a half old. – mipadi Apr 15 '10 at 13:13
  • how to use it with svn tag. I create svn tag with `git svn tag -m "tag description" 0.0.2`. And now I would like to set appropriate tag in git. How I should do this? Thanks – yas375 Dec 15 '11 at 15:50
  • 2
    It's also good practice to check that your local branch points to the right remote branch after you create it, by doing `git svn info`. – XåpplI'-I0llwlg'I - Jan 17 '13 at 21:52
  • 4
    FYI, from GIT v1.8.3.2 the second command no longer works. You can no longer track SVN branches using this syntax, see the following question for more info: [git-svn: Cannot setup tracking information; starting point is not a branch](http://stackoverflow.com/questions/19712735/git-svn-cannot-setup-tracking-information-starting-point-is-not-a-branch) – fvdnabee Sep 18 '14 at 09:11
  • @mipadi: do these commands work correctly after creating a local git branch and hacking in it for a while? If so, could you please add those steps to remove all doubt? – Jim L. Feb 08 '15 at 04:16
  • I had to change the second command to `git checkout -b my-topic origin/my_topic`. Not sure if this is because _remotes_ is only meant to be placeholder. – Christoph Böhme Sep 24 '20 at 08:00
  • @ChristophBöhme: `remotes` is just a placeholder for the name of the remote. – mipadi Sep 25 '20 at 03:27
60

If you created your local branch before the subversion branch existed and you now want to push your local branch into a subversion branch, you can do the following:

Obtain the svn branch revision assigned to the local branch

$ git svn info

from the output, URL field would be the current svn branch path and Revision field would be the subversion revision number

Create the svn branch from the revision that you created your local branch

$ svn cp http://svn-repo/my_app/trunk@123 http://svn-repo/my_app/branches/feature1

Fetch the new svn branch so that your git repo knows about it

$ git svn fetch

The svn branch should now be added as a remote in your git repo $ git branch -a * feature1 master remotes/feature1

At this point your remote will still be trunk. You need to point your local branch to the new remote branch. You can do this by rebasing your local branch from the remote branch: $ git rebase remotes/feature1

Now that your local branch refer to your remote branch, you can commit your changes onto it. First do a dry run so you are confident that your changes will go into your remote branch: $ git svn dcommit --dry-run Commiting to http://svn-repo/my_app/branches/feature1

Now you may commit changes to your remote branch $ git svn dcommit

Most how-tos will tell you to branch subversion first and then create a local branch which tracks the remote branch. But I often don't decide ahead of time whether my local branch should track a remote branch. Often I branch locally, and make changes without any intention of pushing to a remote branch. If I later decide to commit my local branch into a remote branch I perform the steps above.

Oleg Kokorin
  • 2,288
  • 2
  • 16
  • 28
pestrella
  • 9,786
  • 4
  • 39
  • 44
  • 3
    @unpluggd: I think this should be the accepted answer because it actually answers the question asked. That is, unless the accepted answer assumes a local branch has been created and hacked on before typing those commands. – Jim L. Feb 08 '15 at 04:17
  • I dont see the new branch when running `git svn fetch` – Ari M Nov 08 '15 at 13:25
  • @SnapDragon you may have to do `git svn fetch --all` in order to see all remote branches (including the svn branch). – pestrella Nov 19 '15 at 09:26
  • This is the definitive answer. Thanks a lot! – astrojuanlu Sep 01 '16 at 14:40
  • Do you have to do a normal `git commit` before doing `git svn dcommit`? I'm getting `Cannot dcommit with a dirty index. Commit your changes first, or stash them with 'git stash'. at /usr/lib/git-core/git-svn line 846.` – pretzlstyle Jan 04 '18 at 22:48
  • 1
    The answer to my previous comment is, yes, you do. – pretzlstyle Jan 05 '18 at 15:34
0

I just wanted to point out that you shouldn't rebase onto your recently created branch from stuff you already have in a different git branch. git svn dcommit will then afterwards push to trunk it seems. At least that was a problem for me.

Instead, if you want to pull changes from an old git branch onto this new svn branch, use e.g. cherry-pick.

Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
yngve
  • 1,448
  • 1
  • 13
  • 11
  • I just tried this (merge old_branch into new_svn_branch, then dcommit) and it committed to the correct SVN branch (not trunk). Using msysgit 1.7.9. – florisla Jun 14 '12 at 14:59
  • What I meant was if you have branch1 and branch2 in git, want to make new branch1 in svn, and then in the git branch1 rebase from branch2. dcommitting after that will go to trunk (did for me). Might be that with @pestrella's suggestion this isn't a problem. And in any case it is a minor one. – yngve Jul 02 '12 at 15:57