Skip to content

Instantly share code, notes, and snippets.

@trodrigues
Created June 13, 2011 16:51
Show Gist options
  • 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.
@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