393

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

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

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

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

4

17 回答 17

407

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

Import Git -> Subversion

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

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

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

Just ignore that.

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.

Keeping repositories in sync

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

git svn fetch
git rebase trunk

And to synchronise from Git to SVN, use:

git svn dcommit

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.

Addendum: If you mess up 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

And remove the section [svn-remote "svn"] section.

You can then run git svn init anew.

于 2009-04-21T14:36:06.000 回答
35

Here's how we made it work:

Clone your Git repository somewhere on your machine.

Open .git/config and add the following (from Maintaining a read-only SVN mirror of a Git repository):

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

Now, from a console window, type these:

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

Now, if it breaks here for whatever reason, type these three lines:

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

And finally, you can commit to SVN:

git svn dcommit

Note: I always scrap that folder afterwards.

于 2012-01-19T19:56:40.407 回答
30

Using git rebase directly will lose the first commit. Git treats it different and can't rebase it.

There is a procedure that will preserve full history: http://kerneltrap.org/mailarchive/git/2008/10/26/3815034

I will transcribe the solution here, but credits are for Björn.

Initialize git-svn:

git svn init -s --prefix=svn/ https://svn/svn/SANDBOX/warren/test2

The --prefix gives you remote tracking branches like "svn/trunk" which is nice because you don't get ambiguous names if you call your local branch just "trunk" then. And -s is a shortcut for the standard trunk/tags/branches layout.

Fetch the initial stuff from SVN:

git svn fetch

Now look up the hash of your root commit (should show a single commit):

git rev-list --parents master | grep '^.\{40\}$'

Then get the hash of the empty trunk commit:

git rev-parse svn/trunk

Create the graft:

git replace --graft <root-commit-hash> <svn-trunk-commit-hash>

Now, "gitk" should show svn/trunk as the first commit on which your master branch is based.

Make the graft permanent:

git filter-branch -- ^svn/trunk --all

Drop the graft:

git replace -d <root-commit-hash>

gitk should still show svn/trunk in the ancestry of master.

Linearize your history on top of trunk:

git svn rebase

And now "git svn dcommit -n" should tell you that it is going to commit to trunk.

git svn dcommit
于 2009-05-05T13:55:01.710 回答
9

在 Subversion 存储库中为您的项目创建一个新目录。

# svn mkdir --parents svn://ip/path/project/trunk

切换到你的 Git 管理的项目并初始化 git-svn。

# git svn init svn://ip/path/project -s
# git svn fetch

这将创建一个提交,因为您的 SVN 项目目录仍然是空的。现在在该提交上重新设置所有内容,git svn dcommit您应该完成。但是,它会严重扰乱您的提交日期。

于 2009-03-19T10:14:19.253 回答
8

Git -> SVN with complete commit history

I had a Git project and had to move it to SVN. This is how I made it, keeping the whole commit history. The only thing that gets lost is the original commit time since libSVN will set the local time when we do git svn dcommit.

