DEV Community

Ankur Sheel
Ankur Sheel

Posted on • Originally published at ankursheel.com on

How to change the author of multiple Git commits

Problem

I have two accounts from which I access my GitHub repository. I have set one of them as a global setting. On a per-repository basis, I override the default details with my other account. Sometimes, I forget to override the default values and realize it only after making a few commits.

This post will show how we can update the author after making a few commits with incorrect details.

Solution

First, we need to update our gitconfig with the author details.

[user]
name = author_name
email = author_email
Enter fullscreen mode Exit fullscreen mode

We can run the following command.

git rebase -i <commit_hash> -x "git commit --amend --reset-author -CHEAD"
Enter fullscreen mode Exit fullscreen mode
  • git rebase -i : Runs git rebase in interactive mode, allowing altering individual commits in the process.
  • : The hash of the commit after which we want to update the author.
  • -x : Append the shell command for each line creating a commit.
  • git commit —amend : Modify the most recent commit.
  • —reset-author : Resets the author to the settings in the .gitconfig.
  • -CHEAD : -C takes the existing commit object and reuses the log message without allowing the user to edit it. HEAD refers to the current commit we are viewing. -CHEAD takes the message from the current commit without opening the editor.

We will then be presented with an editor to confirm all the commits we want.

We can run the following command to update the author for all commits, including the root.

git rebase -i --root -x "git commit --amend --reset-author -CHEAD"
Enter fullscreen mode Exit fullscreen mode

Conclusion

This will update the author details for all the specified commits.

References

Top comments (0)