Master Git in 30 Days

Day 29: How to Import Subversion Projects into Git Repository

Many organizations still use Subversion (SVN) but want to migrate to Git. This article explains how to import SVN repositories to Git while preserving history.

Why Migrate from SVN to Git?

Advantages of Git:

  • Distributed workflow
  • Faster operations (local)
  • Better branching and merging
  • Offline work
  • More flexible workflows
  • Better tooling and ecosystem

Challenges:

  • Learning curve
  • Different concepts
  • Workflow changes
  • Team adaptation

Migration Strategies

1. Quick Import (Simple)

Fast but loses some SVN metadata:

# Clone entire SVN repository
git svn clone http://svn.example.com/repo

# Or specify trunk/branches/tags structure
git svn clone http://svn.example.com/repo --trunk=trunk --branches=branches --tags=tags

Preserves authors, branches, and tags:

# Create authors file
# Map SVN users to Git users
echo "svnuser1 = John Doe <john@example.com>" >> authors.txt
echo "svnuser2 = Jane Smith <jane@example.com>" >> authors.txt

# Clone with authors file
git svn clone http://svn.example.com/repo \
  --authors-file=authors.txt \
  --trunk=trunk \
  --branches=branches \
  --tags=tags \
  --no-metadata

3. Using svn2git (Easiest)

Install svn2git:

# On Mac
brew install svn2git

# On Linux
gem install svn2git

Create authors file:

# Generate authors list from SVN
svn log -q http://svn.example.com/repo | \
  awk -F '|' '/^r/ {print $2}' | \
  sort -u > authors.txt

# Edit authors.txt to add email addresses
# Format: svnuser = Full Name <email@example.com>

Perform migration:

# Create new directory
mkdir git-repo
cd git-repo

# Run migration
svn2git http://svn.example.com/repo \
  --authors authors.txt

Step-by-Step Migration Process

Phase 1: Preparation

1. Identify SVN repository structure
2. Create authors mapping file
3. Decide on Git repository structure
4. Plan branch/tag strategy
5. Communicate with team

Phase 2: Test Migration

# Do test migration first
git svn clone http://svn.example.com/repo test-migration

# Verify:
- Commit history
- Branches
- Tags
- File contents

Phase 3: Create Authors File

Extract SVN authors:

svn log -q http://svn.example.com/repo | \
  grep "^r" | \
  awk '{print $3}' | \
  sort -u > svn-authors.txt

Convert to Git format:

# authors.txt
jdoe = John Doe <john.doe@example.com>
jsmith = Jane Smith <jane.smith@example.com>
admin = Administrator <admin@example.com>

Phase 4: Perform Migration

# Clone SVN repository
git svn clone http://svn.example.com/repo \
  --authors-file=authors.txt \
  --stdlayout \
  migrated-repo

cd migrated-repo

# Convert SVN tags to Git tags
git for-each-ref refs/remotes/tags | cut -d / -f 4- | \
while read tagname; do
  git tag "$tagname" "refs/remotes/tags/$tagname"
  git branch -r -d "tags/$tagname"
done

# Convert SVN branches to Git branches
git for-each-ref refs/remotes/origin | cut -d / -f 4- | \
while read branchname; do
  git branch "$branchname" "refs/remotes/origin/$branchname"
done

# Remove SVN remote
git remote rm origin

Phase 5: Push to Git Remote

# Add Git remote
git remote add origin https://github.com/your-org/repo.git

# Push all branches
git push -u origin --all

# Push all tags
git push -u origin --tags

Handling Special Cases

Large Repositories

For large repositories, import incrementally:

# Start from revision 1000
git svn clone http://svn.example.com/repo \
  -r 1000:HEAD \
  --authors-file=authors.txt

Ignoring Paths

Create .gitignore based on SVN ignores:

# Extract SVN ignores
git svn show-ignore > .gitignore

# Commit
git add .gitignore
git commit -m "Add .gitignore from SVN"

Binary Files

SVN and Git handle binaries differently:

# For large binary files, consider Git LFS
git lfs install
git lfs track "*.psd"
git lfs track "*.zip"

SVN Bridge (git-svn)

Continue using SVN while transitioning:

# Clone with git-svn
git svn clone http://svn.example.com/repo

# Work normally with Git locally

# Get SVN updates
git svn rebase

# Push to SVN
git svn dcommit

Workflow:

# Pull from SVN
git svn rebase

# Make changes
git add .
git commit -m "Changes"

# Push to SVN
git svn dcommit

Post-Migration Tasks

1. Verify Migration

# Check commit count
git log --oneline | wc -l

# Compare with SVN
# Should match or be close

# Check specific files
git log -- path/to/file.txt
svn log path/to/file.txt

# Verify branches
git branch -a

# Verify tags
git tag

2. Update CI/CD

  • Update build scripts
  • Change VCS URLs
  • Update deployment pipelines
  • Test all automated processes

3. Train Team

  • Git basics workshop
  • New workflow documentation
  • Hands-on practice
  • Support during transition

4. Update Documentation

  • Update README
  • Change checkout instructions
  • Update contributing guidelines
  • Document new workflow

Comparison: SVN vs Git Commands

# Checkout
SVN: svn checkout
Git: git clone

# Update
SVN: svn update
Git: git pull

# Commit
SVN: svn commit
Git: git commit + git push

# Status
SVN: svn status
Git: git status

# Log
SVN: svn log
Git: git log

# Branch
SVN: svn copy .../trunk .../branches/name
Git: git checkout -b name

# Tag
SVN: svn copy .../trunk .../tags/v1.0
Git: git tag v1.0

# Merge
SVN: svn merge
Git: git merge

# Diff
SVN: svn diff
Git: git diff

Troubleshooting

Problem: Missing Commits

# Check SVN revision range
git svn log | head -n 20

# Re-clone from specific revision
git svn clone -r START:HEAD ...

Problem: Author Mapping Errors

# Add missing authors to authors.txt
# Re-run migration

Problem: Large .git Directory

# Garbage collection
git gc --aggressive --prune=now

# Consider shallow clone for very old history

Today’s Summary

Migrating from SVN to Git requires planning and careful execution. Using proper tools like svn2git and maintaining author mappings ensures a smooth transition. Take time to test the migration, verify results, and train your team on Git workflows.

Let me reorganize the Git commands and parameters learned today:

  • git svn clone
  • git svn rebase
  • git svn dcommit
  • git svn show-ignore
  • git for-each-ref
  • git gc –aggressive