How to change author of the git commit

rajhawaldar

Raj Hawaldar

Posted on February 28, 2023

How to change author of the git commit

First of all, we need to understand how the author name and email are set by the git version control.

1. Set the author name and email globally

With the help of the following commands, we can set the committer's name and email globally.



$ git config --global user.name "John Doe"
$ git config --global user.email "john@doe.org"


Enter fullscreen mode Exit fullscreen mode

2. Set the author name and email for each repository

We can set these details at repository level by omitting the --global flag.



git config user.name "John Doe"
git config user.email "john@doe.org"



Enter fullscreen mode Exit fullscreen mode

Change the author while committing the code

We can use the flag --author="Name <email>" with the git commit command. For example,



git commit -m "New feature added" --author="John Doe <john@doe.org>"


Enter fullscreen mode Exit fullscreen mode

Change author of the last commit

It's relatively easy to change the author of the last commit. Simply run the following command in the terminal, and you're done!



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


Enter fullscreen mode Exit fullscreen mode

Don't forget to include email address in "<>"

Change the author of the past commits (not the latest)

This will be a little more challenging. Keep in mind that when we update the past commits , we actually rewrite the history of the git branch. We are going to use the git rebase command, which can help us update almost everything in the past commits. 

First of all, we will identify the last "valid commit".

Commits

We want to update the author of the last two commits. We must specify how far back we want to rewrite commits by telling the command which commit to rebase onto.

Note: Don’t include any commit which is already pushed to a remote repository. By supplying an alternative version of the same change, we will mislead other developers.

In our case we want to update last two commits so we will pass "HEAD~2" as commit hash.



git rebase -i HEAD~2
# We can also use the parrent id of the second commit (hash of the 3rd commit)
git rebase -i 574b2bcfad6f7d347bc9a08953f037a1d8b79967


Enter fullscreen mode Exit fullscreen mode

Git will now open the default editor with the similar details as shown below.

rebase prompt

Now we will mark the commits we want to modify by replacing the word "pick" with "edit".

mark commits

Git will walk us through each commit, and we can update the author details.



Stopped at 3800a1cf1... make login responsive
You can amend the commit now, with

    git commit --amend

Once you are satisfied with your changes, run

    git rebase --continue


Enter fullscreen mode Exit fullscreen mode

Now use below commands to update the author and continue rebase till the last commit



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

git rebase --continue


Enter fullscreen mode Exit fullscreen mode

Once done, if we again run the git log command, we will get the below output.



commit 5e679a69f5aed75d75a0a838167789d3d19431e5 (HEAD -> feature1)
Author: John Doe <john.doe@gmail.com>
Date:   Sun Feb 26 23:46:13 2023 +0530

    New feature added

commit 6293a1uk38fa37e827cc0f4355m77lck65aav965
Author: John Doe <john.doe@gmail.com>
Date:   Sun Feb 26 23:39:06 2023 +0530

    make login responsive

commit 574b2bcfad6f7d347bc9a08953f037a1d8b79967
Author: John Doe <john.doe@gmail.com>
Date:   Sun Feb 26 11:13:40 2023 +0530

    make dashboard screen

commit a0a838167789d3d194315e679a69f5aed75d75bb
Author: John Doe <john.doe@gmail.com>
Date:   Sun Feb 26 10:38:57 2023 +0530

    added border to images

commit 0a7634029ff6968a48781207acd61d74f45c35c6 (master)
Author: John Doe <john.doe@gmail.com>
Date:   Sun Feb 26 10:26:45 2023 +0530

    First commit


Enter fullscreen mode Exit fullscreen mode

The important point is that the commit hashes of the last two commits have changed. If the branch is present remotely, we must force push it in order to push these changes to the git remote repository.

Using the git filter-branch

If we want to update the commits in bulk for the entire branch, we can use the following script:



#!/bin/bash

export FILTER_BRANCH_SQUELCH_WARNING=1 # This will suppress the warning shown by git

git filter-branch -f --env-filter '
    if test "$GIT_AUTHOR_EMAIL" = "richard.roe@gmail.com"
    then
        GIT_AUTHOR_NAME="John Doe"
        GIT_AUTHOR_EMAIL=john.doe@gmail.com
    fi
    if test "$GIT_COMMITTER_EMAIL" = "richard.roe@gmail.com"
    then
        GIT_COMMITTER_NAME="John Doe"
        GIT_COMMITTER_EMAIL=john.doe@gmail.com
    fi
' HEAD


Enter fullscreen mode Exit fullscreen mode

The script mentioned above will change the author of every commit that meets the condition.

What is the difference between a committer and an author?

The "author" is the person who originally wrote the code, and the "committer" is the person who committed the code on behalf of the original author. For instance, if a project maintainer receives a patch and applies it, the author of the patch still receives credit for the work.

The git-filter-branch is such a powerful command that it can easily corrupt repositories or leave you with a mess worse than you began with. Avoid using it with the public or shared repositories.

Thanks for reading, I hope this is helpful.

💖 💪 🙅 🚩
rajhawaldar
Raj Hawaldar

Posted on February 28, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related