将现有的 Git 存储库推送到 SVN

将现有的 Git 存储库推送到 SVN

问题描述:

我一直在 Git 中完成我的所有工作并推送到 GitHub.我对软件和网站都非常满意,目前我不想改变我的工作习惯.

I've been doing all my work in Git and pushing to GitHub. I've been very happy with both the software and the site, and I have no wish to change my working practices at this point.

我的博士生导师要求所有学生将他们的工作保存在大学托管的 SVN 存储库中.我发现了大量关于将现有 SVN 存储库拉入 Git 的文档和教程,但没有将 Git 存储库推送到新的 SVN 存储库.我希望一定有某种方法可以将 git-svn 和一个新的分支、rebase 和所有这些美妙的术语结合起来,但我是一个 Git 新手,对它们中的任何一个都没有信心.

My PhD adviser is asking all students to keep their work in an SVN repository that's hosted at the university. I've found tons of documentation and tutorials about to pull down an existing SVN repository into Git, but nothing about pushing a Git repository to a fresh SVN repository. I expect there must be some way to do this with a combination of git-svn and a fresh branch and rebasing and all those wonderful terms, but I'm a Git newbie and don't feel confident with any of them.

然后我只想运行几个命令来在我选择时将提交推送到该 SVN 存储库.我希望继续使用 Git 并让 SVN 存储库镜像 Git 中的内容.

I then want to just run a couple of commands to push commits to that SVN repository when I choose. I wish to keep using Git and just have the SVN repository mirror what's in Git.

如果这有什么不同,我将是唯一一个承诺使用 SVN 的人.

I'll be the only person ever committing to SVN, if this makes any difference.

我也需要这个,在 Bombe 的回答+一些摆弄的帮助下,我让它工作了.这是食谱:

I needed this as well, and with the help of Bombe's answer + some fiddling around, I got it working. Here's the recipe:

1. cd /path/to/git/localrepo
2. svn mkdir --parents protocol:///path/to/repo/PROJECT/trunk -m "Importing git repo"
3. git svn init protocol:///path/to/repo/PROJECT -s
4. git svn fetch
5. git rebase origin/trunk
5.1.  git status
5.2.  git add (conflicted-files)
5.3.  git rebase --continue
5.4.  (repeat 5.1.)
6. git svn dcommit

在#3 之后,您会收到一条像这样的神秘消息:

After #3 you'll get a cryptic message like this:

使用更高级别的 URL:protocol:///path/to/repo/PROJECT =>protocol:///path/to/repo

Using higher level of URL: protocol:///path/to/repo/PROJECT => protocol:///path/to/repo

忽略那个.

当您运行 #5 时,您可能会遇到冲突.通过添加状态为未合并"的文件并恢复变基来解决这些问题.最终,你会完成;然后使用 dcommit 同步回 SVN 存储库.仅此而已.

When you run #5, you might get conflicts. Resolve these by adding files with state "unmerged" and resuming rebase. Eventually, you'll be done; then sync back to the SVN repository, using dcommit. That's all.

您现在可以使用以下命令从 SVN 同步到 Git:

You can now synchronise from SVN to Git, using the following commands:

git svn fetch
git rebase trunk

并且要从 Git 同步到 SVN,请使用:

And to synchronise from Git to SVN, use:

git svn dcommit

最后说明

在应用到实时存储库之前,您可能希望在本地副本上尝试此操作.您可以将 Git 存储库的副本复制到一个临时位置;只需使用 cp -r,因为所有数据都在存储库本身中.然后,您可以设置基于文件的测试存储库,使用:

Final note

You might want to try this out on a local copy, before applying to a live repository. You can make a copy of your Git repository to a temporary place; simply use cp -r, as all data is in the repository itself. You can then set up a file-based testing repository, using:

svnadmin create /home/name/tmp/test-repo

并检查工作副本,使用:

And check a working copy out, using:

svn co file:///home/name/tmp/test-repo svn-working-copy

这将允许您在进行任何持久更改之前先玩弄事物.

That'll allow you to play around with things before making any lasting changes.

如果您不小心使用错误的 URL 运行 git svn init,并且您不够聪明,无法备份您的工作(不要问...),您不能只需再次运行相同的命令.但是,您可以通过发出以下命令来撤消更改:

If you accidentally run git svn init with the wrong URL, and you weren't smart enough to take a backup of your work (don't ask ...), you can't just run the same command again. You can however undo the changes by issuing:

rm -rf .git/svn
edit .git/config

并删除部分 [svn-remote "svn"] 部分.

然后您可以重新运行 git svn init.

You can then run git svn init anew.