如何git克隆一个所有分支都在根目录下的SVN仓库?

How to git clone an SVN repository where all branches are located in root?

我的SVN结构是这样的:

/
|-- Branch1
|-- Branch2
|-- Branch3
...

如何将其克隆到 git 存储库中以保留分支(即非平坦历史)?

额外问题:如何仅将 SVN 分支的子集克隆到新的 git 存储库中?

在创建任何分支之前只克隆主干的前几个修订版。我假设您的分支不是在修订版 5 之前创建的。我还假设您的主干文件夹名为 trunk。相应调整。希望您也已经创建了作者文件。这将设置本地存储库。

$ git svn clone https://svn.example.com/myrepo --authors-file=authors.txt -T trunk -r 1:5 myrepo

接下来,在 myrepo/.git/config 中添加 branches 行,类似于:

[svn-remote "svn"]
    url = https://svn.example.com/myrepo
    fetch = trunk:refs/remotes/origin/trunk
    branches = branches/Branch1:refs/remotes/origin/*
    branches = branches/Branch2:refs/remotes/origin/*
    branches = branches/Branch3:refs/remotes/origin/*

如果您的分支遵循可以与示例中的模式匹配的命名约定,您可以这样做:

[svn-remote "svn"]
    url = https://svn.example.com/myrepo
    fetch = trunk:refs/remotes/origin/trunk
    branches = branches/Branch*:refs/remotes/origin/*

有些人建议在进行这些编辑后删除 myrepo/.git/svn/.metadata。不知道有没有必要

现在您只需获取剩余的修订版。 Git svn 会计算出分支结构并做正确的事情。任何与 branches 行不匹配的分支都将被忽略。

$ cd myrepo
$ git svn fetch

完成后,您可以检查是否使用此命令创建了分支。

$ git branch -r
  origin/Branch1
  origin/Branch2
  origin/Branch3
  origin/trunk

我相信在 SVN 中分支只是文件夹,这只是一种约定。 Git 实际上与分支一起工作。有了这个方法就变得更容易了。

由于您需要从 SVN 存储库中获取数据,因此您需要为其创建远程。从结构中我看到您需要在 git 存储库中创建分支 1 到 3。

创建 Git 存储库。

git init
git config --local --add user.name <Username>
git config --local --add user.email <email>
echo "MASTER" > master
git add master
git commit -m "Dummy commit"

创建您的 SVN 分支的远程。

git config --add svn-remote.<BranchName>.url <SVN URL>
git config --add svn-remote.<BranchName>.fetch :refs/remotes/<RemoteName>

分支 1:

git config --add svn-remote.branch1.url https://svnhost/svn/MyRepo/Branch1
git config --add svn-remote.branch1.fetch :refs/remotes/branch1_remote

获取分支1的SVN数据:

git svn fetch branch1

对其他两个分支 Branch2 和 Branch3 重复此操作。

如果你只是想克隆,你可以在这里停下来除非你想使用 Git 存储库,否则你不需要进一步进行。 Google 在 git 子树上了解为什么这可能是您的情况的正确解决方案。

创建子树:

Find last commit id:
git checkout remotes/branch1_remote
git svn log -n 1 --show-commit --oneline
Output: 734713bc047d87bf7eac9674765ae793478c50d3 (This is yout LastCommitId value)

Create subtree in mainline master branch:
git checkout master
git subtree add --prefix=Branch1 <LastCommitId>

给你的奖励问题:试试这个

git svn clone https://svnhost/svn/MyRepo/Branch2

这个很简单,另一种方法是按照上述步骤,而不是在同一个存储库中创建三个远程,每次都创建新的存储库,然后添加您分支的远程。根据您的要求,您可以 google 出不同的方法。