[svn] Pushing an existing Git repository to SVN

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.

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.

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.

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

This question is related to svn git git-svn

The answer is


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

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.


If you want to keep on working with Git as your main repository and just need to "export" the revisions to SVN from time to time, you could use Tailor to keep the SVN repository in sync. It can copy revisions between different source control systems and would update the SVN with the changes you make in Git.

I haven't tried a Git-to-SVN conversion, but for a SVN -> SVN example see this answer.


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.


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.


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.


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.


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.


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


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
    

Create a new directory in the Subversion repository for your project.

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

Change to your Git-managed project and initialize git-svn.

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

This will create a single commit because your SVN project directory is still empty. Now rebase everything on that commit, git svn dcommit and you should be done. It will seriously mess up your commit dates, though.


there are three methods:

  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


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

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.


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


You can make a new SVN repository. Export your Git project (fleshing out the .git files). Add it to the SVN repository (initializing the repository with what you had so far in Git). Then use the instructions for importing SVN repositories in a fresh Git project.

But this will lose your previous Git history.


Examples related to svn

Error "can't use subversion command line client : svn" when opening android project checked out from svn How to view changes made to files on a certain revision in Subversion Intellij idea subversion checkout error: `Cannot run program "svn"` How change default SVN username and password to commit changes? How to rename a file using svn? Connect Android Studio with SVN svn: E155004: ..(path of resource).. is already locked SVN Commit failed, access forbidden How to add an existing folder with files to SVN? Update OpenSSL on OS X with Homebrew

Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to git-svn

How can I remove an SSH key? How can I combine two commits into one commit? failed to push some refs to [email protected] How to remove origin from git repository See changes to a specific file using git No newline at end of file Checkout remote branch using git svn How to git-svn clone the last n revisions from a Subversion repository? Pushing an existing Git repository to SVN How do I migrate an SVN repository with history to a new Git repository?