296

I have made a series of commits in Git and I realise now that I forgot to set my user name and user email properties correctly (new machine). I have not yet pushed these commits to my repository, so how can I correct these commits before I do so (only the 3 latest commits on the master branch)?

I have been looking at git reset and git commit -C <id> --reset-author, but I don't think I'm on the right track.

2
  • 2
    Another reason you might want to change the email property is this github error: remote: error: GH007: Your push would publish a private email address. ... ` ! [remote rejected] master -> master (push declined due to email privacy restrictions)`.
    – craq
    Sep 18, 2018 at 1:20
  • Also see stackoverflow.com/q/750172/1340631.
    – scai
    Apr 20, 2020 at 7:35

8 Answers 8

322

Warning: now deprecated in favor of filter-repo.

Rebase/amend seems inefficient, when you have the power of filter-branch at your fingertips:

git filter-branch --env-filter 'if [ "$GIT_AUTHOR_EMAIL" = "incorrect@email" ]; then
     GIT_AUTHOR_EMAIL=correct@email;
     GIT_AUTHOR_NAME="Correct Name";
     GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL;
     GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"; fi' -- --all

(split across lines for clarity, but not necessary)

Be sure to inspect the result when you're done, to make sure that you didn't change anything you didn't mean to!

15
  • 1
    mind explaining this a bit more? not sure what filter branch is Aug 23, 2014 at 19:34
  • 1
    @maxpleaner git filter-branch --help is pretty straightforward :) Jul 10, 2015 at 12:58
  • 3
    see also help.github.com/articles/changing-author-info, which also adds --tag-name-filter cat to the filter-branch in order to migrate tags to the new history. It also uses --branches --tags instead of --all, which only rewrites branch and tag history and leaves other refs alone (though that probably doesn't make much of a difference unless e.g. you're using git-notes) Feb 23, 2017 at 8:39
  • 20
    to perform this on just the last two commits, I replaced -- --all with HEAD~1..HEAD
    – nmz787
    Mar 30, 2018 at 3:11
  • 1
    @nmz787 How many logs are shown if you do git log HEAD~2..HEAD ?
    – Jona
    Mar 20, 2019 at 9:59
273

The interactive rebase approach is pretty nice when used in conjunction with exec. You can run any shell command against a specific commit or all commits in the rebase.

First set your git author settings

git config --global user.name "John Doe"
git config --global user.email johndoe@example.com

Then to reset the author for all commits after the given BASE_SHA:

git rebase -i BASE_SHA -x \
  "git commit --amend --author 'John Doe <johndoe@example.com>' -CHEAD"

This will pop up your editor to confirm the changes. All you need to do here is save and quit and it will go through each commit and run the command specified in the -x flag.

15
  • 3
    Thank you for introducing me to the -x option. Its pretty awesome! for the -i option I used HEAD~4 to fix my email address on my last 4 commits. worked like a charm.
    – Brad Hein
    Feb 9, 2015 at 19:18
  • 4
    This is much simpler than filter-branch if you just want to fix your last commits :). Note however, that this changes the timestamp of the commits.
    – luator
    Oct 27, 2015 at 8:44
  • 49
    To change the author but maintain the original timestamps, use git rebase -i YOUR_SHA -x "git commit --amend --author 'New Name <new_address@example.com>' -CHEAD"
    – Dave
    Apr 25, 2016 at 19:15
  • 2
    @Connor git log also showed old authorship for me, but git status correctly identified new commits and after force push they were as I intended.
    – Dan M.
    Aug 30, 2018 at 10:09
  • 19
    To rebase all commits including the root use: git rebase -i --root … instead of passing a SHA.
    – gfullam
    Apr 15, 2019 at 15:14
151

To change the author only for the last commit:

git commit --amend --author 'Author Name <author.name@mail.com>' --no-edit

Suppose you only want to change the author for the last N commits:

git rebase -i HEAD~4 -x "git commit --amend --author 'Author Name <author.name@mail.com>' --no-edit"

Change the committer as well:

as noted by @sshine and @Hermann.Gruber, the previous commands change the author only. To change the committer as well, you can use the solution proposed by @Hermann.Gruber:

git rebase -i HEAD~4 -x "GIT_COMMITTER_NAME='Author Name' GIT_COMMITTER_EMAIL='author.name@gmail.com' git commit --amend --author 'Author Name <author.name@gmail.com>' --no-edit"

NOTES

  • the --no-edit flag makes sure the git commit --amend doesn't ask an extra confirmation
  • when you use git rebase -i, you can manually select the commits where to change the author,

the file you edit will look like this:

