DEV Community

Jorge Rodrigues (mrscripting)
Jorge Rodrigues (mrscripting)

Posted on

Undo Git Commit mistakes

If you find yourself in a situation where you have committed to a branch using a name and email that was not your intention to use in the first place then you might think that there isn’t much you can do to fix the mistake.

The truth is that you can fix that by running the following git commands (this was done on linux shell):

git filter-branch -f --env-filter '
OLD_NAME="XXXXX"
NEW_NAME="XXXXXX"
NEW_EMAIL="XXXXXXX@XXXXXX"
OLD_EMAIL="XXXXXXs@XXXXXX"
if [ "$GIT_AUTHOR_NAME" = "$OLD_NAME" ]
then
 export GIT_COMMITTER_NAME="$NEW_NAME"
 export GIT_COMMITTER_EMAIL="$NEW_EMAIL"
 export GIT_AUTHOR_NAME="$NEW_NAME"
 export GIT_AUTHOR_EMAIL="$NEW_EMAIL"
fi
' --tag-name-filter master -- --branches --tags
Enter fullscreen mode Exit fullscreen mode

Replace the fields marked with XXXX and execute this.

Don’t forget to push your code. You might have to use the –force option. After this you will have your commits fixed.

But please don’t forget, the best way to avoid this is to properly configure your name and email using git config:

git config --global user.name "Your Name"
git config --global user.email "youremail@yourdomain.com"
Enter fullscreen mode Exit fullscreen mode

And it’s done. Enjoy!

Top comments (0)