How To Migrate From SVN to Git

Migrating from SVN to Git can be a long and painful process. You have to know both setups very well, and you need to understand how command lines run and how it is all tied together.

In order to migrate from SVN to Git, you will need time, discipline, and be willing to learn as you go. Here are a number of ways to complete the SVN to Git migration, so I tried to search out the easiest way to accomplish this.

Get to Know the Git-SVN Command

Getting to know the git-svn command at a high level is probably the best place to start. Basically, the git-svn command is a command that allows Git to interact with Subversion repositories. The git-svn command is actually part of Git, which means it is NOT a plugin but is indeed bundled with your Git installation.

Migrating From SVN to Git Repositories

Let’s get started. First off you need to create a new local copy of the repository by using the following command:

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]git svn clone SVN_REPO [DEST_DIR] -T TRUNK -t TAGS -b BRANCHES[/ht_message]

If your current SVN repository follows the usual, standard layout of trunk, branches, tag folders, the command above will look like this:

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]git svn clone -s SVN_REPO [DEST_DIR][/ht_message]

The git-svn clone part of the command will check out each SVN revision and also make a Git commit in your local repository in order to recreate the history.

Note: If the SVN repository has a lot of commits this process will take a while, so find something else to do in the meantime.

Once this command has finished up you will have an entire Git repository with a local branch called master that will track the trunk branch in the SVN repository.

Note: If the SVN repository has a very long history, then the git svn clone operation might crash or hang. If it does crash or stall you can kill the entire process by using CTRL-C on your keyboard. When or if this happens you do not have to worry, as the Git repository has already been created, but there is just some SVN history that has yet to be retrieved from the server. To resume the operation, just change to the git repository folder and issue the command git svn fetch.

Pull Latest Changes From SVN Repository

When you want to receive the latest changes from your Git server use git pull. The equivalent to git pull in your git-svn journey is the git svn rebase command.

This command retrieves all the changes from the SVN repository and applies them on top of your local commits in your current branch. You can also use git svn fetch to retrieve all the changes from the SVN, but this allows you to do it without applying them to your local branch.

Add Changes

After you pull the work from SVN, you can use your local Git repository as a normal Git repository, with the normal Git commands (git add, git commit, git stash).

Push Local Changes to SVN

The git svn dcommit –rmdir command will create an SVN commit for each of your local Git commits. Just like SVN, your local Git history must be in sync with the latest changes in the SVN repository. If the command fails, try performing a git svn rebase command first.

Note: Remember, your local Git commits will be rewritten when using the command git svn dcommit. The solution is to create a new commit with the same contents and the new message. Technically speaking, this is a new commit anyway.

Things to Keep in Mind

Keep the History Linear: This means you can make all sorts of local operations. You can remove, reorder, or squash commits. Move history around, etc. Pretty much anything except merging.

Merges: Don’t merge local branches. If you find yourself in a position where you need to reintegrate the history of local branches, then use the git rebase command instead.

How To Handle Empty Folders

If you are familiar with Git (and chances are you are since you are performing a migration), you know that Git does not recognize the concept of folders. Git works with files and their file paths. What this means is that Git does not track empty folders.

On the other hand, SVN does. So by using the git svn command, by default any change you do involving empty folders with Git will not be propagated to SVN. To remove existing empty folders you have to do it manually.

To avoid needing to issue the flag each time you do a dcommit, just issue the following command:

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]git config –global svn.rmdir true[/ht_message]

This will change your .gitconfig file and will add these lines:

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ][svn]
rmdir = true  [/ht_message]

Please be cautious if you use the git clean –d command , as this will remove all untracked files, including folders that should be kept empty for SVN. If you need to generate aging the empty folders tracked by SVN use the command git svn mkdirs.I f you want to cleanup your workspace from untracked files and folders you should always use both commands:

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]git clean -fd && git svn mkdirs[/ht_message]

Performing a migration from SVN to Git is a challenging and detailed process. The more you know about both the better off you will be. Hopefully this gives you a starting point and explains some of the things that will happen during the migration process.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Copy link