RSS

Moving from SVN to Git

24 May

I have been playing around with Git for a while now and its a great tool. The workflow is so much development oriented. So recently I decided to move one of my projects from SVN to Git, actually GitHub to be more specific. This post here details the steps in the process and lists some things to look out for. It assumes a basic understanding of Git. The Official Git Tutorial page is a good place to start. I found this talk by Linus Trovalds very insightful.

1. Register with GitHub

If you want to centrally host you Git repository on a server you can use GitHub. Its an excellent service for Git based projects with lots of features. It allows you to have both public and private hosted repositories. 

2. Clone the SVN repository

The first step in the process is to clone the SVN repository to a local Git repository. To do this run the following command

git svn clone -s <SVN Repo URL> <Git Repo Local Dir>

NOTE: Remove the -s option if you don’t have a standard SVN layout (tags, trunks, branches). You might need to use the --trunk, --tags, --branches options.

Depending on the size of the SVN repository and the commits in it this process might take a long time so go grab a coffee.

3. Check the Git repo

Now that the SVN repository is cloned into a  Git repository we can check the files in the new repository. Run the following command  to see commit logs. But we did not commit anything yet in Git, right? Well these are commits in SVN which Git has added into the new repository.

git log

4. Add from svn:ignore

If the SVN repository uses svn:ignore to ignore specific files, corresponding .gitignore files can be created as follows

git svn create-ignore
git add .
git commit --message "Committing gitignore files"

If the repository is to be published on a central server then proceed with the next steps otherwise you can jump to the What Next? section.

5. Push the Git repository to GitHub

You now have a local Git repository, ready to be pushed onto the GitHub server. But first things first. Generate a public/private keypair by following either of these guides based on your OS  Set Up Git (Windows)  and Set Up Git (Linux)

git remote add origin <SSH Git URL>
git push -u origin master

The SSH Git URL can be found by selecting the Git repository in GitHub and looking at the header as shown below

What Next?

Now you are all set and can start making changes to to your code and using the wonderful features of Git. But before that a few things to remember and understand.

  • Git does not track individual files but the contents
  • Git cannot have empty folders
  • A commit operation in Git commits to the local Git repository not the central repository. If you want to push it to the central repository then use git push.
  • Always run git status and git add before doing any commits to ensure that any new files are added. Alternatively you can use the -a switch in Git commit along with  the --interactive switch
  • Use branches whenever making changes. Its way too easy and a very central feature of Git.
  • Use git stash to store your changes into a working directory in case you need to restore to another branch temporarily.
About these ads
 
4 Comments

Posted by on May 24, 2011 in Git

 

4 responses to “Moving from SVN to Git

  1. Javin @ Classpath in Java tutorial

    May 25, 2011 at 7:00 pm

    never heard of this Git, now we are using perforce , could you share some more information about this Git , how do you find it in compare to SVN or CVS or perforce ?

    Thanks
    10 tips on logging in Java

     
    • aniruddhchitre

      June 2, 2011 at 1:42 pm

      @Javin @ Classpath Git is a distributed version control system. It was originally designed by Linus Trovalds as an alternative to Bitkeeper, which was being used for Linux kernel code. There are several differences when compared to SVN, CVS, or Perforce. It would actually take another post to describe it but here are a few major ones I can think of.
      1. Git is distributed so there is no central repository
      2. Git tracks content and not the files
      3. Branching and merging is very central to Git
      4. The workflow is very well suited for a typical development workflow
      5. Built for Performance
      Following links could be helpful
      Talk on Git, at Google by Linus Trovalds
      GitSvnComparision
      Git Tutorial by Lars Vogel
      The Git Community Book

       

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: