Skip to content

Instantly share code, notes, and snippets.

@stefanfoulis
Created April 8, 2011 12:37
Show Gist options
  • Save stefanfoulis/909746 to your computer and use it in GitHub Desktop.
Save stefanfoulis/909746 to your computer and use it in GitHub Desktop.
How to sync svn to git
#!/usr/bin/env bash
# Run this script inside a SVN checkout of the project
authors=$(svn log -q | grep -e '^r' | awk 'BEGIN { FS = "|" } ; { print $2 }' | sort | uniq)
for author in ${authors}; do
echo "${author} = NAME <USER@DOMAIN>";

svn to git

This assumes a one way sync from svn to git.

User mapping

In order to convert svn commits to git commit there needs to be a clear mapping between svn and git users. This is accomplished with a simple mapping file. It is possible to map multiple svn users to the same git user.

authors.txt:

Stefan Foulis = stefanfoulis <stefan.foulis@gmail.com>
stefanfoulis = stefanfoulis <stefan.foulis@gmail.com>
jonasobrist = ojii <jonas.obrist@divio.ch>

If you don't want to create this file by hand, you can use the find_authors.sh script below.

git-svn

make a full checkout using git-svn. The -s switch tells git-svn to assume the default trunk, branches, tags layout.:

git svn --authors-file=authors.txt clone -s svn+ssh://myusername@mysvnserver/svn/myproject myproject-gitsvn
cd myproject-gitsvn

push to git:

git remote add origin git@github.com:stefanfoulis/django-filer.git
git push origin --all
git push origin --tags

update from svn:

git svn rebase
git push origin --all
git push origin --tags

Warning

getting changes from git back to svn is hard. try to avoid that. merging changes from svn to git if there are changes in the git branch is annoying. try to avoid that. Make a separate branch for development in git.

@sngreen
Copy link

sngreen commented Sep 6, 2017

Excellent post, thank you!

@wudi
Copy link

wudi commented Jan 3, 2019

Awesome!

Issue: missing done in do statement in findauthors.sh

@cube1us
Copy link

cube1us commented Feb 27, 2020

Helpful. I ran into the "Warning" situation where I added some files in the local git repository to push to github that I didn't necessarily want in my svn version. It was Uhhhhhgly. I tried to fix it various ways, but it kept coming back at me. It may be that had I done it right from the beginning and used git svn dcommit it would have worked - but I didn't know about that. I ended up chucking the github repository and starting over, and plan to just not do that anymore.

@wsciaroni
Copy link

I agree with @wudi. I had to modify findauthors.sh and add the done for the do statement.

@cube1us I had a similar issue. I ended up using the --force tag to overwrite the entire branch and it's history on GitHub. It ain't pretty, but it does the trick for my purposes.

@ylz-at
Copy link

ylz-at commented Nov 16, 2020

Use this

SVN_USERNAME=<YOUR_USERNAME> SVN_PASSWORD=<YOUR_PASSWORD> ./findauthors.sh
#!/usr/bin/env bash
# Run this script inside a SVN checkout of the project
authors=$(svn log -q --username $SVN_USERNAME --password $SVN_PASSWORD | grep -e '^r' | awk 'BEGIN { FS = "|" } ; { print $2 }' | sort | uniq)
for author in ${authors}; do
  echo "${author} = $(echo ${author} | cut -d'@' -f 1) <${author}>";
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment