How to change the git commit author?

Becoming a Git master could be complicated, due that there are several options that Git provides us and some of them need to be used based on the rules that the team defines around the process of the project that we are working on right now.

Everyone at some moment has needed help with Git, so the main idea is to try to recompile that tips or common problems that you can have with Git.

In this post, we will see what you can do in the case that an error committed with a wrong git user, email, or something like that.

Change last commit author

This is the easiest scenery that you can a front, basically, we just need to run the next command in the terminal.

git commit --amend --author="<name> <email>" --no-edit

The commit was pushed remotely?

In the case that the commit was pushed remotely we need to run an extra commit.

git push -f origin <branch-name>

After that, you can do just a git log command and check in the history that the change was applied.

Change more than a commit

For these cases, the best option could be to apply a rebase of the branch that you are working on right now. First of all please make sure that you are in the branch that you want to change.

For example, we have a branch called feature/test and the last two commits were pushed with the wrong email. So we need to change the author information for the commits:

  • 2c6eececec35ece0dc52c683de2dbc34553b283b
  • 1c6eececec35ece0dc52c683de2dbc34553b283b
commit 2c6eececec35ece0dc52c683de2dbc34553b283b (HEAD -> feature/test, origin/feature/test)
Author: Brayan Arrieta <wrong-email>
Date:   Tue Jun 22 16:06:10 2021 -0600

    Feat: change some stuff

commit 1c6eececec35ece0dc52c683de2dbc34553b283b (HEAD -> feature/test, origin/feature/test)
Author: Brayan Arrieta <wrong-email>
Date:   Tue Jun 22 16:04:10 2021 -0600

    Feat: change server

commit 4d133f673250ed628e58371a8b246d68171fbbf9 (origin/master, origin/HEAD, master)
Merge: 144c453125 cb1ac7e774
Author: Other User <[email protected]>
Date:   Tue Jun 22 13:40:43 2021 -0500

    Merge branch 'feature/test2' into 'master'

To fix that first of all we need to do the rebase of the branch with the next command

git rebase -i -p <previous-commit-id>

In the param previous-commit-id we need to pass the previous commit id before the commits that we want to fix, for example in this case the command will be git rebase -i -p 4d133f673250ed628e58371a8b246d68171fbbf9.

After that will see something similar to

pick 2c6eece Feat: change some stuff
pick 1c6eece Feat: change server

# Rebase 4d133f6..2c6eece onto 4d133f6 (4 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <commit> = run command (the rest of the line) using shell
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
".git/rebase-merge/git-rebase-todo" 28L, 1267C

After that just make sure that all the commit that has the wrong author have the p or pick in the commit list. In the case that needs to change something just type with your keyboard i to enter in insert mode, apply the change, and ESC. After that, we just need to type :wq on our keyboard to save that.

Commands

  • i: Enter to insert mode
  • ESC: Exit of the insert mode
  • :wq: Write and quit
  • :q: Quit

After that Git will be processing the rebase and in the commit that you put the pick will be displayed a message something like

Stopped at 1c6eece... Feat: change server
You can amend the commit now, with

    git commit --amend

Once you are satisfied with your changes, run

    git rebase --continue

When that appears that message mention we have some options to change the author or just continue with the command git rebase --continue. In the case that we want to change the author need to execute the next commit.

git commit --amend --author="Brayan Arrieta <correct-email>" --no-edit

After that just continue to the next commit with

git rebase --continue

Something important to mention is that the previous steps mentioned need to be applied to every commit with the pick option. At you will see a message something like

Successfully rebased and updated refs/heads/feature/test.

After that just need to execute a push command

git push origin <branch-name>

The commits were pushed remotely?

In the case that the commits were pushed remotely we need to run instead of the git push origin <branch-name> command the next command, which will change the Git history of our branch with the fix.

git push -f origin <branch-name>

Conclusion

As you see we can change the commit author with some simple commands so go ahead in the case that afront that issue. I hope you find this article really helpful. I will be updating this post based on your comments and recommendations so let me know in any case. Thanks! šŸ‘

27