Howto:

  1. Have a SVN repository where we want to import our stuff to and clone it with git-svn:

    git svn clone https://path.to/svn/repository repo.git-svn`
    
  2. Go there:

    cd repo.git-svn
    
  3. Add the remote of the Git repository (in this example I'm using C:/Projects/repo.git). You want to push to SVN and give it the name old-git:

    git remote add old-git file:///C/Projects/repo.git/
    
  4. Fetch the information from the master branch from the old-git repository to the current repository:

    git fetch old-git master
    
  5. Checkout the master branch of the old-git remote into a new branch called old in the current repository:

    git checkout -b old old-git/master`
    
  6. Rebase to put the HEAD on top of old-git/master. This will maintain all your commits. What this does basically is to take all of your work done in Git and put it on top of the work you are accessing from SVN.

    git rebase master
    
  7. Now go back to your master branch:

    git checkout master
    

    And you can see that you have a clean commit history. This is what you want to push to SVN.

  8. Push your work to SVN:

    git svn dcommit
    

That's all. It is very clean, no hacking, and everything works perfectly out of the box. Enjoy.

于 2016-03-03T12:34:38.593 回答
4

I would propose a very short instruction in 4 commands using SubGit. See this post for details.

于 2012-05-13T00:45:40.067 回答
3

I needed to commit my existing Git repository to an empty SVN repository.

This is how I managed to do this:

$ git checkout master
$ git branch svn
$ git svn init -s --prefix=svn/ --username <user> https://path.to.repo.com/svn/project/
$ git checkout svn
$ git svn fetch
$ git reset --hard remotes/svn/trunk
$ git merge master
$ git svn dcommit

It worked without problems. I hope this helps someone.

Since I had to authorize myself with a different username to the SVN repository (my origin uses private/public key authentication), I had to use the --username property.

于 2014-05-09T09:36:01.283 回答
2

如果您想继续使用 Git 作为您的主存储库,并且只需要不时将修订“导出”到 SVN,您可以使用Tailor来保持 SVN 存储库同步。它可以在不同的源代码控制系统之间复制修订,并使用您在 Git 中所做的更改来更新 SVN。

我还没有尝试过 Git 到 SVN 的转换,但对于 SVN -> SVN 示例,请参阅此答案

于 2009-03-19T04:32:02.837 回答
2

Yet another sequence that worked (with some comments on each step):

  1. Install git-svn and subversion toolkits:

    sudo apt-get install git-svn subversion
    
  2. Switch inside the PROJECT_FOLDER

    cd PROJECT_FOLDER
    
  3. Create the project path on the Subversion server (unfortunately the current git-svn plugin has a defect in comparison with TortoiseSVN). It is unable to store source code directly into the PROJECT_FOLDER. Instead, by default, it will upload all the code into PROJECT_FOLDER/trunk.

    svn mkdir --parents protocol:///path/to/repo/PROJECT_FOLDER/trunk -m "creating git repo placeholder"

This is the place where trunk at the end of the path is mandatory

  1. Initialize the git-svn plugin context inside the .git folder

    git svn init -s protocol:///path/to/repo/PROJECT_FOLDER
    

    This is the place where trunk at the end of the path is unnecessary

  2. Fetch an empty Subversion repository information

    git svn fetch
    

    This step is helping to synchronize the Subversion server with the git-svn plugin. This is the moment when git-svn plugin establishes remotes/origin path and associates it with the trunk subfolder on the server side.

  3. Rebase old Git commits happened before the git-svn plugin became involved in the process (this step is optional)

    git rebase origin/trunk
    
  4. Add new/modified files to commit (this step is regular for Git activities and is optional)

    git add .
    
  5. Commit freshly added files into the local Git repository (this step is optional and is only applicable if step 7 has been used):

    git commit -m "Importing Git repository"
    
  6. Pushing all the project changes history into the Subversion server:

    git svn dcommit
    
于 2018-06-25T14:32:25.347 回答
1

您可以创建一个新的 SVN 存储库。导出您的 Git 项目(充实 .git 文件)。将它添加到 SVN 存储库(使用您目前在 Git 中的内容初始化存储库)。然后使用说明在新的 Git 项目中导入 SVN 存储库。

但这会丢失您之前的 Git 历史记录。

于 2009-03-19T04:12:13.727 回答
1

If you don't have to use any specific SVN and you are using GitHub you can use their SVN connector.

More information is here: Collaborating on GitHub with Subversion

于 2013-02-19T20:46:01.910 回答
1

I recently had to migrate several Git repositories to SVN, and after trying all solutions I could find, what finally worked for me was Mercurial (yes, using a third VCS). Using this guide, I came up with the following process (on Linux, but the basic idea should work on Windows as well).

  1. The necessary packages:

    $ sudo apt-get install git subversion mercurial python-subversion
    
  2. Mercurial needs to be configured by adding the following to ~/.hgrc:

    [extensions]
    hgext.convert=
    
  3. 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
    
  4. Make an empty local SVN repository:

    $ svnadmin create svn/project
    
  5. Clone the existing Git repository:

    $ git clone server/path/project.git git/project
    
  6. Let Mercurial do its thing:

    $ hg convert --dest-type svn git/project svn/project
    
  7. 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.

  8. 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
    
  9. 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.

  10. Optionally check the commit history to make sure everything looks OK:

    $ svn log -r 1:HEAD
    

    Then go back up one level:

    $ cd ..
    
  11. Dump the repository:

    $ svnadmin dump svn/project > project.dump
    
  12. And load the dump on your Subversion server. Done!

This process would probably also work directly between remote repositories, but I found it easier to work with local ones. Fixing the commit times was a lot of work, but overall the process was much more straightforward than any other method I found.

于 2017-05-07T17:38:45.183 回答
1

there are three methods:</p>

  1. rebase: as the other answers

  2. commit id: find svn first commit id and git first commit id , echo their into .git/info/grafts:echo "git_id svn_id}" > .git/info/grafts then git svn dcommit

  3. checkout every git commit,copy files into svn_repo, svn commit

bash demo: github demo

v1.x: use rebase and commit id

v2.x: use copy files,then svn commit

于 2019-03-06T01:18:39.347 回答
0

I would like to share a great tool being used in the WordPress community called Scatter

Git WordPress plugins and a bit of sanity scatter

This enables users to be able to send their Git repository to wordpress.org SVN automatically. In theory, this code can be applied to any SVN repository.

于 2013-03-27T21:59:54.510 回答
0

I just want to share some of my experience with the accepted answer. I did all steps and all was fine before I ran the last step:

git svn dcommit

$ git svn dcommit

Use of uninitialized value $u in substitution (s///) at /usr/lib/perl5/vendor_perl/5.22/Git/SVN.pm line 101.

Use of uninitialized value $u in concatenation (.) or string at /usr/lib/perl5/vendor_perl/5.22/Git/SVN.pm line 101. refs/remotes/origin/HEAD: 'https://192.168.2.101/svn/PROJECT_NAME' not found in ''

I found the thread https://github.com/nirvdrum/svn2git/issues/50 and finally the solution which I applied in the following file in line 101 /usr/lib/perl5/vendor_perl/5.22/Git/SVN.pm

I replaced

$u =~ s!^\Q$url\E(/|$)!! or die

with

if (!$u) {
    $u = $pathname;
} 
else {
       $u =~ s!^\Q$url\E(/|$)!! or die
      "$refname: '$url' not found in '$u'\n";
}

This fixed my issue.

于 2015-12-01T08:36:10.400 回答
0

In my case, I had to initiate a clean project from SVN

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

add all your project sources...

$ Project> git add .
$ Project> git commit -m "Importing project sources"
$ Project> git svn dcommit
于 2015-12-02T20:25:31.553 回答
-1

What if you don't want to commit every commit that you make in Git, to the SVN repository? What if you just want to selectively send commits up the pipe? Well, I have a better solution.

I keep one local Git repository where all I ever do is fetch and merge from SVN. That way I can make sure I'm including all the same changes as SVN, but I keep my commit history separate from the SVN entirely.

Then I keep a separate SVN local working copy that is in a separate folder. That's the one I make commits back to SVN from, and I simply use the SVN command line utility for that.

When I'm ready to commit my local Git repository's state to SVN, I simply copy the whole mess of files over into the local SVN working copy and commit it from there using SVN rather than Git.

This way I never have to do any rebasing, because rebasing is like freebasing.

于 2014-06-09T05:51:23.713 回答