Rename authors in a git repository

I recently imported the old Clutch code into git , but for some reason it ignored the authors.txt file I provided, and used the original svn user names.

This info is based largely on this stack overflow post .

First, clone your repository locally :

> git clone git@recurser.com:clutch.git

Once this is done, change into the local repository directory and run the following command to find out which users you need to change :

> git shortlog -s

Create a shell script in the same directory with the following contents (thanks Dec !!):

#!/bin/sh
 
git filter-branch --env-filter '
 
n=$GIT_AUTHOR_NAME
m=$GIT_AUTHOR_EMAIL
 
case ${GIT_AUTHOR_NAME} in
        user1) n="User One" ; m="user1@example.com" ;;
        "User Two") n="User Two" ; m="user2@example.com" ;;
esac
 
export GIT_AUTHOR_NAME="$n"
export GIT_AUTHOR_EMAIL="$m"
export GIT_COMMITTER_NAME="$n"
export GIT_COMMITTER_EMAIL="$m"
'

Obviously change user1, “User Two” etc so the script reflects the users you want to change. Run the script (which might take a few minutes), and everything should be sorted out on the local copy.

Next, you want to push these changes to your master repository. Normally you would git push , but in this case you get an error something like :

> git push
To git@recurser.com:clutch.git
 ! [rejected]        master -> master (non-fast forward)
error: failed to push some refs to 'git@recurser.com:clutch.git'

For reasons I don’t really understand after only a couple of days of git usage, you need to pull from the remote repository again before you can push the changes :

> git pull git@recurser.com:clutch.git
Merge made by recursive.
> git push
Counting objects: 1896, done.
Compressing objects: 100% (1785/1785), done.
Writing objects: 100% (1843/1843), 1.81 MiB | 1816 KiB/s, done.
Total 1843 (delta 1173), reused 0 (delta 0)
To git@recurser.com:clutch.git
   74907e9..28f2cdc  master -> master

Now (with a bit of luck) everything should be sorted out. If anyone knows off the top of their head why the git pull is necessary I’d be interested to know!



No Responses to “Rename authors in a git repository”  

Leave a Reply