Migrating an SVN Repository to Github
--
Techstacks change! And so do the tools we use. As with most changes, there is the issue of migration of existing data to a newer platform. E.g., if your repository was on SVN and now you want to move to Github, then how to go about it.
SVN v/s Git used to be hotly debated. In recent years though, Git is the clear winner. So chances are, if you are on SVN, you’d be moving to Git, and probably Github, pretty soon.
Recently I needed to migrate an SVN repository to Github. There are two significant steps involved in this process:
- Migrate SVN repository to a local Git repository
- Push local Git repository to Github
1. Migrate SVN Repository to a Local Git Repository
1.1. Retrieve a list of all Subversion committers
$ svn log -q [SVN repo URL] | \
awk -F '|' '/^r/ {sub("^ ", "", $2); sub(" $", "", $2); \
print $2" = "$2" <"$2">"}' | sort -u > authors-transform.txt
This command will fetch all the log messages, extract and sort the distinct usernames, and place them into an authors-transform.txt file. Now change
username = username <username>
into this:
username = Firstname Lastname <username@example.com>
for the mapping between corresponding SVN and Git users.
1.2. Clone the SVN repository using git-svn
git svn clone [SVN repo URL] --no-metadata -A authors-transform.txt --stdlayout ~/temp
This command may take a while, depending on the size of SVN commit history. Once completed, check if the SVN commit history has been migrated correctly by running git status
and git log
inside directory~/temp
.
1.3. (Optional) Convert svn:ignore properties to .gitignore
If your svn repo was using svn: ignore properties, you can easily convert this to a .gitignore file using:
cd ~/temp
git svn show-ignore > .gitignore
git add .gitignore
git commit -m 'Migrate svn:ignore to .gitignore'
2. Push Local Git Repository to Github
2.1. Create a new repository in Github
Make sure the project is empty i.e., no Readme or License. Copy the .git URL, e.g. https://github.com/<username>/<reponame>.git
.
2.2. Set remote repository for the local repository
git remote add origin https://github.com/<username>/<reponame>.git
2.3. Push local repository to remote
If the remote project is empty with no log, the following command should cleanly push the code to Github repository
git push -u origin master
However, if the Github project has any record, you may run the following command to merge the logs:
git pull --allow-unrelated-histories origin master
git push -u origin master
That’s it!