How To Change The Git Commit Author

Git tracks the changes made to files in a repository by storing commits, which are basically snapshots of the repository at a particular point in time. Each commit is associated with an author, the person who made the changes to the commit. There may be situations in which you want to change the author of a commit, such as if you accidentally committed under the wrong user name or if you want to attribute the commit to someone else. Fortunately, it's relatively easy to change the author of a commit in Git.

There are several ways to change the author of a commit, depending on whether you want to change the author for a single commit, or for multiple commits.

Changing the Author for Last Commit

To change the author of the last commit, you can use the git commit --amend command and specify the --author flag. For example,

git commit --amend --author="John Doe <[email protected]>"

All you need to do is replace "John Doe" with your name, and "[email protected]" with your email address. This will change the author of the most recent commit to the specified name and email address.

Changing the Author for the Next Commit

You can also overwrite the author information for the next commit by removing the --amend flag,

git commit --author="John Doe <[email protected]>"

Changing the Author for Multiple Commits

If you want to change the author for multiple commits, you can use the git filter-branch command. This command allows you to rewrite the commit history of a branch by applying filters to each commit.

git filter-branch --commit-filter '
    if [ "$GIT_COMMITTER_NAME" = "Mark Moe" ];
    then
        GIT_COMMITTER_NAME="John Doe";
        GIT_AUTHOR_NAME="John Doe";
        GIT_COMMITTER_EMAIL="[email protected]";
        GIT_AUTHOR_EMAIL="[email protected]";
        git commit-tree "$@";
    else
        git commit-tree "$@";
    fi' HEAD

Replace Mark Moe with the name of the current author, and John Doe with the name you want to use as the author. Replace [email protected] with the email address you want to use as the author. This will change the author for all commits in the current branch to the specified name and email address.

I hope you found this post useful. In case you've any queries or feedback, feel free to drop a comment.