125

I would like to change my name, surname and email in my all commits, is it possible?

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Joshua
  • 1,253
  • 2
  • 9
  • 4
  • 2
    Is it a reprository for only you, for a few people or for a big project? – thejh Dec 20 '10 at 21:06
  • 3
    possible duplicate of [How do I change the author of a commit in git?](http://stackoverflow.com/questions/750172/how-do-i-change-the-author-of-a-commit-in-git) – Josh Lee Dec 20 '10 at 21:32
  • possible duplicate of [How do I rewrite committer names in a git repository?](http://stackoverflow.com/questions/1566809/how-do-i-rewrite-committer-names-in-a-git-repository) – Josh Lee Feb 28 '11 at 21:10
  • Possible duplicate of [Change the author and committer name and e-mail of multiple commits in Git](https://stackoverflow.com/questions/750172/change-the-author-and-committer-name-and-e-mail-of-multiple-commits-in-git) – Stevoisiak Nov 08 '17 at 17:48

6 Answers6

215

Use git-filter-branch.

git filter-branch --commit-filter 'if [ "$GIT_AUTHOR_NAME" = "Josh Lee" ];
  then export GIT_AUTHOR_NAME="Hobo Bob"; export GIT_AUTHOR_EMAIL=hobo@example.com;
  fi; git commit-tree "$@"'

This only affects the author, not the committer (which for most commits will be the same as the author). If you want to rewrite those as well, set the GIT_COMMITTER_NAME and GIT_COMMITTER_EMAIL variables.

The standard warning about rewriting history applies; only do it to history that has not yet been shared.

June 2018 Update

The manual now includes a solution, using --env-filter, in its examples: https://git-scm.com/docs/git-filter-branch#_examples :

git filter-branch --env-filter '
    if test "$GIT_AUTHOR_EMAIL" = "root@localhost"
    then
        GIT_AUTHOR_EMAIL=john@example.com
    fi
    if test "$GIT_COMMITTER_EMAIL" = "root@localhost"
    then
        GIT_COMMITTER_EMAIL=john@example.com
    fi
' -- --all
Jeff Clemens
  • 912
  • 9
  • 17
Josh Lee
  • 171,072
  • 38
  • 269
  • 275
  • 4
    If you’re using `msysgit`, you still have access to `bash`. Otherwise, I’ve no clue. – Josh Lee Dec 20 '10 at 21:59
  • @Joshua if you're using something where you don't have bash, you could probably use windows batch scripting, though I haven't tried it. – Tyler Dec 22 '10 at 05:41
  • and what about tags? this solution won't change author of tags – piotrek Jan 05 '15 at 22:37
  • @Joshua check out the git repo on a linux box and perform the fix there – Will Sheppard May 11 '15 at 08:47
  • Is there an options which result in new branch and leave source commits intact? – Eugen Konkov Mar 30 '18 at 16:20
  • +1. Good solution. Would be even nicer if you put the old and new emails in two env var at the beginning of the line, something like: `OLD_EMAIL=local@localhost && NEW_EMAIL=john@example.com && git filter-branch --env-filter " if test \"\$GIT_AUTHOR_EMAIL\" = \"$OLD_EMAIL\" then GIT_AUTHOR_EMAIL=$NEW_EMAIL fi if test \"\$GIT_COMMITTER_EMAIL\" = \"$OLD_EMAIL\" then GIT_COMMITTER_EMAIL=$NEW_EMAIL fi " -- --all` then this answer would be workable from command-line – xiay Jul 22 '18 at 23:18
  • @EugenKonkov Source commits are intact because you create a branch/tree from the first commit you modify. If you want a branch to point to old commits, just first create a new branch before rewriting history. – xiay Jul 22 '18 at 23:22
  • Wonderful, but don't forget `--tag-name-filter cat` to rewrite tags; see https://stackoverflow.com/a/7673115/421049 . Unfortunately the tagger emails don't seem to be updated; see https://stackoverflow.com/q/51790056/421049 . – Garret Wilson Aug 10 '18 at 16:17
  • @xiay Is `root@localhost` supposed to stay the same? – David G May 27 '22 at 00:09
57

To rewrite both author and commiter in all selected commits:

git filter-branch --commit-filter \
'if [ "$GIT_AUTHOR_NAME" = "OldAuthor Name" ]; then \
export GIT_AUTHOR_NAME="Author Name";\
export GIT_AUTHOR_EMAIL=authorEmail@example.com;\
export GIT_COMMITTER_NAME="Commmiter Name";\
export GIT_COMMITTER_EMAIL=commiterEmail@example.com;\
fi;\
git commit-tree "$@"'
user11153
  • 8,536
  • 5
  • 47
  • 50
40

If there are no other authors, you can do:

git filter-branch --commit-filter 'export GIT_AUTHOR_NAME="authorname"; \
export GIT_AUTHOR_EMAIL=mail@example.com; git commit-tree "$@"'
123 456 789 0
  • 10,565
  • 4
  • 43
  • 72
denis.peplin
  • 9,585
  • 3
  • 48
  • 55
13

Save the script below as e.g. ~/.bin/git-replace-author and run it using, e.g:

git replace-author "John Ssmith" "John Smith" "johnsmith@example.com"

With no arguments, it updates all commits with your name to use your current email address according to Git config.

DEFAULT_NAME="$(git config user.name)"
DEFAULT_EMAIL="$(git config user.email)"
export OLD_NAME="${1:-$DEFAULT_NAME}"
export NEW_NAME="${2:-$DEFAULT_NAME}"
export NEW_EMAIL="${3:-$DEFAULT_EMAIL}"

echo "Old:" $OLD_NAME "<*>"
echo "New:" "$NEW_NAME <$NEW_EMAIL>"
echo "To undo, use: git reset $(git rev-parse HEAD)"

git filter-branch --env-filter \
'if [ "$GIT_AUTHOR_NAME" = "${OLD_NAME}" ]; then
    export GIT_AUTHOR_NAME="${NEW_NAME}"
    export GIT_AUTHOR_EMAIL="${NEW_EMAIL}"
    export GIT_COMMITTER_NAME="${NEW_NAME}"
    export GIT_COMMITTER_EMAIL="${NEW_EMAIL}"
fi'

Raw (to download)

Zaz
  • 46,476
  • 14
  • 84
  • 101
3

Only if you haven't pushed your commits to the world. Other wise everyone else has your old name in their repo which is unlikely you can change everyone's.

EnabrenTane
  • 7,428
  • 2
  • 26
  • 44
  • True, but in some cases you have no choice. I my case I had a wrong email address configured in my git config (as I could see with "git config --global -l"). As a result, I had no commit activity showing up in my own Github repo (because the email address didn't match with the email configured in Github) ! To solve this, I fixed my local commits using the recipe from http://stackoverflow.com/a/23564785/2474068 (worked perfect) and then I pushed the changed commits to Github using "git push -u -f origin master" (with the force flag "-f"). That goes against accepted practice but I had no choice! – leo Nov 10 '16 at 11:10
  • 1
    Yes, my point was that any forks of that repo wouldn't have that change unless they accepted your force push. It would be challenging to get every fork to update :) – EnabrenTane Nov 10 '16 at 22:01
2

With Git 2.24 (Q4 2019), git filter-branch (and BFG) is deprecated.

The equivalent would be, using newren/git-filter-repo, and its example section:

cd repo
git filter-repo --mailmap my-mailmap

with my-mailmap:

Correct Name <correct@email.com> <old@email.com>

That would replace the author name and email of any commit done by anyone with <old@email.com>

See git shortlog mapping author section for the exact syntax of

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250