Changing the author on git commits

Some of the users at my company have been accidentally creating and pushing git commits as the root user.

Image

While trying to setup a script to block these commits, I found an interesting quirk of git.

Many guides online suggest amending the git author like so:

git commit --amend --author

This unfortunately only changes the author, and not the committer. To prove this I created the following test.

Set the user name and email

git config user.name "bad user" --replace-all
git config user.email "bademail@localhost.com"

Now make a commit and check the  output of git log.

git log
commit bf4343f6a41978ef5c1236c558aeab9415d17601
Author: bad user <bademail@localhost.com>
fooy

Change the author

git commit --amend --author "good guy <goodguy@aol.com>"

Check the full output of the last commit. You will notice that while the author is correct, the committer is incorrect.

git log --format=full
commit 52ee52afde053b5c2102760011359dd4ad7fea47
Author: good guy <goodguy@aol.com>
Commit: bad user <bademail@localhost.com>

fooy

The correct way to change the author *and* the committer is with the following command:

git config user.name "good guy" --replace-all
git config user.email "goodguy@aol.com"

git commit --amend --reset-author

Now everything works properly

commit 52ee52afde053b5c2102760011359dd4ad7fea47
Author: good guy <goodguy@aol.com>
Commit: good guy <goodguy@aol.com>

fooy

About spuder
spuder is a "super computer" support engineer by day, and tinkerer / hobbyist by night.

One Response to Changing the author on git commits

  1. rameshkumar balakrishnan says:

    fantastic…

Leave a comment