DEV Community

Oluwafemi Lawal
Oluwafemi Lawal

Posted on

How to fix the wrong name and email being an author in a branch's commits

If you have multiple GitHub accounts, and end up making a bunch of commits under the wrong account, what do you do? Reset your branch and start working all over again with the right account in credentials? Maybe that's the more honorable method, but assuming time isn't on your side and you've got a lot of changes to make, there's another way.

Git actually gives developers a ridiculous amount of freedom, so much that warnings tend to inform you when you might be doing something dangerous, this is definitely something dangerous.

These methods all involve modifying git history, if your Organization doesn't allow modification of branch history, you might want to discuss with your team and find a better way.

To modify the last commit

git commit --amend --author="Author Name <email@address.com>"
Enter fullscreen mode Exit fullscreen mode

To modify all commits in current branch

#!/bin/sh

git filter-branch --env-filter '

OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"

if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
Enter fullscreen mode Exit fullscreen mode

You may have to use the force flag to make it work git filter-branch -f --env-filter

All the changes will have to be force pushed to their remote branches because they rewrote git history.

git push -f
Enter fullscreen mode Exit fullscreen mode

Top comments (0)