The Problem

If you’re here, you probably have multiple Git accounts authenticated on your PC. This can happen for several reasons. You could be using a personal account, perhaps Github, plus a company account on Gitlab - a standard setup.

With this setup, it is a common mistake to accidentally commit changes in Git with the wrong user information, leading to some permission issues. Fortunately, Git provides a way to amend the commit author information easily.

Amending the Last Commit

If you’re not knee-deep in the mud and only need to change the user information of the most recent commit, you can use the following command:

TLDR:

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

Replace CorrectUsername and [email protected] with the correct username and email address you want to use.

Walkthrough

First, we’d check the details of the last commit:

git log -1

The git log command shows you the details of your commits, while the -1 argument limits the output to only the last (most recent) commit.

Sample result:

commit 03f08dafcd886c9 (HEAD -> CHORE-add-lint, origin/CHORE-add-lint)
Author: MartinsVictor <[email protected]>
Date:   Mon Apr 14 16:34:42 2025 +0100

   CHORE: add linting

From the result, the Author line shows the user information for the last commit, in the order Username < EmailAddress >. In my case, I want to change these values to MartinsOnuoha <[email protected]>.

To do this, I run the git commit command with the correct values:

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

You can verify the change by rechecking the log:

git log -1

Sample result:

commit 03f08dafcd886c9 (HEAD -> CHORE-add-lint, origin/CHORE-add-lint)
Author: MartinsOnuoha <[email protected]>
Date:   Mon Apr 14 16:34:42 2025 +0100

    CHORE: add linting

The last commit should now have the correct author information. That’s all good, but how about older commits?

Changing Author Information for Older Commits

What if the incorrect information is buried in an older commit? You can use Git’s interactive rebase as a solution. Here’s how to do it:

First, start an interactive rebase for the last N commits (N is the number of commits you want to go back):

git rebase -i HEAD~N

Sample result:

pick 8e4c4af CHORE: improve code block
pick 8533371 CHORE: improve pre block
pick 48c9f8a CHORE: update featured articles
pick c3dd6b8 CHORE: replace featured articles

# Rebase 869a296..c3dd6b8 onto 869a296 (4 commands)
#
# Commands:
# p, pick <commit> = use commit

In the Vim editor that opens, find the commit you want to change and replace pick with edit next to that commit. In my case, I’d like to change the commit with the hash: 8533371.

Sample result:

pick 8e4c4af CHORE: improve code block
edit 8533371 CHORE: improve pre block
pick 48c9f8a CHORE: update featured articles
pick c3dd6b8 CHORE: replace featured articles
...

Save and close the editor with :wq. Git will know to pause the rebase process at the commit you marked for editing.

Now you can amend the commit with the correct author information:

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

Finally, continue and complete the rebase process:

git rebase --continue

To be sure, you can verify the changes by rechecking the log:

git log

In summary, this is relatively straightforward, but a problem we encounter nonetheless. If you’d like to explore more about git commits, you can consider reading Git’s documentation on amending commits.