Skip to content

Instantly share code, notes, and snippets.

@trodrigues
Created June 13, 2011 16:51
Show Gist options
  • Star 90 You must be signed in to star a gist
  • Fork 18 You must be signed in to fork a gist
  • Save trodrigues/1023167 to your computer and use it in GitHub Desktop.
Save trodrigues/1023167 to your computer and use it in GitHub Desktop.
Checkout only certain branches with git-svn
If you want to clone an svn repository with git-svn but don't want it to push all the existing branches, here's what you should do.
* Clone with git-svn using the -T parameter to define your trunk path inside the svnrepo, at the same time instructing it to clone only the trunk:
git svn clone -T trunk http://example.com/PROJECT
* If instead of cloning trunk you just want to clone a certain branch, do the same thing but change the path given to -T:
git svn clone -T branches/somefeature http://example.com/PROJECT
This way, git svn will think that branch is the trunk and generate the following config on your .git/config file:
[svn-remote "svn"]
url = https://example.com/
fetch = PROJECT/branches/somefeature:refs/remotes/trunk
* If at any point after this you want to checkout additional branches, you first need to add it on your configuration file:
[svn-remote "svn"]
url = https://example.com/
fetch = PROJECT/branches/somefeature:refs/remotes/trunk
branches = PROJECT/branches/{anotherfeature}:refs/remotes/*
The branches config always needs a glob. In this case, we're just specifying just one branch but we could specify more, comma separating them, or all with a *.
* After this, issue the following command:
git svn fetch
Sit back. It's gonna take a while, and on large repos it might even fail. Sometimes just hitting CTRL+C and starting over solves it. Some dark magic here.
* After this, if you issue a git branch -r you can see your remote branch definitions:
git branch -r
anotherfeature
* Now you can add a local branch which tracks the remote svn branch:
git branch --track myanotherfeature remotes/anotherfeature
Try not to use the same branch name for the local one if you don't wanna mess it up easily.
@RichardJECooke
Copy link

Thanks so much for this. Exactly what I've been seeking for days.

If you have branches with numbers in your repository after fetching you can safely delete them:
http://stackoverflow.com/questions/11356901/git-svn-clone-spurious-branches

If you have complex paths that git-svn cannot understand then you can use this format instead in your .git/config file:
[svn-remote "svn"]
url = http://server1.com/svn/project
fetch = trunk:refs/remotes/prod
fetch = branches/dev:refs/remotes/branches/dev
fetch = branches/features/feature 1:refs/remotes/branches/feature_1

@Fr0sT-Brutal
Copy link

Thank you for this trick, it saved me from looooong exporting of the whole project

@skyezju
Copy link

skyezju commented May 8, 2015

Thank you! It helps me so much!

@liuanyou
Copy link

liuanyou commented Aug 6, 2015

This is cool, Thank you, very helpful

@whiteboardmonk
Copy link

@atronah
Copy link

atronah commented Feb 14, 2018

the same, but styled

If you want to clone an svn repository with git-svn but don't want it to push all the existing branches, here's what you should do.

  • Clone with git-svn using the -T parameter to define your trunk path inside the svnrepo, at the same time instructing it to clone only the trunk:
    git svn clone -T trunk http://example.com/PROJECT
  • If instead of cloning trunk you just want to clone a certain branch, do the same thing but change the path given to -T:
    git svn clone -T branches/somefeature http://example.com/PROJECT
    This way, git svn will think that branch is the trunk and generate the following config on your .git/config file:
[svn-remote "svn"]
	url = https://example.com/
	fetch = PROJECT/branches/somefeature:refs/remotes/trunk
  • If at any point after this you want to checkout additional branches, you first need to add it on your configuration file:
[svn-remote "svn"]
	url = https://example.com/
	fetch = PROJECT/branches/somefeature:refs/remotes/trunk
        branches = PROJECT/branches/{anotherfeature}:refs/remotes/*

The branches config always needs a glob. In this case, we're just specifying just one branch but we could specify more, comma separating them, or all with a *.

  • After this, issue the following command:
    git svn fetch
    Sit back. It's gonna take a while, and on large repos it might even fail. Sometimes just hitting CTRL+C and starting over solves it. Some dark magic here.
  • After this, if you issue a git branch -r you can see your remote branch definitions:
git branch -r
  
anotherfeature
  • Now you can add a local branch which tracks the remote svn branch:
    git branch --track myanotherfeature remotes/anotherfeature
    Try not to use the same branch name for the local one if you don't wanna mess it up easily.

@mathurk29
Copy link

what is the diff b//w fetch and branches in the config file? I understand both are creating branches in git from the path provided of svn branches?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment