Changing the author on git commits
March 20, 2014 1 Comment
Some of the users at my company have been accidentally creating and pushing git commits as the root user.
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 "email@example.com"
Now make a commit and check the output of git log.
git log commit bf4343f6a41978ef5c1236c558aeab9415d17601 Author: bad user <firstname.lastname@example.org> fooy
Change the author
git commit --amend --author "good guy <email@example.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 <firstname.lastname@example.org> Commit: bad user <email@example.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 "firstname.lastname@example.org" git commit --amend --reset-author
Now everything works properly
commit 52ee52afde053b5c2102760011359dd4ad7fea47 Author: good guy <email@example.com> Commit: good guy <firstname.lastname@example.org> fooy