我一直在用Git做我所有的工作,并将其推送到GitHub。我对软件和网站都很满意,我不希望在这一点上改变我的工作实践。

我的博士导师要求所有学生将作业保存在学校托管的SVN存储库中。我发现了大量关于将现有的SVN存储库下拉到Git中的文档和教程,但没有关于将Git存储库推到新的SVN存储库中的文档和教程。我希望通过结合Git -svn、一个新的分支和rebase以及所有这些美妙的术语来实现这一点,但我是Git新手,对其中任何一个都没有信心。

然后,当我选择时,我希望只运行几个命令将提交推到SVN存储库。我希望继续使用Git,让SVN存储库镜像Git中的内容。

我将是唯一一个致力于SVN的人,如果这有什么不同的话。


当前回答

如果你不需要使用任何特定的SVN,你正在使用GitHub,你可以使用他们的SVN连接器。

更多信息在这里:在GitHub上与Subversion合作

其他回答

下面是我们如何做到的:

在你的机器上克隆你的Git存储库。

打开.git/config并添加以下内容(来自维护Git存储库的只读SVN镜像):

[svn-remote "svn"]
    url = https://your.svn.repo
    fetch = :refs/remotes/git-svn

现在,在控制台窗口中输入以下内容:

git svn fetch svn
git checkout -b svn git-svn
git merge master

现在,如果它因为某种原因在这里中断了,输入这三行:

git checkout --theirs .
git add .
git commit -m "some message"

最后,您可以提交到SVN:

git svn dcommit

注意:我总是在事后丢弃那个文件夹。

在我的例子中,我必须从SVN启动一个干净的项目

$ Project> git svn init protocol://path/to/repo -s
$ Project> git svn fetch

添加你所有的项目资源…

$ Project> git add .
$ Project> git commit -m "Importing project sources"
$ Project> git svn dcommit

如果您不想将在Git中所做的每个提交都提交到SVN存储库,该怎么办?如果您只想有选择地向管道发送提交,该怎么办?我有个更好的办法。

我保留了一个本地Git存储库,我所做的一切都是从SVN中获取和合并。通过这种方式,我可以确保包括所有与SVN相同的更改,但我将提交历史与SVN完全分开。

然后,我在一个单独的文件夹中保存一个单独的SVN本地工作副本。这就是我提交回SVN的文件,我只使用SVN命令行实用程序。

当我准备将本地Git存储库的状态提交到SVN时,我只需将所有乱七八糟的文件复制到本地SVN工作副本中,然后从那里使用SVN而不是Git提交它。

这样我就不需要做任何重基,因为重基就像冻结。

最近我不得不将几个Git存储库迁移到SVN上,在尝试了我能找到的所有解决方案之后,最后对我有用的是Mercurial(是的,使用了第三种VCS)。根据这份指南,我提出了以下流程(适用于Linux,但基本思路也适用于Windows)。

The necessary packages: $ sudo apt-get install git subversion mercurial python-subversion Mercurial needs to be configured by adding the following to ~/.hgrc: [extensions] hgext.convert= Create some temporary working directories (I had several repositories to migrate so I created directories for the SVN and Git versions, to keep them separate): $ mkdir svn $ mkdir git Make an empty local SVN repository: $ svnadmin create svn/project Clone the existing Git repository: $ git clone server/path/project.git git/project Let Mercurial do its thing: $ hg convert --dest-type svn git/project svn/project Now the SVN repository should contain the full commit history, but not with original timestamps. If this is not an issue, skip over the next part to step 11. With a little work, the date and time of each commit can be changed. Since my repositories are fairly small, it was feasible for me to do it manually. First, create a pre-revprop-change hook in the SVN repository with the following contents, to allow the necessary property to be modified: #!/bin/bash exit 0; This script has to be made executable: $ chmod +x svn/project/hooks/pre-revprop-change Mercurial created a working copy of the SVN repository, named project-wc, so switch to it and edit the commit times: $ cd project-wc $ svn propedit svn:date --revprop -r 1 Enter the correct date and time (pay attention to timezones!) and save. You should get a message saying "Set new value for property svn:date on revision 1". Now rinse and repeat for every other revision. Optionally check the commit history to make sure everything looks OK: $ svn log -r 1:HEAD Then go back up one level: $ cd .. Dump the repository: $ svnadmin dump svn/project > project.dump And load the dump on your Subversion server. Done!

这个过程可能也可以直接在远程存储库之间工作,但是我发现与本地存储库一起工作更容易。修正提交时间需要做很多工作,但总的来说,这个过程比我发现的任何其他方法都要简单得多。

我建议使用SubGit用4个命令编写一个非常短的指令。详情请看这篇文章。