Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@tannerwatson

This comment has been minimized.

Show comment
Hide comment
@tannerwatson

tannerwatson Aug 9, 2012

Where did you get the "production" branch from under the "Creating the Local Git Repository" section?

tannerwatson commented Aug 9, 2012

Where did you get the "production" branch from under the "Creating the Local Git Repository" section?

@chrisdembia

This comment has been minimized.

Show comment
Hide comment
@chrisdembia

chrisdembia May 8, 2013

Grammar: "you’re" should be "your".

chrisdembia commented May 8, 2013

Grammar: "you’re" should be "your".

@Kakadu

This comment has been minimized.

Show comment
Hide comment
@Kakadu

Kakadu Jul 1, 2013

I think that git reset --hard production should be read as git reset --hard develop

Kakadu commented Jul 1, 2013

I think that git reset --hard production should be read as git reset --hard develop

@k-stailey

This comment has been minimized.

Show comment
Hide comment
@k-stailey

k-stailey Aug 14, 2013

s/get svn rebase/git svn rebase/

k-stailey commented Aug 14, 2013

s/get svn rebase/git svn rebase/

@rcourtna

This comment has been minimized.

Show comment
Hide comment
@rcourtna

rcourtna Aug 18, 2014

I'm trying to do something similar, but I can't for the life of me understand what this step is doing:

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

Why the reset?

rcourtna commented Aug 18, 2014

I'm trying to do something similar, but I can't for the life of me understand what this step is doing:

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

Why the reset?

@JuergenKosel

This comment has been minimized.

Show comment
Hide comment
@JuergenKosel

JuergenKosel Mar 18, 2018

Hello,

please take a look at my 14 lines script: https://github.com/JuergenKosel/git-svn-mirror/blob/master/git-svn-mirror.sh.
The script mirrors all branches from svn into a git repository.

JuergenKosel commented Mar 18, 2018

Hello,

please take a look at my 14 lines script: https://github.com/JuergenKosel/git-svn-mirror/blob/master/git-svn-mirror.sh.
The script mirrors all branches from svn into a git repository.

@andry81

This comment has been minimized.

Show comment
Hide comment
@andry81

andry81 Jul 28, 2018

Here is another one click solution over pure and raw windows batch scripts (windows only).

http://svn.code.sf.net/p/contools/deploy/trunk/solutions/all-in-one/
(no sources inside, you have to checkout it through the svn)

Supported only svn-to-git mirroring.

If you want to something clean and bigger, then you have to write your own one.

andry81 commented Jul 28, 2018

Here is another one click solution over pure and raw windows batch scripts (windows only).

http://svn.code.sf.net/p/contools/deploy/trunk/solutions/all-in-one/
(no sources inside, you have to checkout it through the svn)

Supported only svn-to-git mirroring.

If you want to something clean and bigger, then you have to write your own one.

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