Categories
git git-filter-branch git-rewrite-history version-control

How do I change the author and committer name/email for multiple commits?

2771

How do I change the author for a range of commits?

10

  • 13

    Question: does using git filter-branch preserve the SHA1’s for previous tags, versions and objects? Or will changing the author name force change the associated SHA1’s as well?

    – AndyL

    Aug 3, 2010 at 14:13

  • 41

    Hashes will change yes

    Oct 14, 2010 at 15:16

  • 3

    Tangentially, I created a small script which finally fixed the root cause for me. gist.github.com/tripleee/16767aa4137706fd896c

    – tripleee

    May 30, 2014 at 8:51

  • 2

    @impinball The age of the question is hardly relevant. Creating a new duplicate question is out of the question. I suppose I could create a question which begs this particular answer but I’m not altogether convinced it would get all that much visibility. It’s not like there is a shortage of Git questions here… Glad I could help, anyway.

    – tripleee

    Sep 1, 2014 at 14:50


  • 2

    The github script that @TimurBernikovich mentioned is great and works for me. But that github url has changed: docs.github.com/en/enterprise/2.17/user/github/using-git/…

    Oct 13, 2020 at 4:14


1481

+50

This answer uses git-filter-branch, for which the docs now give this warning:

git filter-branch has a plethora of pitfalls that can produce non-obvious manglings of the intended history rewrite (and can leave you with little time to investigate such problems since it has such abysmal performance). These safety and performance issues cannot be backward compatibly fixed and as such, its use is not recommended. Please use an alternative history filtering tool such as git filter-repo. If you still need to use git filter-branch, please carefully read SAFETY (and PERFORMANCE) to learn about the land mines of filter-branch, and then vigilantly avoid as many of the hazards listed there as reasonably possible.

Changing the author (or committer) would require rewriting all of the history. If you’re okay with that and think it’s worth it then you should check out git filter-branch. The manual page includes several examples to get you started. Also note that you can use environment variables to change the name of the author, committer, dates, etc. — see the “Environment Variables” section of the git manual page.

Specifically, you can fix all the wrong author names and emails for all branches and tags with this command (source: GitHub help):

#!/bin/sh

git filter-branch --env-filter '
OLD_EMAIL="[email protected]"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="[email protected]"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

For using alternative history filtering tool git filter-repo, you can first install it and construct a git-mailmap according to the format of gitmailmap.

Proper Name <[email protected]> Commit Name <[email protected]>

And then run filter-repo with the created mailmap:

git filter-repo --mailmap git-mailmap

29

  • 44

    After executing the script you may remove the backup branch by executing “git update-ref -d refs/original/refs/heads/master”.

    – D.R.

    Aug 14, 2013 at 16:47

  • 12

    @rodowi, it duplicates all my commits.

    Jun 17, 2014 at 17:43

  • 12

    @RafaelBarros the author info (just like anything else in the history) is part of the commit’s sha key. Any change to the history is a rewrite leading to new id’s for all commits. So don’t rewrite on a shared repo or make sure all users are aware of it …

    – johannes

    Jun 11, 2015 at 13:52

  • 30

    Solved using git push --force --tags origin HEAD:master

    – mcont

    Nov 13, 2016 at 11:50

  • 21

    IMPORTANT!!! Before executing the script, set your user.name and user.email git config parameter properly! And after executing the script you’ll have some duplicate backup history called “original”! Delete it via git update-ref -d refs/original/refs/heads/master and then check if .git/refs/original folder structure is empty and then just remove it with rm -rf .git/refs/original. Lastly, you can verify the new rewritten log via: git log --pretty=format:"[%h] %cd - Committer: %cn (%ce), Author: %an (%ae)" ! One more thing: .git/logs has some log files that still have your old name!

    – gw0

    Feb 3, 2017 at 22:23


1481

+50

This answer uses git-filter-branch, for which the docs now give this warning:

git filter-branch has a plethora of pitfalls that can produce non-obvious manglings of the intended history rewrite (and can leave you with little time to investigate such problems since it has such abysmal performance). These safety and performance issues cannot be backward compatibly fixed and as such, its use is not recommended. Please use an alternative history filtering tool such as git filter-repo. If you still need to use git filter-branch, please carefully read SAFETY (and PERFORMANCE) to learn about the land mines of filter-branch, and then vigilantly avoid as many of the hazards listed there as reasonably possible.

Changing the author (or committer) would require rewriting all of the history. If you’re okay with that and think it’s worth it then you should check out git filter-branch. The manual page includes several examples to get you started. Also note that you can use environment variables to change the name of the author, committer, dates, etc. — see the “Environment Variables” section of the git manual page.

Specifically, you can fix all the wrong author names and emails for all branches and tags with this command (source: GitHub help):

#!/bin/sh

git filter-branch --env-filter '
OLD_EMAIL="[email protected]"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="[email protected]"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

For using alternative history filtering tool git filter-repo, you can first install it and construct a git-mailmap according to the format of gitmailmap.

Proper Name <[email protected]> Commit Name <[email protected]>

And then run filter-repo with the created mailmap:

git filter-repo --mailmap git-mailmap

29

  • 44

    After executing the script you may remove the backup branch by executing “git update-ref -d refs/original/refs/heads/master”.

    – D.R.

    Aug 14, 2013 at 16:47

  • 12

    @rodowi, it duplicates all my commits.

    Jun 17, 2014 at 17:43

  • 12

    @RafaelBarros the author info (just like anything else in the history) is part of the commit’s sha key. Any change to the history is a rewrite leading to new id’s for all commits. So don’t rewrite on a shared repo or make sure all users are aware of it …

    – johannes

    Jun 11, 2015 at 13:52

  • 30

    Solved using git push --force --tags origin HEAD:master

    – mcont

    Nov 13, 2016 at 11:50

  • 21

    IMPORTANT!!! Before executing the script, set your user.name and user.email git config parameter properly! And after executing the script you’ll have some duplicate backup history called “original”! Delete it via git update-ref -d refs/original/refs/heads/master and then check if .git/refs/original folder structure is empty and then just remove it with rm -rf .git/refs/original. Lastly, you can verify the new rewritten log via: git log --pretty=format:"[%h] %cd - Committer: %cn (%ce), Author: %an (%ae)" ! One more thing: .git/logs has some log files that still have your old name!

    – gw0

    Feb 3, 2017 at 22:23


706

One liner, but be careful if you have a multi-user repository – this will change all commits to have the same (new) author and committer.

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

With linebreaks in the string (which is possible in bash):

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

9

  • 7

    Why does it rewrite all commits if you specify HEAD in the end of the command?

    Jun 18, 2015 at 3:26

  • 3

    This does not work for my bitbucket repository, any idea ? I do a git push --force --tags origin 'refs/heads/*' after the advised command

    – Olorin

    Oct 5, 2016 at 21:46

  • 8

    The push command for this is : $git push --force --tags origin 'refs/heads/master'

    Jun 8, 2018 at 22:07


  • 4

    Neat; this keeps the old timestamps too.

    Mar 6, 2020 at 21:19

  • 2

    @HARSHNILESHPATHAK Note that for recently created repositories the branch master has been renamed main, so the command becomes $git push --force --tags origin 'refs/heads/main'

    – Alex Gisi

    Jan 20, 2021 at 22:30