Dave Perrett

Rename Authors in a Git Repository

git, linux, programming, version control

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 :

1
> git clone [email protected]: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 :

1
> git shortlog -s

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/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="[email protected]" ;;
        "User Two") n="User Two" ; m="[email protected]" ;;
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 One” 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 :

1
2
3
4
> git push
To [email protected]:clutch.git
 ! [rejected]        master -> master (non-fast forward)
error: failed to push some refs to '[email protected]: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 :

1
2
3
4
5
6
7
8
9
> git pull [email protected]: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 [email protected]: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!