我阅读了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

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


当前回答

对于GitLab用户,我在这里提出了我如何从SVN迁移的要点:

https://gist.github.com/leftclickben/322b7a3042cbe97ed2af

从SVN迁移到GitLab的步骤

安装程序

SVN位于SVN.domain.com.au。SVN可以通过http访问(其他协议也可以)。GitLab位于git.domain.com.au,并且:与命名空间开发团队一起创建一个组。至少创建一个用户帐户,将其添加到组中,并具有用于迁移的帐户的SSH密钥(使用SSH进行测试git@git.domain.com.au).项目收藏夹项目是在开发团队名称空间中创建的。文件users.txt包含相关的用户详细信息,每行一个用户,格式为username=First Last<address@domain.com.au>,其中username是SVN日志中给定的用户名。(有关详细信息,请参见参考资料部分的第一个链接,特别是用户Casey的回答)。

版本

subversion 1.6.17(r1128011)git版本1.9.1GitLab版本7.2.1 ff1633fUbuntu服务器14.04

命令

git svn clone --stdlayout --no-metadata -A users.txt http://svn.domain.com.au/svn/repository/favourite-project
cd favourite-project
git remote add gitlab git@git.domain.com.au:dev-team/favourite-project.git
git push --set-upstream gitlab master

就是这样!在GitLab web UI中重新加载项目页面,您将看到现在列出的所有提交和文件。

笔记

如果有未知用户,git-svnclone命令将停止,在这种情况下,update-users.txt、cd-f收藏夹项目和git-svnfetch将从停止的位置继续。SVN存储库的标准主干标记分支布局是必需的。给git-svnclone命令的SVN URL在trunk/、tags/和branches/的正上方停止。git-svn-clone命令产生大量输出,包括顶部的一些警告;我无视警告。

其他回答

如果您正在使用SourceTree,则可以直接从应用程序执行此操作。转到文件->新建/克隆,然后执行以下操作:

输入远程SVN URL作为“源路径/URL”。在提示时输入凭据。输入本地文件夹位置作为“目标路径”。给它起个名字。在高级选项中,从“创建本地”下拉列表中选择“Git”类型的存储库”。您可以选择指定要从中克隆的修订。点击克隆。

在SourceTree中打开回购,您将看到您的提交消息也已迁移。

现在转到存储库->存储库设置并添加新的远程存储库详细信息。如果您愿意,请删除SVN远程(我通过“编辑配置文件”选项执行此操作)。

准备好后,将代码推送到新的远程存储库,并自由编码。

将svn子模块/文件夹“MyModule”转换为具有历史记录的git,既没有标记也没有分支。

git-svn clone--无元数据--trunk=SomeFolder1/SomeFolder2/SomeFolder3/MyModulehttp://svnhost:port/repo_root_folder/MyModule_temp-一个C: \ccheetah\svn\authors-transform.txtgit克隆MyModule_temp MyModulecd我的模块git流初始化git远程设置url源https://userid@stashhost/stash/scm/xyzxyz/MyModule.gitgit push-u原始主机gitpush-u源代码开发

要保留svn忽略列表,请在步骤1之后使用以上注释

这里的几个答案涉及https://github.com/nirvdrum/svn2git,但对于大型存储库,这可能会很慢。我试过使用https://github.com/svn-all-fast-export/svn2git相反,它是一个同名的工具,但用于将KDE从SVN迁移到Git。

设置它的工作稍微多了一些,但当为我完成转换时,其他脚本花费了数小时的时间。

我们可以使用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命令包装器。

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

只使用git、SVN和bash的稍微扩展的答案。它包括SVN存储库的步骤,这些存储库不使用带有主干/分支/标签目录布局的常规布局(SVN绝对不执行这种布局)。

首先使用此bash脚本扫描SVN repo以查找不同的贡献者,并为映射文件生成模板:

#!/usr/bin/env bash
authors=$(svn log -q | grep -e '^r' | awk 'BEGIN { FS = "|" } ; { print $2 }' | sort | uniq)
for author in ${authors}; do
  echo "${author} = NAME <USER@DOMAIN>";
done

使用此选项可以创建一个作者文件,在该文件中,您可以使用git-config财产user.name和user.email将svn用户名映射到开发人员设置的用户名和电子邮件(请注意,对于GitHub这样的服务,只有匹配的电子邮件就足够了)。

然后让git-svn将svn存储库克隆到git存储库,告诉它映射:

git-svn clone--authors file=authors--stdlayoutsvn://example.org/Folder/projectroot

这可能需要非常长的时间,因为git-svn将单独检查存在的每个标记或分支的每个修订。(请注意,SVN中的标记实际上只是分支,因此它们在Git中最终也是如此)。您可以通过删除SVN中不需要的旧标记和分支来加快速度。

在同一网络中的服务器上或同一服务器上运行它也可以真正加快速度。此外,如果由于某种原因,此过程中断,您可以使用

git-svn-rebase--继续

在很多情况下,你已经在这里完成了。但是,如果您的SVN回购有一个非传统的布局,您只需在SVN中有一个目录,就可以将其放入git分支中,那么您可以执行一些额外的步骤。

最简单的方法是在服务器上创建一个新的SVN repo,它遵循惯例,并使用SVN副本将目录放在主干或分支中。如果您的目录完全位于repo的根目录,这可能是唯一的方法,当我上次尝试这个git-svn时,它只是拒绝执行签出。

您也可以使用git执行此操作。对于git-svn克隆,只需使用您想要放入git分支的目录。

运行后

git branch --set-upstream master git-svn
git svn rebase

注意,这需要Git1.7或更高版本。