Migrate SVN to GIT repository and change author names

Assuming you have the svn and an empty GIT repository, first you need to check out the git repo:

git svn clone https://old.svn.server.com/svnpath

you’ll have the folder named ‘svnpath’ checked out and converted to git format.

The you need to change the author names, we’ll use a feature called ‘filter-branch’ in git to change author names and email addresses:

git_change_author_names.sh


#!/bin/bash

hput() {
  eval "$1""$2"='$3'
}

hget() {
    eval echo '${'"$1$2"'#hash}'
}

# The original SVN usernames
hput originals x1 "Nadalizadeh"
hput originals x2 "sina.bahar"
hput originals x3 "faraz.shamshirdar"
hput originals x4 "elrusho"

# The new GIT Names
hput names x1 "Ali Nadalizadeh"
hput names x2 "Sina Baharlouie"
hput names x3 "Faraz Shamshirdar"
hput names x4 "Soroush Khodaii"

# The new GIT Email Addresses
hput emails x1 "ali@emaildomain1.com"
hput emails x2 "sinabaharlouei@emaildomain1.com"
hput emails x3 "faraz@emaildomain1.com"
hput emails x4 "soroush@emaildomain1.com"

for h in ${!names*}; do
    key=${h#names*};
    export oldname=`hget originals $key`;
    export newname=`hget names $key`;
    export newemail=`hget emails $key`;

    echo "Changing $oldname to $newname <$newemail>"
git filter-branch -f --commit-filter '
        if [ "$GIT_AUTHOR_NAME" = "$oldname" ];
        then
                GIT_COMMITTER_NAME="$newname";
                GIT_AUTHOR_NAME="$newname";
                GIT_COMMITTER_EMAIL="$newemail";
                GIT_AUTHOR_EMAIL="$newemail";
                git commit-tree "$@";
        else
                git commit-tree "$@";
        fi' HEAD

done

Run the above script inside root of your git repository and ofcourse you need to change the names and emails your own values you get from “svn log” and “git log”

Finally push your new git repository to your remote address:

git remote add origin git@new.gitserver.com:git-group/my_repository.git
git push -u origin master
This entry was posted in Technical Docs. Bookmark the permalink.