DEV Community

Jorge Rodrigues (mrscripting)
Jorge Rodrigues (mrscripting)

Posted on

3 2

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!

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay