Skip to content

Instantly share code, notes, and snippets.

@ticean
Created January 3, 2012 21:14
Show Gist options
  • Save ticean/1556967 to your computer and use it in GitHub Desktop.
Save ticean/1556967 to your computer and use it in GitHub Desktop.
SVN Git Mirror

Create Git Mirror from SVN Repository

This guide will demonstrate how to mirror an SVN into a Git repo. You're the target audience if you're an SVN user, just getting started with Git and need to coax your project team over to Git.

The branching scenario has been simplified for clarity.

References

First off, I'll give some credits. I've lifted all this information from various placed on the web. You should probably follow these links for better instruction. :)

Prerequisites

We'll start assuming that you

  • Have installed Git
  • Have an SVN repo with standard layout (trunk, branches, tags)
  • Can type into a terminal

We'll also assume that SVN trunk is your integration branch, and that SVN branches/production contains the production code.

Creating the Local Git Repository

# Clone an SVN repo with Git
git svn init -s http://svn.yourdomain.com/repos/project_name
cd project_name

# Fetch the SVN branches to the local Git repo
git svn fetch

# Once this is done, you’re local 'master' branch is linked to trunk/.
# Make a new branch for develop & check it out
git checkout -b develop 

# Back to master.
git checkout master

# We'll change this to link local 'develop' branch to trunk
git reset --hard production

If you'd like to see how your branches are organized you can view all branches

# Show current branches
git branch -va

Adding a Git remote

Now we have Git branches properly mapped to SVN branches. But, so far there's only a relationship with the SVN repository. We need to create another relationship to the Git repo.

You can use an existing Git repository, or create a new one. In either case, you'll need the remote Git URL to add the remote. You can name the remote anything you'd like, but the default remote name is 'origin'.

# Add a Git remote
git remote add origin git@github.com:guidance/emergencylink.git

Keeping Up to Date

Once things are configured, it's a matter of keeping the git mirror up to date.

The basic procedure looks like this:

Get Latest Code from SVN

git checkout develop     // Switch to develop branch
get svn rebase           // Get latest stuff from SVN repository

# Push changes to git remote
# git push <repository> <branch>
git push origin develop  // Send code to Github repository

Merging

At some point you'll want to get your code into production. We'll assume that you're going to use git to merge the whole thing over to master. (This is a much-too-simple worldview, but source control strategy and workflow isn't in scope of this article.)

# In git, merging pulls code from another branch into the current branch:
# git merge <branch_to_merge_from>

git checkout master
git merge develop
git push origin master
@andry81
Copy link

andry81 commented Oct 29, 2023

@farOverNinethousand I've removed it because completely moved to the git. If you still want to mirror (not migrate, but not excluding that), then I recommend to use the SubGit: https://subgit.com/documentation/

The SubGit is the only reliable solution I could find and it is free for SVN migration:

If you are doing a one-time migration from SVN to git, you may use SubGit without any license — just download the tool.
Yet if you need support, sign up for a trial license.

If you want to start partial migration to migrate later completely, then you can use original repo of vcstool: https://github.com/dirk-thomas/vcstool (vcstool2 has removed support of not Git repositories)

It can use GIT external repositories together with SVN external repositories.

PS: SVN-to-Git mirroring is a pain in arse, DO NOT use it, instead migrate to Git and use Git instead.

@farOverNinethousand
Copy link

@andry81
Damn SVN-to-GIT mirroring is exactly what I wanted to do here.
Many thanks for the super detailed response!

@andry81
Copy link

andry81 commented Nov 1, 2023

@farOverNinethousand There is too many blocks and pitfalls on the road of a full SVN-to-Git mirroring. One of these are svn:externals. There is no a reliable way to mirror them because it requires a remote repo creation on a Git hub or server. There is no such functionality in the Git, because it is a part of a hub. All you can do is write down a script to map all svn:externals to Git branches or external repos, but that is not easy either. I can recall only one script that has tried to acquire this:
https://support.tmatesoft.com/t/script-for-conversion-of-svn-externals-to-git-submodules/2980
https://github.com/newren/git-filter-repo/blob/main/contrib/filter-repo-demos/convert-svnexternals

And there won't be any duplex mode. If you commit and pushed something to the Git manually, then you have to manually port it back to the SVN.

@farOverNinethousand
Copy link

@andry81
Okay someone just did exactly what I was looking for (a mirror of the JDownloader SVN repo): mirror/jdownloader#58 (comment)
-> https://github.com/mycodedoesnotcompile2/jdownloader_mirror/
Looks like he did this with github actions?
Thank you nevertheless for your detailed answer. If I ever need to set this up myself, I know where to find all relevant information.

@andry81
Copy link

andry81 commented Nov 1, 2023

@farOverNinethousand

Okay someone just did exactly what I was looking for (a mirror of the JDownloader SVN repo): mirror/jdownloader#58 (comment) -> https://github.com/mycodedoesnotcompile2/jdownloader_mirror/

This is not a complete and automatic mirror. He just checkouts 4 predefined root directories and pushes them into the git repo. If there would be other root directories or a root directory rename, then the script will miss that. All svn properties seems does ignore too.

@farOverNinethousand
Copy link

@andry81
Okay good to know.
The links to the forks are not helpful though as there is no current git mirror of the JDownloader SVN available apart from [maybe] the new one I linked.
Thanks for the additional explanation regarding the missing SVN properties.

@rinrab
Copy link

rinrab commented Mar 18, 2024

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