多依赖项目从SVN迁移至Git(单仓库)的svn2git实施咨询
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 svn2giton 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:
Create a temporary branch for the project:
git checkout -b migrate-proj1Migrate 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/tagslayout: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-prefixand--tag-prefixensure project-specific branches/tags don’t clash with others (e.g.,proj1/feature-logininstead of justfeature-login).- If your SVN repo uses the standard
Switch back to your main branch and merge the migrated project (use
--allow-unrelated-historiessince the SVN history is independent of Git’s initial state):git checkout main git merge migrate-proj1 --allow-unrelated-historiesDelete 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 -lashould show all your projects as subdirectories. - Verify project history: Use
git log --oneline -- proj1/to see all commits specific toproj1—they should match your SVN history. - Check tags/branches: Run
git tagandgit branch -ato 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/libinstead of the old SVN URL).
内容的提问来源于stack exchange,提问作者Nikhil Khurana

