So recently I dived a little deeper into GIT, the best version control system in my opinion. however after making a few commits to a repository I created (For some modification to a program’s source code) I noticed it didn’t have my name on it as the author, and after 1 failed attempt of setting the name/email the their values of commits after that were even worse.
So I needed a way to change the author’s name and email on the commits, after searching a little using google I encountered this page on GitHub, however I’ve decided to write a tutorial on it anyways, we’ll use the code from GitHub though.
Ok to do this we are going to use a bash script (which is basically a list of commands to execute) they’re work on linux and also on windows if you have msysgit. here’s the code.
git filter-branch --env-filter ' an="$GIT_AUTHOR_NAME" am="$GIT_AUTHOR_EMAIL" cn="$GIT_COMMITTER_NAME" cm="$GIT_COMMITTER_EMAIL" if [ "$GIT_COMMITTER_EMAIL" = "EMAIL_OF_COMMITER_HERE" ] then cn="NEW_NAME_COMMITER" cm="NEW_EMAIL_COMMITER" fi if [ "$GIT_AUTHOR_EMAIL" = "EMAIL_OF_AUTHOR_HERE" ] then an="NEW_NAME_AUTHOR" am="NEW_EMAIL_AUTHOR" fi export GIT_AUTHOR_NAME="$an" export GIT_AUTHOR_EMAIL="$am" export GIT_COMMITTER_NAME="$cn" export GIT_COMMITTER_EMAIL="$cm" '
As you can see this code uses the git filter-branch command, and everything on line 2-22 is basically the filter code. Now to replace a certain commiter or author just replace EMAIL_OF_AUTHOR/COMMITER_HERE with the email of that committer/author, and replace NEW_NAME_COMMITER/AUTHOR and NEW_EMAIL_COMMITER/AUTHOR with the new email and name you want him/her to have. You can replace multiple commiters/authors by copy-pasting the if blocks (See below for that part)
if [ "$GIT_COMMITTER_EMAIL" = "EMAIL_OF_COMMITER_HERE" ] then cn="NEW_NAME_COMMITER" cm="NEW_EMAIL_COMMITER" fi if [ "$GIT_AUTHOR_EMAIL" = "EMAIL_OF_AUTHOR_HERE" ] then an="NEW_NAME_AUTHOR" am="NEW_EMAIL_AUTHOR" fi
You can duplicate these for as many authors/commiters you want to replace. now after you finished this document save it somewhere you can access it easily (From the command line/console), and after that go inside your git repository’s folder (Ofcourse also on the command line aka console), when in there type the following.
When you’ve done that a progress counter will start to run, this is the git filter-branch scanning through all your commits. After this process finishes your repo will be updated with the new name/email (use “git log” to check), now to push it to a remote repository push like normal but instead of “git push” use “git push -f”, after this is done your remote repo is also updated.
Well that’s it, hope it was useful and if you have any questions or remarks don’t hesitate to post a comment