42

I am attempting to use git svn to clone a single directory of a SVN repository into a Git repository.

If I use git svn clone svn+ssh://path/to/repo/trunk/directory, I get a Git repo without branches that mirror the branches in the source SVN repo.

If I use git svn --stdlayout svn+ssh://path/to/repo/trunk/directory, I get an empty Git repo. The following is the output of the command:

Initialized empty Git repository in /directory/.git/
Using higher level of URL: svn+ssh://path/to/repo/trunk/directory => svn+ssh://path/to/repo
W: Ignoring error from SVN, path probably does not exist: (160013): Filesystem has no item: File not found: revision 100, path '/trunk/directory'
W: Do not be alarmed at the above message git-svn is just searching aggressively for old history.
This may take a while on large repositories

I had read that the way to fix the above was to add a revision range like -r 1000:HEAD, this still produces an empty repo. The output is:

Initialized empty Git repository in /directory/.git/
Using higher level of URL: svn+ssh://path/to/repo/trunk/directory => svn+ssh://path/to/repo

Any ideas on how to clone a subdirectory of an SVN repository using git-svn that still grabs all of the branches & tags from the source SVN respository?

oconnor0
  • 3,154
  • 6
  • 34
  • 52

2 Answers2

69

You don't want the standard layout, you want something like this:

git svn clone svn+ssh://path/to/repo/ --trunk=trunk/directory --branches=branches/*/directory --tags=tags/*/directory
Mykola Gurov
  • 8,517
  • 4
  • 29
  • 27
  • 9
    The insight here is that you can use wildcard characters in the `--branches` argument. Good one. – Stephen Harrison May 22 '15 at 14:41
  • 5
    Just a note -- if you don't have branches nor tags for that specific directory then you can safely omit `--branches` and `--tags` parameters. That is just using `--trunk=...` instead of `--stdlayout` should work fine. – Nux Oct 07 '15 at 13:44
  • 4
    Can I do this with multiple `directory` values instead of only one? At best with a list of specific directory names. Background: Splitting up a single SVN repository by sub-directories into multiple Git repositories (while only migrating the history for these sub-directories and not the whole SVN repository). – Patrick Bergner May 03 '18 at 12:49
35

Prepare and enter the local project directory:

mkdir local/project/path
cd local/project/path

Initialize the local git repository with svn remote and fetch the data:

git svn init http://my_svn_server.com/repository/project/location
git svn fetch

Credit goes to Gabriel Saldaña: http://blog.gabrielsaldana.org/using-git-with-subversion-repository-subdirectory/

Dmitry Avtonomov
  • 8,747
  • 4
  • 32
  • 45
  • 2
    Note that this does not allow adding user (authors) mapping file. At least not with `-A`. – Nux Oct 07 '15 at 13:44
  • 1
    This is practically no different than the one-step `git svn clone` usually. – MarkHu Jun 16 '16 at 00:32
  • This did the trick. The other thing that also seems to work is git svn clone --no-metadata... – Warpzit Feb 08 '17 at 12:24
  • 3
    Does that just get you the current contents of the svn project or all the history too ? – PaulNUK Jun 07 '18 at 08:06
  • Daft question, but what do I do after this? I have an empty folder with a .git folder in it. How do I get at the source I checked out? – Steve Smith Mar 29 '22 at 15:55
  • @SteveSmith Try running fetch more than once. Worked for me. Used this method to fetch one single directory from SVN (to be pushed to Git subsequently). The problem however was that the init naturally creates a .git inside the folder and then pushing elsewhere is not possible. So, I deleted the .git and then could push it to my Git Repo (the fetched folder is part of this structure, but was ignored during the first fetch). – soumya Jan 20 '23 at 09:28