Moving Repositories from SVN to GIT
Moving Code Repositories from SVN to GIT
Contents
Prerequisites
You need the following tools installed on your machine to successfully convert a subversion repo to a git repo.
- svn
- git-svn
- git-core
- ruby
- rubygems
Check the versions of the tools listed above to see if you have them installed.
-
svn --version
-
git --version
-
git svn --version
-
ruby --version
-
gem --version
If you don't have them installed you will need to install them using an appropriate version for your operating system. Check with the tools documentation for instructions on how to do this.
Install svn2git
svn2git is a ruby gem what will take all of your subversion info and package it into a git repo. This includes the ability to pull history as well as branches and tags and other useful svn info you will want to migrate. To install this use the gem tool. You may need to run this as root or sudo if you are on a mac, linux or unix operating system.
-
gem install svn2git
Create SVN Authors File
The first step in converting your SVN repository is to get a list of all authors that have committed or worked in the repository. This will allow you to query SVN later to gather and collect the history and commit logs that you will want to migrate to GIT along with the source code.
- cd to the root of your subversion project (ie. "cd ./projects/my-svn-project")
- Make sure you have write permission in this folder. You can check that by using chmod from a unix based OS.
- Run the following command to create the authors file and store it in the root of your project folder
-
svn log . -q | awk -F '|' '/^r/ {sub("^ ", "", $2); sub(" $", "", $2); print $2" = "tolower($2)" <"tolower($2)"@ldschurch.org>"}' | sort -u > ./authors.txt
-
- Verify that the authors.txt file was created and the authors look like regular committers for your project. (ie. "cat authors.txt" on unix platforms)
Create GIT repo from SVN info
You're now ready to actually do the conversion from SVN to GIT.
-
cd
to a new folder where you want to create your GIT repo.-
cd /path/to/new/git_repo
- Run the
svn2git
command to create the repository. Remember where you stored your authors.txt file because you need to reference it correctly in this command. Also when you reference your svn repo url do not add trunk. You want to reference the project above the trunk, branches and tags so you can collect the details for everything (trunk, branches, tags)-
svn2git https://url/to/your/svn/repo --no-minimize-url --authors /path/to/authors.txt --verbose
Push to GIT
You now have a local GIT repository on your machine. You can either work with it from here if you don't want to share with others or you can push it to a place like Stash, Bitbucket or Github. If you are going to push to an area where you can share the code and work with it on a team you need to do the following to get it pushed properly.
Before pushing the code you need to remove remote svn references from your git project.
- Open the following .git metadata files:
-
.git/config
-
.git/info/refs
-
.git/packed-refs
-
vi .git/config .git/info/refs .git/packed-refs
- Delete the
[svn-remote "svn"]
and[svn]
entries from the.git/config
metadata file - Delete all svn lines in the
.git/info/refs
metadata file - Delete all svn lines in the
.git/packed-refs
metadata file
-
- Delete the following folders:
.git/svn
.git/logs/refs/remotes/svn
.git/refs/remotes/svn
-
rm -r .git/svn .git/refs/remotes/svn .git/logs/refs/remotes/svn
- Set your remote
-
git remote add origin url.to.your/git/repo.git
-
- Push the the master sources and branches to git.
- By specifying the
--all
argument you will push master and all branches to the remote git repo. Set the remote git location (usually origin) and run the following command:
-
git push origin --all
- By specifying the
- Push your tags to to git.
- By specifying the
--tags
arguments, you will push all tags to the remote git repository. Set the remote git location (usually origin) and run the following command:
-
git push origin --tags
- By specifying the
- Open the following .git metadata files:
- Run the
-