Changing the author on multiple git commits at once
Recently I have been using two different emails for git on the same computer (work and personal). My personal is the default, but on some repositories I want to use a different name and email address. Unfortunately, when I create a repo for work, sometimes I forget to change my email!
This can be found in a Stack Overflow answer somewhere, but I got tired of finding that every time, so here it is in a place I can always find it!
Note: this will rewrite the git history. Only do this if a) you have not pushed your commits yet, or b) you are the only one working on the project.
Step zero here is to reset your git name/email. Run git config --local user.name "My User name"
and the same with user.email
to set this only for the repo you are currently in.
git rebase --root -x "git commit --amend --no-edit --reset-author --date=\"$(git log -n 1 --format=%aD)\""
--root
runs the command on every commit, including the root (first) commit-
-x
defines the command we want to run after every commit.--reset-author
changes the author user details to the ones you have got set up for your repo.--date=...
sets the date for the commit. We don't want to change this, so we need specify the date of the existing commit. If we don't, the date will be updated to the date/time you run the command. Might not be a big deal, but still, we can avoid it.$(git log -n 1 --format=%aD)
runs thegit log
command, only getting the last commit and formatting it to display the date only. This is run in a subcommand so the result of this command is used in the--date=...
value.
If you don't want to do it for all commits, you can remove --root
and instead specify a specific commit to go back to, or use the HEAD~X
notation.