Skip to content

Modify the Author Information of Multiple Git Commits

I have had instances where I have mixed up my git accounts in the local git repositories I am working on. I would often make the mistake of using a different account on a repository, work on updates and commit changes locally then only to find out that I have been using a different account.

One possible command to modify the author information of multiple commits is the git filter-branch command. git filter-branch allows rewriting the revision history of an entire branch.

Warning

Using git filter-branch changes the target commit's hash. This means that you may not be able to easily push the rewritten branch on top of the original branch.

See the following links:

Modifying All Commits in the Current Branch

To change the author information of all the commits in the current branch, you can invoke the following command:

git \
    filter-branch \
    -f \
    --env-filter \
    "
    GIT_AUTHOR_NAME='yourusername';
    GIT_AUTHOR_EMAIL='[email protected]';
    GIT_COMMITTER_NAME='yourusername';
    GIT_COMMITTER_EMAIL='[email protected]';
    " \
    HEAD

This will rewrite the revision history of the current branch from the beginning up to the last commit with the specified name and email.

Note

HEAD in this context can be viewed as a pointer referring to the current branch.

Specifying Target Commits

You may wish to update the author information only of select commits. To do this, you can specify a specify a range of commits instead of only HEAD.

  1. Check your commit history and identify the target commits

    git log --oneline
    
    93e523c Add nodejs tools
    e32d66e Update aliases
    4230d52 Use CTRL+Space to switch TMUX panes
    53001af Add Python virtualenv VIM plugin
    c54a311 Reduce number of grep commands in getting download url
    5680c6d Add python installation script through pyenv
    17c0559 Add separate install script for compiled binaries
    8a2060d Update packages to install in apt
    

    For example, you would like to update the commits from hash c54a311 up to the latest commit.

    93e523c Add nodejs tools
    e32d66e Update aliases
    4230d52 Use CTRL+Space to switch TMUX panes
    53001af Add Python virtualenv VIM plugin
    c54a311 Reduce number of grep commands in getting download url
    5680c6d Add python installation script through pyenv
    17c0559 Add separate install script for compiled binaries
    8a2060d Update packages to install in apt
    
  2. Invoke the git filter-branch command

    git \
        filter-branch \
        -f \
        --env-filter \
        "
        GIT_AUTHOR_NAME='yourusername';
        GIT_AUTHOR_EMAIL='[email protected]';
        GIT_COMMITTER_NAME='yourusername';
        GIT_COMMITTER_EMAIL='[email protected]';
        " \
        c54a311~..HEAD
    

    Note

    • The ~ in the previous command after c54a311 is important. c54a311~ refers to the commit before c54a311. Git starts processing commits after the first provided commit hash in the range. Specifying c54a311..HEAD will only include commit 53001af onwards.

    • HEAD in this context refers to the last commit in the current branch.

    You can also use the HEAD~<number> notation to specify the number of commits from the last commit.

    The following command modifies the last 10 commits:

    git \
        filter-branch \
        -f \
        --env-filter \
        "
        GIT_AUTHOR_NAME='yourusername';
        GIT_AUTHOR_EMAIL='[email protected]';
        GIT_COMMITTER_NAME='yourusername';
        GIT_COMMITTER_EMAIL='[email protected]';
        " \
        HEAD~10..HEAD
    

    You can also omit the last HEAD instance in the range.

    git \
        filter-branch \
        -f \
        --env-filter \
        "
        GIT_AUTHOR_NAME='yourusername';
        GIT_AUTHOR_EMAIL='[email protected]';
        GIT_COMMITTER_NAME='yourusername';
        GIT_COMMITTER_EMAIL='[email protected]';
        " \
        HEAD~10..
    

Alternative: Git's mailmap

One alternative to using the filter-branch command is Git's mailmap feature. You can create a .mailmap file in your repository to specify the name and email address mapping from your commits.

However, one downside of this approach is that you might need to keep the .mailmap file tracked in your repository.

See https://git-scm.com/docs/git-check-mailmap.