Categories
git git-commit

How to change the commit author for a single commit?

2706

I want to change the author of one specific commit in the history. It’s not the latest commit.

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

2

4525

Interactive rebase off of a point earlier in the history than the commit you need to modify (git rebase -i <earliercommit>). In the list of commits being rebased, change the text from pick to edit next to the hash of the one you want to modify. Then when git prompts you to change the commit, use this:

git commit --amend --author="Author Name <[email protected]>" --no-edit

For example, if your commit history is A-B-C-D-E-F with F as HEAD, and you want to change the author of C and D, then you would…

  1. Specify git rebase -i B (here is an example of what you will see after executing the git rebase -i B command)
    • if you need to edit A, use git rebase -i --root
  2. Change the lines for both C and D from pick to edit
  3. Exit the editor (for vim, this would be pressing Esc and then typing :wq).
  4. Once the rebase started, it would first pause at C
  5. You would git commit --amend --author="Author Name <[email protected]>"
  6. Then git rebase --continue
  7. It would pause again at D
  8. Then you would git commit --amend --author="Author Name <[email protected]>" again
  9. git rebase --continue
  10. The rebase would complete.
  11. Use git push -f to update your origin with the updated commits.

41

  • 54

    If you don’t know what editor you’re in, the answer is likely vim. To save and quit, type Esc : w q Enter. On the other hand, if it’s Nano and you see things like “WriteOut: ^O” along the bottom, then you should use Ctrl+O, Enter, Ctrl+X instead.

    – Amber

    Jun 12, 2012 at 17:02


  • 41

    what if you want to modify the very first commit? What is the previous commit hash then?

    – Brenden

    Aug 31, 2012 at 16:52

  • 19

    note that you can set the commit author to the current user with git commit --amend --reset-author

    – njzk2

    Jan 14, 2014 at 16:35

  • 265

    Use --no-edit option. git commit --amend --reset-author --no-edit won’t open an editor. Available since git 1.7.9.

    – 5lava

    Jun 9, 2014 at 18:43

  • 73

    @Brenden to modify the very first commit in the project, use git rebase -i --root

    May 4, 2016 at 20:07


655

The accepted answer to this question is a wonderfully clever use of interactive rebase, but it unfortunately exhibits conflicts if the commit we are trying to change the author of used to be on a branch which was subsequently merged in. More generally, it does not work when handling messy histories.

Since I am apprehensive about running scripts which depend on setting and unsetting environment variables to rewrite git history, I am writing a new answer based on this post which is similar to this answer but is more complete.

The following is tested and working, unlike the linked answer.
Assume for clarity of exposition that 03f482d6 is the commit whose author we are trying to replace, and 42627abe is the commit with the new author.

  1. Checkout the commit we are trying to modify.

     git checkout 03f482d6
    
  2. Make the author change.

     git commit --amend --author "New Author Name <New Author Email>"
    

Now we have a new commit with hash assumed to be 42627abe.

  1. Checkout the original branch.

  2. Replace the old commit with the new one locally.

     git replace 03f482d6 42627abe
    
  3. Rewrite all future commits based on the replacement.

     git filter-branch -- --all
    
  4. Remove the replacement for cleanliness.

     git replace -d 03f482d6
    
  5. Push the new history (only use –force if the below fails, and only after sanity checking with git log and/or git diff).

     git push --force-with-lease
    

Instead of 4-5 you can just rebase onto new commit:

git rebase -i 42627abe

31

  • 9

    Please put a note in there to re-checkout your original branch after step 2.

    Mar 11, 2015 at 0:26

  • 62

    This looks much clear alternative to the horrific git rebase -i. Never heard of this git replace thing before. +1

    Jul 8, 2015 at 23:29

  • 3

    For clean the refs/original/… backup see here

    – alexis

    Mar 9, 2016 at 17:54


  • 9

    I recommend using --force-with-lease instead of -f. It’s safer.

    Jul 3, 2017 at 17:14

  • 26

    WARNING: be aware that git filter-branch -- --all is changing commits in all branches that the original commit was in. If you don’t have enough credentials (or you just don’t want to change other’s branches history), it’s good to be careful with this answer.

    – ribamar

    Jan 11, 2018 at 10:45


332

Github documentation contains a script that replaces the committer info for all commits in a branch (now irretrievable, this is the last snapshot).

  • Run the following script from terminal after changing the variable values

    #!/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
    
  • Push the corrected history to GitHub:

    git push --force --tags origin 'refs/heads/*'
    

OR if you like to push selected references of the branches then use

```
git push --force --tags origin 'refs/heads/develop'
```

13

  • 19

    This changes it in all commits, not just one. Funny thing, I did this less than 30 minutes ago.

    – Artjom B.

    Jun 9, 2015 at 16:20


  • When I found this answer after reading previous ones I thought it’s worth giving a try and voila it did the job. However in my case it changed committer name only in initial commit. By the way, before I tried ideas from the first answer. Maybe it affected the system somehow.

    Aug 8, 2016 at 19:31

  • 2

    Note that if you avoid using clone / push, you’ll end up with a backup namespace refs/original/. I couldn’t find a way to remove this namespace intelligently, so I ended up deleting directory .git/refs/original ,which worked.

    Jan 18, 2018 at 17:33


  • 1

    Why does this result in changes to the repo if e.g. OLD_EMAIL does not match anything? For some reason a few (but not all!) commit hashes change.

    – mjs

    Aug 10, 2018 at 14:47

  • 1

    My use case for this answer is: I have two github accounts, one which I unintentionally used to make commits. This script helped fix all my commits by renaming the incorrect committer email/names. Of course, if I’ve been committing with the wrong user from, let’s say, 50th commit to 500th commit, there will be 450 diverged commits. Anyway, after running the script, as @andrej pointed out, you’ll need to git push -f to force push changes to the repo.

    – LWY

    Nov 16, 2018 at 1:45