Migrating from SVN to Git

Finally we migrated one of our legacy project from Subversion to Git considering the fact some of the team members still continue to work on SVN until they complete the development of feature(s) of a product on which they are working on.

In this post I tried to replicate the steps what we followed during migration taking an example of moving the project name my-application stored in a locally hosted SVN, following below steps:

Step 1: Download svn-migration-scripts.jar from here and place it in any directory, for me it’s svn-to-git under windows directory D:\ as follows:

D:\> mkdir svn-to-git
D:\> cd svn-to-git

Step 2: Verify the scripts to make sure Java Runtime Environment, Git, Subversion, and the git-svn utility installed.

java -jar D:\svn-to-git\svn-migration-scripts.jar verify

Make sure Java, Subversion and Git are installed before proceeding to next step.

Step 3: Extract the author information from SVN in a text file as follows:

D:\svn-to-git> java -jar D:\svn-to-git\svn-migration-scripts.jar authors http://localhost:81/svn/my-application > authors.txt

http://localhost:81/svn specified above is the svn host url

my-application refers to the name of a project in svn

Above command creates authors.txt that contains the username of every author in the SVN repository along with a generated name and email address. Edit the name and email address of the user and save it.

Step 4: Clone the SVN repository using git svn clone command as follows:

D:\svn-to-git> git svn clone --trunk=/trunk/dev/ --username=ArpitAggarwal --branches=/branches/dev --authors-file=authors.txt http://localhost:81/svn/my-application dev-git

Above command clone and update the information essential for git about the dev branch stored in a /branches/dev of my-application inside .git folder generated under directory dev-git.

If above command fails because of Perl crash then instead of restarting the git svn clone process move to your partially retrieved git repository and execute git-svn fetch command, it continues fetching the svn revisions from where we left off as follows:

D:\svn-to-git\dev-git> git svn fetch

Step 5: Next, we will clean the newly created Git repository to make it ready to push on a remote GitHub repository and also make Git aware about the authors file which we created before, as follows:

D:\svn-to-git\dev-git> java -Dfile.encoding=utf-8 -jar D:\svn-to-git\svn-migration-scripts.jar clean-git --force
D:\svn-to-git\dev-git> git config svn.authorsfile D:\svn-to-git\authors.txt

If clean-git command fails then checkout my answer on stackoverflow.com.

Step 6: Now create a new repository on GitHub for me it’s my-application and push the code to it following below commands:

D:\svn-to-git\dev-git> git remote add origin git@github.com:arpitaggarwal/my-application.git
D:\svn-to-git\dev-git> git push -u origin master

Now anytime from now if you want to update Git from SVN code, just fetch the new commits from the SVN repository, rebase the same with the local Git repository, clean and push it to the remote, as follows:

D:\svn-to-git\dev-git> git svn fetch
D:\svn-to-git\dev-git> java -Dfile.encoding=utf-8 -jar D:\svn-to-git\svn-migration-scripts.jar sync-rebase
D:\svn-to-git\dev-git> java -Dfile.encoding=utf-8 -jar D:\svn-to-git\svn-migration-scripts.jar clean-git --force
D:\svn-to-git\dev-git> git add .
D:\svn-to-git\dev-git> git push

Sources: https://www.atlassian.com/git/tutorials/svn-to-git-prepping-your-team-migration

SSH Access to Git

Committing code in Github becomes pain because of it’s asking username and password nature for each and every commit to the repository, specially when you are doing frequent commits.

Github provides a remedy to overcome it through SSH Keys. We can simply create SSH Keys and ask Github to identify our computer and don’t ask for username and password for every commit. It can be easliy done in few steps:

Step 1: Open Git Bash and execute command:

ls ~/.ssh

Step 2: Next, execute below command, and keep on pressing press Enter, it will create default public SSH key.

ssh-keygen -t rsa -C "your_email@example.com"
For example : ssh-keygen -t rsa -C "aggarwalarpit.89@gmail.com"

Step 3: Turn on ssh-agent entering below command:

ssh-agent -s

Step 4: Add generated SSH key to the ssh-agent:

ssh-add ~/.ssh/id_rsa

Step 5: Go to directory /Users/you/.ssh and open the “id_rsa.pub” in notepad and copy the content. Now login to your Github account, click on Settings and from right panel click on SSH Keys. Now, click on Add SSH Key button and paste the “id_rsa.pub” content in it and give it any name you want.

Step 6: Testing the connection, execute below command:

ssh -T git@github.com

Thats all!

Now commit the code in Github using SSH or HTTPS and I am sure Github will not ask you to enter username and password.