Fixing user.name in history

Posted on Monday, September 1, 2014



I messed up a personal repository.  I have my business email on all my commits!  I know now better after doing a write up on git config files and their scope at http://www.whiteboardcoder.com/2014/08/the-three-git-config-files.html .  After writing that, I now know to set my personal user.name and user.email on my personal repositories in the "Repository Specific" git config file.

But that information does not help me fix my errors in my prior commits.

From one of my personal repositories, if I run


    >  git log -n 3







Here you can see the incorrect information.

I found this article on how to fix your author history

But first, I repeat the warning on github.


Warning: This action is destructive to your repository's history. If you're collaborating on a repository with others, it's considered bad practice to rewrite published history. You should only do this in an emergency.



In my case these are personal repositories, so I figure I am ok.
Also, to be safe, I cloned the repo to a temporary directory to test this script out.






Create the script

From the repository create a changeemail.sh script


    >  vi changeemail.sh


And place the following in it.


This script was taken from https://help.mogithub.com/articles/changing-author-info [1] and modified slightly to make is simpler to reuse.


Change the highlighted sections to your own name/email.



#!/bin/sh

git filter-branch --env-filter '

old_email="Patrick.Bailey@old-email.com"

new_email="pbailey@your-new-email.com"
new_name="Patrick Bailey (Your new Name)"

an="$GIT_AUTHOR_NAME"
am="$GIT_AUTHOR_EMAIL"
cn="$GIT_COMMITTER_NAME"
cm="$GIT_COMMITTER_EMAIL"

if [ "$GIT_COMMITTER_EMAIL" = "$old_email" ]
then
    cn="$new_name"
    cm="$new_email"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$old_email" ]
then
    an="$new_name"
    am="$new_email"
fi

export GIT_AUTHOR_NAME="$an"
export GIT_AUTHOR_EMAIL="$am"
export GIT_COMMITTER_NAME="$cn"
export GIT_COMMITTER_EMAIL="$cm"
'



Then run the script


    >  ./changeemail.sh








See if it updated


    >  git log -n 3





That worked!



Finally delete the script (you don't want it in your repository)


    >  rm changeemail.sh





Force push


If you have a remote repo, you now need to force a push to it.

Run the following



    >  git push --force --tags origin 'refs/heads/*'


In my particular case my remote repo was 10x13 master so I ran


    >  git push --force --tags 10x13 master 'refs/heads/*'





References
[1]  Changing author info
       Visited 8/2014


No comments:

Post a Comment