198

I have a git repository which tracks an svn repository. I cloned it using --stdlayout.

I created a new local branch via git checkout -b foobar

Now I want this branch to end up in …/branches/foobar in the svn repository.

How do I go about that?

(snipped lots of investigative text. see question history if you care)

kch
  • 77,385
  • 46
  • 136
  • 148
  • Related question: [How do I make git-svn use a particular svn branch as the remote repository?](http://stackoverflow.com/questions/192736/how-do-i-make-git-svn-use-a-particular-svn-branch-as-the-remote-repository) – sleske Sep 09 '12 at 14:39

3 Answers3

287

I know this question has been answered a while ago, but after reading it, I it might help adding examples of the specific git svn branch command and relate it to a typical workflow.

Like kch answered, use git svn branch. Here is a full example, (note the -n for dry-run to test):

git svn branch -n  -m "Branch for authentication bug" auth_bug

If this goes well, server replies with answer like this:

Copying https://scm-server.com/svn/portal/trunk at r8914 to https://scm-server.com/svn/portal/branches/auth_bug...

And without the -n switch the server probably adds something like:

Found possible branch point: https://scm-server.com/svn/portal/trunk => https://scm-server.com/portal/branches/auth_bug, 8914

Found branch parent: (refs/remotes/auth_bug)

d731b1fa028d30d685fe260f5bb912cbf59e1971

Following parent with do_switch

Successfully followed parent r8915 = 6ed10c57afcec62e9077fbeed74a326eaa4863b8

(refs/remotes/auth_bug)

The best part of it, now you can create a local branch based on your remote branch like so:

git checkout -b local/auth_bug auth_bug

Which means "check out and create local branch named auth_bug and make it follow the remote branch (last parameter) auth_bug

Test that your local branch works on that remote branch by using dcommit with --dry-run (-n):

git svn dcommit -n

And SVN server should reply with the new branch name:

Committing to https://scm-server.com/svn/portal/branches/auth_bug ...

Jesper Rønn-Jensen
  • 106,591
  • 44
  • 118
  • 155
  • "git co" doesn't work on my version. I used "git checkout" instead. – Chris Conway Apr 08 '10 at 22:04
  • 2
    Yeah, git co means that Jesper set up a git alias. – Jason Axelson May 04 '10 at 00:21
  • 3
    updated and replaced `git co` with `git checkout` so that the example can work for anybody – Jesper Rønn-Jensen Oct 15 '10 at 19:53
  • 36
    There is a tricky part in `git-svn branch` command — you have to be online in order to create a new branch this way. You can do the following in offline: 1. `git checkout -b foobar` 2. hack-hack-hack 3. `git commit -m "Done foobar"`. And when online push this change by doing 1. `git svn branch foobar` 2. `git branch --set-upstream foobar remotes/foobar` 3. `git svn dcommit`. – vadishev Feb 17 '12 at 22:01
  • Thanks! Also if you got other error message, please check [multiple branch paths...][http://stackoverflow.com/questions/2974016/git-svn-branching] – primeminister Mar 27 '12 at 14:26
  • You should replace the -n in the first call with --dry-run to be more clear that it's just a test. I missed that -n meant dry-run so I wondered why creating a local branch didn't work at first. Great example otherwise! :) – Joakim Dec 05 '12 at 08:40
  • All the answers here assume a trunk-vs.-branches folder structure. What about a plain svn repo? I tried to set up branches = mybranch/*:refs/remotes/* in .git/config; after that I run the command in the above answer and things seem to go well until I use git svn dcommit. Then I realized that there are not physical folder created for my branch. What's wrong? – kakyo Oct 03 '13 at 14:51
  • 1
    If you want to create the branch from an older revision than HEAD you can use the `svn copy` command directly instead of `git svn branch`. See http://stackoverflow.com/questions/2152595/git-svn-create-branch-off-past-revision – Bernhard Fürst Mar 19 '14 at 10:18
  • @vadishev answer apparently no longer works as of git 1.8.3.2. http://stackoverflow.com/a/19833311/780194 – jjcf89 May 12 '15 at 15:56
  • 2
    So replace step 2 with `git checkout -b foobar-new remotes/foobar`. 3. `git checkout foobar` 4. `git rebase foobar-new` 5. To test to make sure the branch now commits to the svn branch. `git svn dcommit --dry-run` 6. Finally actually commit `git svn dcommit` 7. Remove temp branch `git branch -D foobar-new` – jjcf89 May 12 '15 at 16:13
  • 2
    Checking out the branch like that didn't work for me, got `Cannot update paths and switch to branch`. But doing: `git branch -b auth_bug origin/auth_bug` worked. – Zitrax Aug 11 '15 at 12:41
  • Yup, I needed to add origin/ as well. I don't remember how I set this up initially. Additionaly, on Windows you can go derp and use a client like SourceTree to click your way around switching an SVN branch. ;) – Pawel Krakowiak May 17 '17 at 12:37
  • Why do you use local/auth_bug to refer to the local branch? Is it a convention to prepend local/? I figured it was to indicate the difference to git, but checking out a local branch by prepending `local/` didn't work out for me. – batbrat Nov 14 '17 at 07:01
  • 1
    @batbrat I just use `local` to indicate the difference, and make it easier to follow along. – Jesper Rønn-Jensen Nov 21 '17 at 22:36
65

as of git v1.6.1, git svn branch is available.

From the git docs:

    branch
        Create a branch in the SVN repository.

        -m, --message
            Allows to specify the commit message.

        -t, --tag
            Create a tag by using the tags_subdir instead of the branches_subdir
            specified during git svn init.

Previous versions of git do not provide a way to create an svn branch.

kch
  • 77,385
  • 46
  • 136
  • 148
4

@kch I just (7 December 2008) compiled the v1.6.1-rc1 tag of git and it does contain the git svn branch command and the documentation for it. So the v1.6.1 release of git should (hopefully) contain this command.

Bryan J Swift
  • 1,449
  • 2
  • 15
  • 17
  • indeed, i installed 1.6.1 now and the command is available. updated my answer accordingly. – kch Jan 01 '09 at 14:00