Migrating from SVN to Git

You are viewing a wiki page. You are welcome to join the group and then edit it. Be bold!

Joshua Jabbour gave a great session on using the Git distributed version control system at Drupal Camp NYC 8. I was able to come home, download the source for Git, and easily build it on my laptop. I'd just started a new drupal project, and hadn't moved it into svn yet, so I easily created a git repostitory for the project. In fact, it was so easy I really wanted to move all my projects into git and chuck svn. That wasn't hard either, but I did need to combine two different tutorials and one of those used deprecated commands, so I thought I'd write up how I did it. This write up assumes a *nix environment. Hopefully someone who speaks Windows will add to the page if that is needed. References to the original sources are at the end.

  1. Before you begin, check your svn working copy and be sure all changes are committed to the svn repository.
  2. Extract the Authors from SVN: git tracks more information about authors than svn. We're going to use a shell script to parse the svn log and create a text file that lists all the authors shown in svn. You will then edit the text file to map those users to the names and email addresses that git needs.
    1. Depending on your editor, open a blank text file or create and edit a text file: extractAuthors
    2. Paste the following script into the text file:
      #!/usr/bin/env bash
      authors=$(svn log -q | grep -e '^r' | awk 'BEGIN { FS = "|" } ; { print $2 }' | sort | uniq)
      for author in ${authors}; do
        echo "${author} = NAME <USER@DOMAIN>";
      done
                  
    3. Save extractAuthors, naming it if necessary, into a directory in your shell path. I put mine in /usr/local/bin/
    4. Change the permissions on extractAuthors to make it executable.
    5. Change directory to your svn working copy.
    6. Execute the script: extractAuthors > /path/to/your_project-authors.txt
    7. Open your_project-authors.txt in an editor. Each author will be on a line such as:
      luke = NAME <USER@DOMAIN>
      darth = NAME <USER@DOMAIN>
      leia = NAME <USER@DOMAIN>

      Add real names and emails and save the file:
      luke = Luke Skywalker <luke@tatooine.net>
      darth = Darth Vader <lordvader@imperialpalace.org>
      leia = Leia Organa <lorgana@imperialsenate.org>
  3. Pull down your SVN into a temporary Git repository: We're going to move the project through git twice. The first time converts it and the second time cleans out extra SVN data you don't need anymore. For this write-up I'm using your_project as the directory that you are migrating from svn to git and projects as the parent directory for your_project. Here are the commands:
    1. cd projects
    2. mkdir svn_your_project
    3. cd svn_your_project
    4. git svn init http://example.com/your_project/ --no-metadata or git svn init file:///path/to/local-repo/your_project --no-metadata
    5. git config svn.authorsfile /path/to/your_project-authors.txt
    6. git svn fetch
  4. Backup your_project somewhere and delete the directory. We're going to restore it from the temporary git repository.
    1. cd projects
    2. mv your_project your_project.bak
  5. Create your clean working copy with git: Now we're going to clone the temporary git repository back into the same point in the file system where your project was originally.
    1. git clone svn_your_project your_project
    2. Now your_project is in git with all the revision history, you still have the original at your_project.bak in case something went wrong. You can delete it as soon as your sure that all went well:
    3. sudo rm -r svn_your_project

References:

Cleanly Migrate Your Subversion Repository To a GIT Repository
Creating a svn.authorsfile when migrating from subversion to git

New York City

Group events

Add to calendar

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week