我阅读了Git手册、常见问题解答、Git-SVN速成课程等,他们都解释了这一点和那一点,但你找不到像这样的简单说明:

SVN存储库位于:svn://myserver/path/to/svn/repos

Git存储库位于:git://myserver/path/to/git/repos

git-do-the-magic-svn-import-with-history \
svn://myserver/path/to/svn/repos \
git://myserver/path/to/git/repos

我不希望它这么简单,也不希望它是一个命令。但我确实希望它不要试图解释任何事情——只是说在这个例子中应该采取什么步骤。


当前回答

atlassian网站上的这份指南是我找到的最好的指南之一:

https://www.atlassian.com/git/migration

此工具-https://bitbucket.org/atlassian/svn-migration-scripts-对于生成authors.txt等内容也非常有用。

其他回答

我建议在尝试经常使用Git-svn之前,先熟悉Git,即保持svn作为集中存储库并在本地使用Git。

然而,对于具有所有历史记录的简单迁移,以下是几个简单步骤:

初始化本地回购:

mkdir project
cd project
git svn init http://svn.url

标记要开始导入修订的时间:

git svn fetch -r42

(或仅对所有版本使用“gitsvn-fetch”)

事实上,从那时起,获取一切:

git svn rebase

您可以使用Gitk检查导入的结果。我不确定这是否适用于Windows,但适用于OSX和Linux:

gitk

当您在本地克隆了SVN repo后,您可能希望将其推送到集中的Git repo,以便于协作。

首先创建空的远程存储库(可能在GitHub上?):

git remote add origin git@github.com:user/project-name.git

然后,可选地同步您的主分支,这样当远程主机和本地主机都包含新内容时,拉操作将自动合并远程主机:

git config branch.master.remote origin
git config branch.master.merge refs/heads/master

之后,您可能会有兴趣尝试我自己的git_remote_branch工具,它有助于处理远程分支:

第一篇解释文章:“Git远程分支”

跟进最新版本:“git与git_remote_branch协作的时间”

我们可以使用git-svnclone命令,如下所示。

svn log-q<svn_URL>|awk-F'|''/^r/{sub(“^”,“”,$2);sub(”$“,”,$2);print$2“=”$2“<”$2”>“}'|sort-u>authors.txt

上述命令将从SVN提交创建authors文件。

svn日志—复制时停止<svn_URL>

创建SVN项目时,上面的命令将为您提供第一个修订号。

git svn clone-r<svn_REV_NO>:HEAD--无最小化url--stdlayout--无元数据--作者文件authors.txt<svn_url>

以上命令将在本地创建Git存储库。

问题是它不会将分支和标签转换为推送。您必须手动执行这些操作。以下分支机构示例:

$ git remote add origin https://github.com/pankaj0323/JDProjects.git
$ git branch -a
* master
  remotes/origin/MyDevBranch
  remotes/origin/tags/MyDevBranch-1.0
  remotes/origin/trunk
$$ git checkout -b MyDevBranch origin/MyDevBranch
Branch MyDevBranch set up to track remote branch MyDevBranch from origin.
Switched to a new branch 'MyDevBranch'
$ git branch -a
* MyDevBranch
  master
  remotes/origin/MyDevBranch
  remotes/origin/tags/MyDevBranch-1.0
  remotes/origin/trunk
$

对于标记:

$git checkout origin/tags/MyDevBranch-1.0
Note: checking out 'origin/tags/MyDevBranch-1.0'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 3041d81... Creating a tag
$ git branch -a
* (detached from origin/tags/MyDevBranch-1.0)
  MyDevBranch
  master
  remotes/origin/MyDevBranch
  remotes/origin/tags/MyDevBranch-1.0
  remotes/origin/trunk
$ git tag -a MyDevBranch-1.0 -m "creating tag"
$git tag
MyDevBranch-1.0
$

现在将master、branches和标记推送到远程git存储库。

$ git push origin master MyDevBranch MyDevBranch-1.0
Counting objects: 14, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (14/14), 2.28 KiB | 0 bytes/s, done.
Total 14 (delta 3), reused 0 (delta 0)
To https://github.com/pankaj0323/JDProjects.git
 * [new branch]      master -> master
 * [new branch]      MyDevBranch -> MyDevBranch
 * [new tag]         MyDevBranch-1.0 -> MyDevBranch-1.0
$

svn2git实用程序

svn2git实用程序删除了带有分支和标记的手动工作。

使用命令sudogeminstallsvn2git安装它。之后运行以下命令。

$svn2git<SVN_URL>--authors.txt--修订版<SVN_REV_NO>

现在,您可以列出分支、标记并轻松推送它们。

$ git remote add origin https://github.com/pankaj0323/JDProjects.git
$ git branch -a
  MyDevBranch
* master
  remotes/svn/MyDevBranch
  remotes/svn/trunk
$ git tag
  MyDevBranch-1.0
$ git push origin master MyDevBranch MyDevBranch-1.0

假设您有20个分支和标记,显然svn2git将为您节省大量时间,这就是为什么我比原生命令更喜欢它的原因。这是一个很好的原生git-svnclone命令包装器。

有关完整的示例,请参阅我的博客条目。

将Subversion存储库干净地迁移到Git存储库。首先,您必须创建一个将Subversion提交作者名称映射到Git commiters的文件,比如~/authors.txt:

jmaddox = Jon Maddox <jon@gmail.com>
bigpappa = Brian Biggs <bigpappa@gmail.com>

然后您可以将Subversion数据下载到Git存储库中:

mkdir repo && cd repo
git svn init http://subversion/repo --no-metadata
git config svn.authorsfile ~/authors.txt
git svn fetch

如果您在Mac上,可以通过安装git-core+svn从MacPorts获取git-svn。

如果您的subversion存储库与所需的git存储库位于同一台机器上,那么您可以在init步骤中使用此语法,否则都一样:

git svn init file:///home/user/repoName --no-metadata

我使用以下脚本读取了一个包含所有SVN存储库列表的文本文件,并将其转换为Git,然后使用gitclone--bare将其转换成Git存储库:

#!/bin/bash
file="list.txt"
while IFS= read -r repo_name
do
 printf '%s\n' "$repo_name"
 sudo git svn clone --shared --preserve-empty-dirs --authors-file=users.txt file:///programs/svn/$repo_name
 sudo git clone --bare /programs/git/$repo_name $repo_name.git
 sudo chown -R www-data:www-data $repo_name.git
 sudo rm -rf $repo_name
done <"$file"

list.txt的格式为:

repo1_name
repo2_name

users.txt的格式为:

(无作者)=罗杰斯王子<prince.rogers.nelson@payesley.park.org>

www数据是Apache web服务器用户,需要权限才能通过HTTP推送更改。

GitHub有一个导入程序。创建存储库后,可以通过其URL从现有存储库导入。如果适用,它将要求您提供证书,然后从那里开始。

当它运行时,它会找到作者,您可以简单地将他们映射到GitHub上的用户。

我已经在一些存储库中使用了它,而且它非常准确,速度也快得多!一个大约4000次提交的存储库花了10分钟,之后我的朋友花了4天!