Move code from SVN to GIT


I will show the recommended way to move code from SVN repository to a GIT repository without losing commits history.
I have broken down the SVN to GIT migration process steps into 5 steps
1.     Prepare your environment for the migration.
2.     Convert the SVN repository to a local Git repository.
3.     Add the code to remote git
For demonstration purpose, I will use https://svn.eionet.europa.eu/repositories/GNInspire/
It is a public SVN repository.
Prepare
In the first step, we will import the code from SVN to the migration lead’s machine. We need to download “svn-migration-scripts.jar” from https://bitbucket.org/atlassian/svn-migration-scripts/downloads/
Although GIT comes with the necessary tools for importing a SVN repository, still we need to download a utility tool provided by Atlassian that fills in the functionalities missed by GIT. Also for doing this migration, we need a case sensitive system based on Linux.
Once downloaded, it is good to verify that this works.
Run the below command
java -jar ~/svn-migration-scripts.jar verify
Create a folder on the local machine called Gitmigration.
Next, we must extract the author information from SVN. SVN stores only usernames as author information whereas GIT stores both usernames and email addresses.
Because of this, we need to map SVN author information with the GIT information.

cd Gitmigration
java -jar ~/svn-migration-scripts.jar authors https://svn.eionet.europa.eu/repositories/GNInspire/ > authors.txt
You will see a author.txt file. It will look like
chartben = chartben <chartben@mycompany.com>
francpru = francpru <francpru@mycompany.com>
Now, use some tool to replace mycompany.com with your domain name.

Convert
Now we must SVN’s trunk, branch, tags information to the new GIT format.
Run the below command
git svn clone --stdlayout --authors-file= GitMigration\authors.txt https://svn.eionet.europa.eu/repositories/GNInspire/ GNInspireAsGIT (Any name for the project)

You need to install git svn on Linux.
Note: If you want to try this migration step on windows (And you don’t care about data corruption), you must install git svn on windows through Cygwin.
If you get some error like "Error finding tracking ref for branch master”, then use the below command.
git svn clone --prefix="" --stdlayout --authors-file=C:\Users\ragulkumar.rajendran\KNOWLEDGE\SVN-GIT\GitMigration\authors.txt https://svn.eionet.europa.eu/repositories/GNInspire/ GNInspireAsGIT
This step is time consuming. Once this is complete, you won’t find any branches and tags inside Git folder.
This is to enable two-way synchronization between SVN and GIT.
The git svn clone command imports your SVN branches as remote branches and imports your SVN tags as remote branches prefixed with tags/.
That is why, we must convert these remote branches and tags to actual GIT versions.
We must use the clean-git operation from svn-migration-scripts.jar. This is destructive operation and we need to be careful. It means that the commits from GIT cannot be moved back to SVN.
java -Dfile.encoding=utf-8 -jar svn-migration-scripts.jar clean-git –force (Note: If you remove -force and run the command, it will simply output the changes the script wants to make and not actually make any change)
Note: However, if you’re planning on committing to the Git repository and the SVN repository during the migration process, you should not perform the following commands.

Create GIT repository
Now add the authors file to git configuration.
git config svn.authorsfile authors.txt
Now add the remote origin
git remote add origin https://GIT_URL.git
git push -u origin --all
git push --tags







Comments

Popular Posts