30日でマスター Git バージョン管理

Day 29: Subversion プロジェクトを Git リポジトリにインポートする方法

多くの組織は今も Subversion(SVN)を使っていますが、Git への移行を望んでいます。本記事では、履歴を保持しながら SVN リポジトリを Git にインポートする方法を説明します。

なぜ SVN から Git に移行するのか

Git の利点:

  • 分散型のワークフロー
  • 高速な操作(ローカル)
  • 優れたブランチとマージ
  • オフライン作業
  • より柔軟なワークフロー
  • 優れたツールとエコシステム

課題:

  • 学習曲線
  • 異なる概念
  • ワークフローの変更
  • チームの適応

移行戦略

1. クイックインポート(シンプル)

高速ですが、一部の SVN メタデータが失われます。

# 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

2. 適切なインポート(推奨)

著者、ブランチ、タグを保持します。

# 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. svn2git を使う(最も簡単)

svn2git をインストールします。

# On Mac
brew install svn2git

# On Linux
gem install svn2git

著者ファイルを作成します。

# 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>

移行を実行します。

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

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

段階的な移行プロセス

フェーズ 1:準備

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

フェーズ 2:テスト移行

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

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

フェーズ 3:著者ファイルを作成する

SVN の著者を抽出します。

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

Git の形式に変換します。

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

フェーズ 4:移行を実行する

# 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

フェーズ 5:Git リモートにプッシュする

# 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

特殊なケースへの対応

大規模なリポジトリ

大規模なリポジトリについては、段階的にインポートします。

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

パスを無視する

SVN の ignore に基づいて .gitignore を作成します。

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

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

バイナリファイル

SVN と Git ではバイナリの扱いが異なります。

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

SVN ブリッジ(git-svn)

移行期間中も SVN を使い続けます。

# 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

ワークフロー:

# Pull from SVN
git svn rebase

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

# Push to SVN
git svn dcommit

移行後のタスク

1. 移行を検証する

# 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. CI/CD を更新する

  • ビルドスクリプトを更新します
  • VCS の URL を変更します
  • デプロイパイプラインを更新します
  • すべての自動化プロセスをテストします

3. チームをトレーニングする

  • Git の基礎ワークショップ
  • 新しいワークフローのドキュメント
  • ハンズオンの練習
  • 移行期間中のサポート

4. ドキュメントを更新する

  • README を更新します
  • チェックアウト手順を変更します
  • コントリビューションのガイドラインを更新します
  • 新しいワークフローを文書化します

比較:SVN と Git のコマンド

# 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

トラブルシューティング

問題:コミットが欠けている

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

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

問題:著者マッピングのエラー

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

問題:.git ディレクトリが大きい

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

# Consider shallow clone for very old history

本日のまとめ

SVN から Git への移行には、計画と慎重な実行が必要です。svn2git のような適切なツールを使い、著者マッピングを維持することで、スムーズな移行が保証されます。時間をかけて移行をテストし、結果を検証し、チームに Git ワークフローのトレーニングを行いましょう。

本日学んだ Git コマンドとパラメータを整理しておきます。

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