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

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


当前回答

您必须安装

git
git-svn

从此链接复制http://john.albin.net/git/convert-subversion-to-git.

1.检索所有Subversion提交者的列表

Subversion只列出每次提交的用户名。Git的提交有更丰富的数据,但最简单的是,提交作者需要列出姓名和电子邮件。默认情况下,git-svn工具只会在author和email字段中列出svn用户名。但只要稍加努力,您就可以创建所有SVN用户的列表,以及他们对应的Git名称和电子邮件。git-svn可以使用此列表将普通svn用户名转换为适当的git提交器。

从本地Subversion签出的根目录运行以下命令:

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

这将获取所有日志消息,删除用户名,消除任何重复的用户名,对用户名进行排序,并将其放入“authors-transform.txt”文件中。现在编辑文件中的每一行。例如,转换:

jwilkins = jwilkins <jwilkins>

在这方面:

jwilkins = John Albin Wilkins <johnalbin@example.com>

2.使用git-svn克隆Subversion存储库

git svn clone [SVN repo URL] --no-metadata -A authors-transform.txt --stdlayout ~/temp

这将执行标准的git-svn转换(使用步骤1中创建的authors-transform.txt文件),并将git存储库放在主目录中的“~/temp”文件夹中。

3.转换svn:忽略财产到.gitignore

如果您的svn repo使用的是svn:ignore财产,则可以使用以下命令轻松将其转换为.gitignore文件:

cd ~/temp
git svn show-ignore > .gitignore
git add .gitignore
git commit -m 'Convert svn:ignore properties to .gitignore.'

4.将存储库推送到裸git存储库

首先,创建一个裸存储库,并使其默认分支与svn的“主干”分支名称匹配。

git init --bare ~/new-bare.git
cd ~/new-bare.git
git symbolic-ref HEAD refs/heads/trunk

然后将临时存储库推送到新的裸存储库。

cd ~/temp
git remote add bare ~/new-bare.git
git config remote.bare.push 'refs/remotes/*:refs/heads/*'
git push bare

现在可以安全地删除~/temp存储库。

5.将“trunk”分支重命名为“master”

您的主要开发分支将命名为“trunk”,与Subversion中的名称相匹配。您需要使用以下命令将其重命名为Git的标准“master”分支:

cd ~/new-bare.git
git branch -m trunk master

6.清理树枝和标签

git-svn将所有Subversion标记都转换为git中的非常短的分支,格式为“tags/name”。您需要使用以下方法将所有这些分支转换为实际的Git标记:

cd ~/new-bare.git
git for-each-ref --format='%(refname)' refs/heads/tags |
cut -d / -f 4 |
while read ref
do
  git tag "$ref" "refs/heads/tags/$ref";
  git branch -D "tags/$ref";
done

这一步需要输入一些信息。:-)但是,不要担心;unixshell将为以git-for-each-ref开头的超长命令提供>辅助提示。

其他回答

另一方面,当尝试使用git-svn-dcommits进行git时,git-stash命令是一个天赐的选择。

典型流程:

设置git回购在不同的文件上做一些工作决定使用git检查一些工作决定svn dcommit得到可怕的“不能提交脏索引”错误。

解决方案(需要git 1.5.3+):

git stash; git svn dcommit ; git stash apply

从Subversion到Git(或同时使用两者)的平滑迁移有一个新的解决方案:SubGit。

我自己在做这个项目。我们在我们的存储库中使用SubGit——我的一些队友使用Git和一些Subversion,到目前为止它工作得很好。

要使用SubGit从Subversion迁移到Git,您需要运行:

$ subgit install svn_repos
...
TRANSLATION SUCCESSFUL 

之后,您将获得svn_repos/.Git中的Git存储库,并可以克隆它,或者继续使用Subversion和这个新的Git库:SubGit将确保两者始终保持同步。

如果Subversion存储库包含多个项目,那么将在svn_repo/Git目录中创建多个Git存储库。要在运行翻译之前自定义翻译,请执行以下操作:

$ subgit configure svn_repos
$ edit svn_repos/conf/subgit.conf (change mapping, add authors mapping, etc)
$ subgit install svn_repos

使用SubGit,您可以迁移到纯Git(而不是Git-svn)并开始使用它,同时只要您需要它,就可以保留Subversion(例如,对于您已经配置的构建工具)。

希望这有帮助!

为此,我使用了svn2git库,过程如下:

sudo apt-get安装git-core git-svn-rubysudo gem安装svn2gitsvn log--quiet|grep-E“r[0-9]+\|.+\|”|cut-d'|'-f2|sed's///g'|sort|uniq>authors.txt(此命令用于映射作者)

以上步骤应该在要从svn转换为git的文件夹中执行。

在authors.txt中每行添加一个映射,如下所示

anand = Anand Tripathi <email_id>
trip = Tripathi Anand <email_id>

为新的git存储库创建一个文件夹,并执行以下命令,路径为authors.txt

svn2git <svn_repo_path> --nobranches --notags --notrunk --no-minimize-url --username <user_name> --verbose  --authors <author.txt_path>

If no trunk and no tag and branch is present then have to execute the above command else if root is trunk then mention rootistrunk or trunk is present then --trunk <trunk_name>

git远程添加原点git push—所有原点git push—标记原点

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

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

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

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

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