How to change author name and the email of the commit

Valentsea
Total
0
Shares

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 ” with the git commit command. For example,

git commit -m "New feature added" --author="John Doe "
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 " --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 rebase command, which can help you update almost everything in the past commits. 

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

Commits

We want to modify the author of the last two commits, so we will use the commit hash 574b2bcfad6f7d347bc9a08953f037a1d8b79967 with the rebase command.

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 " --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 
Date:   Sun Feb 26 23:46:13 2023 +0530

    New feature added

commit 6293a1uk38fa37e827cc0f4355m77lck65aav965
Author: John Doe 
Date:   Sun Feb 26 23:39:06 2023 +0530

    make login responsive

commit 574b2bcfad6f7d347bc9a08953f037a1d8b79967
Author: John Doe 
Date:   Sun Feb 26 11:13:40 2023 +0530

    make dashboard screen

commit a0a838167789d3d194315e679a69f5aed75d75bb
Author: John Doe 
Date:   Sun Feb 26 10:38:57 2023 +0530

    added border to images

commit 0a7634029ff6968a48781207acd61d74f45c35c6 (master)
Author: John Doe 
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.

Total
0
Shares
Valentsea

Deep dive into Angular pipes

The aim of this article is to explain everything related to pipes. First, we will learn how to…

You May Also Like