Course Tonight

Course Tonight

Did You Know?

We design Docy for the readers, optimizing not for page views or engagement

Chapter 36: Migrating to Git

Estimated reading: 6 minutes 29 views

Section 36.1: SubGit

SubGit may be used to perform a one-time import of an SVN repository to git.

$ subgit import --non-interactive --svn-url http://svn.my.co/repos/myproject myproject.git

Section 36.2: Migrate from SVN to Git using Atlassian conversion utility

Download the Atlassian conversion utility here. This utility requires Java, so please ensure that you have the Java Runtime Environment (JRE) installed on the machine you plan to do the conversion.

Use the command java -jar svn-migration-scripts.jar verify to check if your machine is missing any of the programs necessary to complete the conversion. Specifically, this command checks for the Git, Subversion, and git-svn utilities. It also verifies that you are performing the migration on a case-sensitive file system. Migration to Git should be done on a case-sensitive file system to avoid corrupting the repository.

Next, you need to generate an authors file. Subversion tracks changes by the committer’s username only. Git, however, uses two pieces of information to distinguish a user: a real name and an email address. The following command will generate a text file mapping the Subversion usernames to their Git equivalents:

java -jar svn-migration-scripts.jar authors <svn-repo> authors.txt

where <svn-repo> is the URL of the Subversion repository you wish to convert. After running this command, the contributors’ identification information will be mapped in authors.txt. The email addresses will be of the form <username>@mycompany.com. In the authors file, you will need to manually change each person’s default name (which by default has become their username) to their actual names. Make sure to also check all of the email addresses for correctness before proceeding.

The following command will clone an SVN repository as a Git one:

git svn clone --stdlayout --authors-file=authors.txt <svn-repo> <git-repo-name>

where <svn-repo> is the same repository URL used above, and <git-repo-name> is the folder name in the current directory to clone the repository into. There are a few considerations before using this command:

  • The --stdlayout flag tells Git that you’re using a standard layout with trunk, branches, and tags folders. Subversion repositories with non-standard layouts require you to specify the locations of the trunk folder, any/all branch folders, and the tags folder. This can be done by following this example: git svn clone --trunk=/trunk --branches=/branches --branches=/bugfixes --tags=/tags --authors-file=authors.txt <svn-repo> <git-repo-name>.
  • This command could take many hours to complete depending on the size of your repo.
  • To cut down the conversion time for large repositories, the conversion can be run directly on the server hosting the Subversion repository to eliminate network overhead.

git svn clone imports the Subversion branches (and trunk) as remote branches, including Subversion tags (remote branches prefixed with tags/). To convert these to actual branches and tags, run the following commands on a Linux machine in the order they are provided. After running them, git branch -a should show the correct branch names, and git tag -l should show the repository tags.

git for-each-ref refs/remotes/origin/tags | cut -d / -f 5- | grep

Section 36.3: Migrating Mercurial to Git

One can use the following methods to import a Mercurial Repo into Git:

  1. Using fast export:
cd
git clone git://repo.or.cz/fast-export.git
git init git_repo
cd git_repo
~/fast-export/hg-fast-export.sh -r /path/to/old/mercurial_repo
git checkout HEAD
  1. Using Hg-Git: A very detailed answer here: https://stackoverflow.com/a/31827990/5283213
  2. Using GitHub’s Importer: Follow the (detailed) instructions at GitHub.

Section 36.4: Migrate from Team Foundation Version Control (TFVC) to Git

You can migrate from Team Foundation Version Control to Git by using an open-source tool called Git-TF. Migration will also transfer your existing history by converting TFS check-ins to Git commits.

To put your solution into Git using Git-TF, follow these steps:

  1. Download Git-TF from Codeplex.
  2. Clone your TFVC solution:

Launch PowerShell (Windows) and type the command:

git-tf clone http://my.tfs.server.address:port/tfs/mycollection '$/myproject/mybranch/mysolution' -deep

The --deep switch is the keyword to note as this tells Git-TF to copy your check-in history. You now have a local Git repository in the folder from which you called your clone command.

  1. Cleanup:
  • Add a .gitignore file. If you are using Visual Studio, the editor can do this for you. Otherwise, you could do this manually by downloading a complete file from GitHub/gitignore.
  • Remove TFS source control bindings from the solution (remove all *.vssscc files). You could also modify your solution file by removing the GlobalSection(TeamFoundationVersionControl)......EndGlobalSection.
  1. Commit & Push:

Complete your conversion by committing and pushing your local repository to your remote.

git add .
git commit -a -m "Converted solution source control from TFVC to Git"
git remote add origin https://my.remote/project/repo.git
git push origin master

Section 36.5: Migrate from SVN to Git using svn2git

svn2git is a Ruby wrapper around git’s native SVN support through git-svn, helping you migrate projects from Subversion to Git while preserving history, including trunk, tags, and branches history.

Examples

To migrate a SVN repository with the standard layout (i.e., branches, tags, and trunk at the root level of the repository):

$ svn2git http://svn.example.com/path/to/repo

To migrate a SVN repository that is not in standard layout:

$ svn2git http://svn.example.com/path/to/repo --trunk trunk-dir --tags tags-dir --branches branches-dir

If you do not want to migrate (or do not have) branches, tags, or trunk, you can use the options –notrunk, –nobranches, and –notags.

For example:

$ svn2git http://svn.example.com/path/to/repo --trunk trunk-dir --notags --nobranches

This will migrate only the trunk history.

To reduce the space required by your new repository, you may want to exclude any directories or files that were mistakenly added (e.g., build directory or archives):

$ svn2git http://svn.example.com/path/to/repo --exclude build --exclude '.*\.zip$'

Post-migration optimization

If you already have thousands of commits (or more) in your newly created Git repository, you may want to reduce the space used before pushing your repository to a remote. This can be done using the following command:

$ git gc --aggressive

Note: The previous command can take several hours on large repositories (tens of thousands of commits and/or hundreds of megabytes of history).

Leave a Comment

Share this Doc

Chapter 36: Migrating to Git

Or copy link

CONTENTS