使用svn2git迁移至Git仓库时,如何保留原SVN版本号?
Absolutely! You can totally retain those original SVN revision numbers in your newly migrated Git repository, and there are a few straightforward, reliable approaches to make this happen—pick the one that fits how your team wants to access that historical info.
Option 1: Embed SVN Revisions Directly in Git Commit Messages
This is the easiest, most out-of-the-box solution since svn2git has built-in support for it. When you run your migration command, just add the --add-metadata flag:
svn2git https://your-svn-repo-url.com/project --add-metadata
Every Git commit generated by the migration will automatically have a line at the end of its message that looks like:
git-svn-id: https://your-svn-repo-url.com/project@1234 5a1b2c3d...
The @1234 here is exactly the original SVN revision number. Anyone checking the commit history will immediately see which SVN revision maps to that Git commit.
If you want a cleaner format (like just SVN Revision: 1234), you can tweak svn2git's commit template, but --add-metadata is the no-fuss default that works for most teams.
Option 2: Use Git Notes to Link SVN Revisions (No Commit Message Changes)
If you don't want to clutter up your commit messages, Git Notes are perfect—they let you attach extra metadata (like SVN revision numbers) to commits without modifying the commits themselves.
- First run your basic svn2git migration (skip the
--add-metadataflag if you want clean commit messages) - Run this command to link the current HEAD commit to its SVN revision:
git svn fetch git notes add -m "SVN Revision: $(git svn find-rev $(git rev-parse HEAD))" HEAD
To batch-add notes for all commits, use a simple shell script:
git log --pretty=format:"%H" | while read commit_hash; do svn_rev=$(git svn find-rev $commit_hash) if [ -n "$svn_rev" ]; then git notes add -m "SVN Revision: $svn_rev" $commit_hash fi done
To see the SVN revision alongside commit details, use:
git log --notes
Option 3: Create Git Tags for Critical SVN Revisions
If your SVN repo had key milestones (like release versions, hotfixes), you can create Git tags that include the SVN revision number to make those easy to find later.
- First find the Git commit hash that maps to your target SVN revision:
git svn find-rev r1234
- Create an annotated tag for that commit:
git tag -a svn-r1234-release <commit-hash-you-found> -m "Maps to SVN revision r1234 (v1.2.3 release)"
This way, your team can quickly jump to important historical versions using the tag, and the SVN revision is clearly labeled.
内容的提问来源于stack exchange,提问作者venu