pick 897fe9e simplify code a little
pick abb60f9 add new feature
exec git commit --amend --author 'Author Name <author.name@mail.com>' --no-edit
pick dc18f70 bugfix
6
  • 3
    for all commits from root. git rebase -i --root UPTO_COMMIT_SHA -x "git commit --amend --author 'NEW_CHANGE' --no-edit" Oct 30, 2017 at 17:16
  • 3
    I recommend to add --rebase-merges (short -r) option, to keep the topology of your branch intact if it contains some merges.
    – donquixote
    Apr 5, 2020 at 19:06
  • 11
    @ChrisMaes: Please note that git commit --amend --author ... does not change the committer, only the author! So while it may appear that your email was changed, there is, in fact, metadata in the repository that says who the old committer is. While the filter-branch (or filter-repo) methods are more crude, they actually change both. Proof: curl -s https://api.github.com/repos/sshine/author-committer/commits | jq '.[0].commit | { author, committer }' -- I did a git commit --amend --author="John Doe ..." here, and you can see that the committer is not John Doe.
    – sshine
    Feb 3, 2022 at 0:44
  • 1
    As a note, you can use --root in place of HEAD if trying to specify every commit of the rreo.
    – matttm
    Jun 28, 2022 at 0:37
  • 1
    @ChrisMaes @sshine to change also the committer information of the 4 latest commits, you can use git rebase -i HEAD~4 -x \ "GIT_COMMITTER_NAME='Author Name' GIT_COMMITTER_EMAIL='Author Name <author.name@gmail.com>' git commit --amend --author 'Author Name <author.name@gmail.com>' --no-edit" Mar 22 at 15:06
40

The highest voted answer here is now out of date. Git shows this scary warning when using git filter-branch -

WARNING: git-filter-branch has a glut of gotchas generating mangled history
         rewrites. Hit Ctrl-C before proceeding to abort, then use an
         alternative filtering tool such as 'git filter-repo'
         (https://github.com/newren/git-filter-repo/) instead.

filter-repo is not (yet) part of git and needs to be installed separately.

# Requires git v2.22+ and python v3.5+. Check with -
git --version && python3 --version
    
# Install using pip
pip install git-filter-repo    
    

To replace only the email in previous commits run the the command like this -

git filter-repo --email-callback '
    return email if email != b"incorrect@email" else b"correct@email"
' 

To replace both, email and author name in the previous commits run the the command like this -

git filter-repo --commit-callback '
    if commit.author_email == b"incorrect@email":
        commit.author_email = b"correct@email" 
        commit.author_name = b"Correct Name"
        commit.committer_email = b"correct@email" 
        commit.committer_name = b"Correct Name"
' 

Make sure the indents are there when you paste the command in your terminal. The callback uses python syntax so indents are important.

Read more about filter-repo callbacks in the docs.

2
  • 5
    Thank you. It worked! If you're getting zsh: command not found: pip on a Mac, try pip3 - pip3 install git-filter-repo.
    – Nelu
    Jan 28, 2022 at 10:23
  • on Windows (in ConEmu terminal), in a venv, I had to swap quotes and make it a one-liner, and add --force (because I made commits since last pushing): git filter-repo --force --email-callback " return email if email != b'wrong@email' else b'correct@email'"
    – nmz787
    Feb 4, 2022 at 2:07
13

This method was documented by GitHub for this very purpose (though GitHub has since removed it). The steps are:

  1. Open the terminal and make a bare clone of your repo
git clone --bare https://github.com/user/repo.git
cd repo
  1. Edit the following script (replacing OLD_EMAIL, CORRECT_EMAIL, and CORRECT_NAME)
#!/bin/sh

git filter-branch --env-filter '
OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"
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
  1. Copy/paste the script into your terminal and press enter to run it.
  2. Push your changes with git push --force --tags origin 'refs/heads/*' and you're done!
3
  • I followed the same instructions on GitHub that you referenced, and GitHub looks right now. However, I'm a Git newb and not sure how to sync my local repo back up after that. When I pull I get the same "refusing to merge unrelated histories" error mentioned in another answer. I think I need to rebase against that new commit history, but I'd very much appreciate more specific steps.
    – enigment
    Apr 20, 2019 at 23:59
  • @enigment if you are happy with the repo as it is on github, you can delete (or perhaps move to another location) the folder you have locally and simply clone from github
    – stevec
    Jun 8, 2019 at 3:42
  • Thanks, I know, but that doesn't seem like the idiomatic GitHub/Git way.
    – enigment
    Jun 9, 2019 at 14:53
2

I believe what you are looking for is git rebase --interactive

It allows you to go reset to an specific commit and then go throw the history changing adding or grouping commits

Here you have an explanation https://web.archive.org/web/20100213104931/http://blog.madism.org/index.php/2007/09/09/138-git-awsome-ness-git-rebase-interactive

2

If you're looking for a script, this one came handy for me.

  1. Download the script from GitHub and save it to an easily-accessible location.

  2. Change the permissions of the script file to allow it to execute:

    chmod +x changeauthor.sh

  3. Navigate into the repository with the incorrect commit history

    cd path/to/repo

  4. Run the script (with or without flags)

    ../path/to/changeauthor.sh --old-email kaka.ruto@example.com \
        --new-email ruto.kaka@example.com --new-name "Kaka Ruto" --remote origin
    

Be careful as this will rewrite all history in your current dir repository! Good thing is the script give you warnings and info about what you're about to do

Read more here https://www.adamdehaven.com/blog/update-commit-history-author-information-for-git-repository/

0
0

As suggested in this answer git-filter-repo is preferred for the task.

However to simply change author name and/or email one can use --mailmap or --use-mailmap instead of callbacks.

You need to create a mailmap file according to the format (see git-filter-repo/docs or git-scm.com/docs)

Then simply run

git filter-repo --mailmap .mailmap

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.