You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

多依赖项目从SVN迁移至Git(单仓库)的svn2git实施咨询

Migrate Multiple Inter-Dependent SVN Repos to a Single Git Repo with svn2git (Preserving Full History)

Great choice going with a monorepo for your inter-dependent projects—it’ll make cross-project changes way smoother. Let’s break down exactly how to pull this off with svn2git, keeping every project’s full commit history intact.

Prerequisites First

Make sure you have these tools installed locally:

  • Git
  • Subversion command-line client (svn)
  • svn2git (install via your package manager, e.g., brew install svn2git on macOS, or build from source if needed)
  • A text editor to create an author mapping file (critical for preserving commit author info)

Step 1: Create an Author Mapping File

SVN uses plain usernames, while Git expects the Name <email> format. Collect all unique usernames from all your SVN repos, then create a file (e.g., authors.txt) with entries like:

svn_user1 = Jane Doe <jane.doe@company.com>
svn_user2 = John Smith <john.smith@company.com>

Place this file somewhere accessible (e.g., the parent directory of your new Git repo).

Step 2: Initialize Your Unified Git Repo

Start with a clean local Git repo to hold all your projects:

mkdir unified-monorepo
cd unified-monorepo
git init

Step 3: Migrate Each SVN Project to a Subdirectory (With Full History)

For each SVN project, we’ll import its entire history into a dedicated subdirectory in the Git repo. The key here is using --prefix to scope the project to its subfolder, and handling branches/tags to avoid conflicts.

Option A: Migrate to a Branch (Recommended for Control)

This method keeps each project’s migration isolated, making it easier to resolve conflicts if they arise:

  1. Create a temporary branch for the project:

    git checkout -b migrate-proj1
    
  2. Migrate the SVN project to this branch, scoping it to proj1/ (adjust the SVN URL and prefix to match your project):

    • If your SVN repo uses the standard trunk/branches/tags layout:
      svn2git https://your-svn-server/svn/proj1 \
        --prefix=proj1/ \
        --branch-prefix=proj1/ \
        --tag-prefix=proj1/ \
        --authors ../authors.txt
      
    • If your SVN repo doesn’t use the standard layout (e.g., code lives directly at the root):
      svn2git https://your-svn-server/svn/proj1 \
        --trunk . \
        --prefix=proj1/ \
        --branch-prefix=proj1/ \
        --tag-prefix=proj1/ \
        --authors ../authors.txt
      

    The --branch-prefix and --tag-prefix ensure project-specific branches/tags don’t clash with others (e.g., proj1/feature-login instead of just feature-login).

  3. Switch back to your main branch and merge the migrated project (use --allow-unrelated-histories since the SVN history is independent of Git’s initial state):

    git checkout main
    git merge migrate-proj1 --allow-unrelated-histories
    
  4. Delete the temporary branch (optional but clean):

    git branch -d migrate-proj1
    

Repeat this process for every SVN project, adjusting the prefix and SVN URL each time.

Option B: Migrate Directly to Main Branch

If you’re confident no conflicts will occur (e.g., all projects have unique root files), you can migrate directly to the main branch for each project:

svn2git https://your-svn-server/svn/proj2 \
  --prefix=proj2/ \
  --branch-prefix=proj2/ \
  --tag-prefix=proj2/ \
  --authors ../authors.txt \
  --no-rebase

The --no-rebase flag prevents svn2git from trying to rebase onto existing commits, which would cause issues with unrelated histories.

Step 4: Validate the Migration

Before pushing to a remote repo, verify everything is intact:

  • Check the file structure: ls -la should show all your projects as subdirectories.
  • Verify project history: Use git log --oneline -- proj1/ to see all commits specific to proj1—they should match your SVN history.
  • Check tags/branches: Run git tag and git branch -a to ensure project-specific tags/branches were imported correctly.

Step 5: Push to Remote Git Repo

Once you’re satisfied with the local migration, push everything to your remote Git host:

# Add the remote repo
git remote add origin git@your-git-server:your-team/unified-monorepo.git

# Push the main branch
git push -u origin main

# Push all tags
git push --tags

# Push project-specific branches (if you have any)
git push origin proj1/feature-new-ui
git push origin proj2/bugfix-payment

Key Tips to Avoid Headaches

  • Test first: Do a full dry run migration locally before touching any production repos.
  • Resolve conflicts early: If two projects have overlapping files (e.g., root-level README.md), merge conflicts will pop up—resolve them manually and commit the fix.
  • Keep SVN around temporarily: Don’t delete your SVN repos immediately. Wait a few weeks to ensure the Git repo is fully adopted and no critical history was missed.
  • Update dependencies: Since your projects are now in a monorepo, update any internal dependency paths to use relative paths (e.g., ../proj2/lib instead of the old SVN URL).

内容的提问来源于stack exchange,提问作者Nikhil Khurana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:14:25