Git How to Change Commit Author (with Real Examples)

To change the author of the most recent commit in Git, run:

git commit --amend --author="New Author Name <[email protected]>" --no-edit

To do this to any earlier commit in the commit history happens with the same command, but requires doing some checkouts, replacements, and rewrites.

This guide teaches you how to change the commit author of any commit in Git. You will learn how to change the most recent commit author, an arbitrary commit author, as well as any commits in the future. Besides, you will learn how to push the changes safely to the remote. All the theory is backed up with examples of real Git repositories.

Let’s jump into it!

Changing the Commit Author of the Most Recent Commit

A long git command to change the author of the most recent commit

To change the commit author of the most recent commit, you need to rewrite the commit history. This happens by using the git commit command with the –amend option to change the most recent commit and the –author option to change the author name.

This is what the command looks like:

git commit --amend --author="New Author Name <[email protected]>" --no-edit

Let’s inspect the individual parts of this command.

  • git commit –amend adds changes to the most recent commit instead of creating a new one.
  • The –author flag lets you specify the author of this commit.
  • The –no-edit option suppresses the interactive shell that would normally open when amending a commit.

So for example, to change the commit author to Alice Johnson ([email protected]), you’d do:

git commit --amend --author="Alice Johnson <[email protected]>" --no-edit

Pushing New Author Fails

Notice that changing the name of the commit author changes your local commit history. The hash (unique ID) of the most recent commit changes so even though the contents of the commit remain, the commit is technically now a different commit that it used to be.

This causes no problems if you hadn’t pushed the most recent commit to the remote yet.

But if the most recent commit already exists in the remote, the history of your local repo and remote contradict. The remote repo says your commit was made by author A whereas the local repo claims it was made by author B. To overcome this contradiction, you need to force-push the most recent commit to the remote repo.

A safe way to force-push a change to the remote is by using the –force-with-lease option when pushing. This prevents overwriting other collaborators’ commits (if any).

git push --force-with-lease

Example

That’s a lot of information to digest. Let me show you a concrete example to support your understanding.

Here’s the commit history of my example project. These commits exist both on my local repo as well as the remote.

Git log shows commit history

Now, let’s change the most recent commit author to alicebobson <[email protected]>:

 $ git commit --amend --author="alicebobson <[email protected]>" --no-edit

[main 0389f89] Add a third text file
 Author: alicebobson <[email protected]>
 Date: Tue Nov 29 11:34:27 2022 +0200
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file3.txt

Now, let’s take a look at the commit history once again.

Git log shows commit history with a new commit author

Now the most recent commit Author has changed!

Let’s push the changes to the remote as well.

$ git push
Git error of pushing some refs to origin

As mentioned earlier, this fails because the remote and the local repositories now have different commit histories. The local one claims the most recent commit was made by Alice Bobson whereas the remote claims it was made by Artturi Jalli.

To make the remote repository agree with the history of the local one, you need to force-push the changes to the remote origin.

 $ git push --force-with-lease

Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 253 bytes | 50.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/artturijalli/exampleProject.git
 + 2b82e0c...0389f89 main -> main (forced update)

Now the new author’s name is present in both the local repo and the remote one.

Now you understand how to change the most recent commit’s author. Next, let’s take a look at how you can rename any commit’s author.

Changing the Commit Author for Any Commit

Changing the author of any commit requires a bit more than just a single command.

To change the author of a commit with hash “ABC”:

  1. Checkout to the commit (git checkout ABC).
  2. Change the author (git commit –amend –author “New Author <email>”). This creates a new commit with the hash “DEF”.
  3. Check out the original branch (e.g. git checkout –).
  4. Replace the old commit with the new one (git replace ABC DEF).
  5. Rewrite the future commits (git filter-branch — –all).
  6. Remove the replacement commit (git replace -d ABC).
  7. Force-push the changes (git push –force-with-lease).

To truly understand what’s going on here, you need to see a concrete example.

Example

Let’s change the author of a commit in my example project.

Here’s the commit history:

git log that shows commit history

Let’s change the author of the second commit from the bottom, that is, the one with the message “Add the first text file”.

To change the author of that commit, let’s check out the commit by using the commit’s ID.

$ git checkout 1b77523bd8ee4aa8eea3f670efb8ff7ae59b6ca2

Running this command checks out the target commit and activates the “detached HEAD” state:

a git output that shows detached head state warning

Now, let’s change the author of the commit.

$ git commit --amend --author="alicebobson <[email protected]>" --no-edit
a successfull git message that shows author rename was successful

This action changes the commit hash (ID). Check this new hash with the git log command.

checking git log on a detached head state
0e570203b83b2a1287c9949857f9c7af651120ea is the new hash for the old commit with a new author

Store the new hash somewhere, you’ll need it in just a bit.

Then, let’s check out back to the most recent commit. In our case, we can do “git checkout –“. This checks back out to the previous branch, that is, the most recent commit.

$ git checkout -
the checkout message from modified detached head to the original branch

Now, let’s replace the old commit with the newly created one. This happens with the git replace command by using the commit hashes. In my case, the original commit whose author I’m changing was 1b77523bd8ee4aa8eea3f670efb8ff7ae59b6ca2. The commit with the new author was 0e570203b83b2a1287c9949857f9c7af651120ea.

$ git replace 1b77523bd8ee4aa8eea3f670efb8ff7ae59b6ca2 0e570203b83b2a1287c9949857f9c7af651120ea

This action replaces the old commit with the new commit where the author’s name is different. You can view the change in the git log:

a replacement commit in the commit history

If you now tried to push the changes, it wouldn’t do anything because the replacement commit still has the ID of the original one. In the eyes of Git, nothing has changed yet.

To be able to push the changes, you need to actually change the ID of the replaced commit (and the ones that come after it):

$ git filter-branch -- --all

This operation takes a couple of seconds. After it has been completed, you can check the commit history again:

the commit history of an overridden replacement commit

Now the commit with the new author is no longer with status (replaced). Instead, the commit (and all the preceding commits) now has a new hash.

At this point, let’s delete the reference to the original commit 1b77523bd8ee4aa8eea3f670efb8ff7ae59b6ca2.

$ git replace -d 1b77523bd8ee4aa8eea3f670efb8ff7ae59b6ca2

Finally, let’s push the changes to the remote origin:

$ git push --force-with-lease

If this command fails, you might need to do a force-push with git push -f. This action is dangerous as it can overwrite other contributors’ commits! So only run it if you’re sure no one’s work is going to be deleted.

Thanks for reading. Happy coding!

Read Also

How to Change Most Recent Git Commit

About the Author

Artturi Jalli
I'm an entrepreneur and a blogger from Finland. My goal is to make coding and tech easier for you with comprehensive guides and reviews.