Selective import of SVN branches into a git/git-svn repository

Use Case

You are using Git and git-svn on a big project which has lots of branches and tags but you only need to work with a small selection of those and you don’t want to import all of the rest.

Import an individual SVN branch

1) Define the new branch in .git/config :

[svn-remote "release-branch"]
        url = svn+ssh://xxxx@mono-cvs.ximian.com/source/branches/mono-2-2/mcs
        fetch = :refs/remotes/git-svn-release-branch

2) Import the SVN branch. SVN_BRANCHED_REVISION is the the revision when the branch happened in SVN.

[~]$ git svn fetch release-branch -r SVN_BRANCHED_REVISION

3) Hook up a local Git branch to the remote branch:

[~]$ git branch --track release git-svn-release-branch

5) Checkout and update

[~]$ git checkout release
[~]$ git svn rebase

Done!

Delete the branch checkout

The following command sequence will delete (locally only) the SVN branch and its history:

[~]$ git branch -D release
[~]$ git branch -D -r git-svn-release-branch
[~]$ rm -rf .git/svn/git-svn-release-branch
[~]$ git gc

Now what?

Well, for starters given that you are ready to commit something to trunk and you also want to “backport” to the release branch you just have to do:

[~]$ git svn dcommit
[~]$ git checkout release && git svn rebase
[~]$ git cherry-pick master && git svn dcommit
[~]$ git checkout master
  • Bob

    I shopped around for quite a while looking for a net branch opportunity. Opening up an Indiana mortgage net branch was my ultimate goal. After getting several recommendations and wooing from various companies, I settled on Top Flite Financial and I couldn’t be happier (http://www.tffinc.net). I’m now my own loan officer. I set my own hours, and personally couldn’t be happier with my choice.

  • http://www.ripsdomain.com tony petruzzi

    i have been searching for this answer for quite some time and even asked on stackoverflow. thank you. i only wish they would add something to do this manually to git-svn.

  • http://www.ripsdomain.com tony petruzzi

    i have been searching for this answer for quite some time and even asked on stackoverflow. thank you. i only wish they would add something to do this manually to git-svn.

  • Dan

    This is amazing! Thanks for posting this, it's resolved what I've spent hours trying to figure out how to do.

    Dan.

  • Mashrur Mia

    yeap that worked like charm!

    me being a super noob, tried too many things to initialize my git repo — all I needed was git init – then all your instructions!

  • Anonymous

    Thank you, thank you, thank you. You saved my life. I was trying almost all git svn commands without success. Now I managed to link an existing svn repo to my git repo.

  • http://twitter.com/JensEggersZA Jens Eggers

    Awesome! Thanks.

  • Ondrej Platek

    Hi,

    I think in step 3)

    git branch –track release git-svn-release-branch

    there should be just

    git branch –track release git-svn-release

    At least that worked for me with git 1.7.9.5

  • skarootz

    Apparently “git branch –track release git-svn-release-branch” won’t work from GIT v1.8.3.2, see here http://stackoverflow.com/questions/19712735/git-svn-cannot-setup-tracking-information-starting-point-is-not-a-branch

    • http://ivanz.com/ Ivan Zlatev

      git.exe branch –set-upstream-to=origin/git-svn-release-branch release