2981

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?

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
MicTech
  • 42,457
  • 14
  • 62
  • 79
  • 3
    Possible duplicate of [How to modify existing, unpushed commits?](https://stackoverflow.com/questions/179123/how-to-modify-existing-unpushed-commits) – tkruse Jan 21 '18 at 01:31
  • 49
    I just wanted to make it my currently configured `user`, so what I needed was `git commit --amend --reset-author` – Boris Verkhovskiy Jan 02 '22 at 09:45
  • Checkout the [`filter-repo` solution](https://stackoverflow.com/a/65685729/1814970), as it does everything for you using a Git recommended tool. – marcelocra Aug 25 '22 at 14:18
  • 4
    I did `git commit config --global user.name "your name"` and `git commit config --global user.email "your email"`, and then I did `git commit --amend --reset-author` – Alex Antoine Sep 21 '22 at 21:17

27 Answers27

4905

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@address.com>" --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@address.com>"
  6. Then git rebase --continue
  7. It would pause again at D
  8. Then you would git commit --amend --author="Author Name <email@address.com>" again
  9. git rebase --continue
  10. The rebase would complete.
  11. Use git push -f to update your origin with the updated commits.
Amber
  • 507,862
  • 82
  • 626
  • 550
  • 8
    @Mathew Byrne Or specify a1b3c3d4^ to refer to the preceding commit. – Alan Krueger Feb 15 '12 at 21:07
  • @AlanKrueger: How command-line will look like then? `git commit --amend --author="..." a1b3c3d4^`? Does not work for me :( – dma_k Feb 24 '12 at 17:56
  • 3
    @dma_k The `a1b3c3d4^` part would be for the rebase command - e.g. `git rebase -i a1b3c3d4^`. – Amber Feb 24 '12 at 18:18
  • OK, I got it. So commit `a1b3c3d4^` should be the one proceeding my commits. Suppose my commits where `abc01^` and `abc02^` (and that one is also the last one). What are my steps to change author for both of them? E.g. what should I specify for [interactive](http://book.git-scm.com/4_interactive_rebasing.html) `rebase`? `pick abc02` and `edit abc01`? And then `git commit --amend ...`? Please, edit your post for this case. Thanks in advance. – dma_k Feb 25 '12 at 13:09
  • 2
    I change the line from pick to edit but how do I continue... I am in a text editor but I have no idea of how to proceed after changing lines... – toto_tico Jun 11 '12 at 15:35
  • @toto_tico Just save the file being edited and then quit the editor and the rebase will continue automatically. – Amber Jun 11 '12 at 17:55
  • 1
    thanks @Amber but don't know how to save in this editor... is Ctrl + something? – toto_tico Jun 12 '12 at 16:27
  • 58
    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 '12 at 17:02
  • 44
    what if you want to modify the very first commit? What is the previous commit hash then? – Brenden Aug 31 '12 at 16:52
  • 7
    One caveat here: rebasing flattens merge history, which can lead to significantly modified history, an even difficult merge conflicts while rebasing. To make git not do this, add the `-p` option to `git rebase `. – asmeurer Dec 13 '12 at 21:00
  • 2
    Add `--reuse-message=HEAD` flag to avoid editing every commit message – Konstantin Pelepelin Sep 06 '13 at 11:38
  • 1
    @Amber As there a way to prevent git from asking me about the contents of each commit? I only want to modify several commits' authors - I don't want to edit the _content_ of any commits. Also, +1 for an excellent answer thus far. – CHK Oct 28 '13 at 19:18
  • 1
    @CHK see the comment immediately before yours. – Amber Oct 28 '13 at 20:23
  • @Amber isn't that the opposite of what I want? https://www.kernel.org/pub/software/scm/git/docs/git-commit.html says `--reuse-messages` preserves authorship. I want to change _only_ the authorship. – CHK Oct 28 '13 at 23:15
  • @CHK I believe explicitly setting the author overrides that (but feel free to test this). – Amber Oct 28 '13 at 23:29
  • Amber answer is correct, but if you are beginner, this website is helpful to let you know how it's work. Visit http://pcottle.github.io/learnGitBranching/ – Zhijian Tai Aug 29 '13 at 15:24
  • 31
    note that you can set the commit author to the current user with `git commit --amend --reset-author` – njzk2 Jan 14 '14 at 16:35
  • 294
    Use `--no-edit` option. `git commit --amend --reset-author --no-edit` won't open an editor. Available since git 1.7.9. – 5lava Jun 09 '14 at 18:43
  • 2
    would I need to do a force push in the end? – Amit Apr 16 '15 at 18:16
  • 2
    You are awesome!.. Saved my time. Thanks. Those who are not aware of editor interface, press "Insert" to turn on edit mode on screen while git bash propose you list of commits. Then, do changes as suggested in step 2. After you are done, press "Esc" to quite edit mode and press ":wq" + "Enter" to save and continue to next step. – Dhrumil Bhankhar Jul 25 '15 at 06:38
  • doesn't work for me. But this does https://help.github.com/articles/changing-author-info/ – rofrol Jan 26 '16 at 12:32
  • 5
    You have to force push to git after you are done with rebase using command `git push -f remote branch-name`, to apply the changes to remote. – DroidDev Feb 10 '16 at 06:34
  • Same question with @Breden. I want to change author of the first commit in history. How to do it? What's the preceding one? – emeraldhieu Feb 25 '16 at 12:59
  • @amber You said change the lines for both C and D to edit, but what should i put? – oshingc Mar 23 '16 at 16:36
  • @oshingc you literally change the word at the beginning of the line to the word `edit`. – Amber Mar 24 '16 at 00:23
  • 80
    @Brenden to modify the very first commit in the project, use `git rebase -i --root` – Noah Passalacqua May 04 '16 at 20:07
  • Does this keep a history of that change ? I mean is possible to realize that author name change after the rebase is done ? – rahpuser Nov 22 '16 at 15:32
  • 1
    @rahpuser not unless you have a copy of the older version of the history. rebase is a command that specifically rewrites history. – Amber Nov 24 '16 at 02:38
  • 5
    After follow all this steps I had to run ```git push -f``` – gsalgadotoledo Jan 17 '17 at 22:15
  • Great answer, a quick way to get the right would be to do HEAD~1. – JavierIEH Mar 27 '17 at 18:20
  • How exactly does one point local back to the newest commit? I'm stuck at 5 commits ahead of master now... – notAChance May 03 '17 at 17:15
  • 4
    Beware this also changes your commit dates, which is generally an undesirable effect. – Pablo Gonzalez Portela Aug 16 '17 at 13:43
  • 2
    Adding `-C @`, e.g. `git commit --amend --author="Author Name -C @` will save you some steps. This will automatically reuse the commit message and the editor will not open at all. – Ryan H. Nov 17 '17 at 01:33
  • If the base commit are in the same branch rebase is not performed, you need to use "--force" param: "git rebase -i B --force" in order to force the rebase. – Javier Ros Mar 14 '18 at 12:20
  • 2
    if you've actually changed your git config (with `git config --global user.name "Developer Display Name"` and `git config --global user.email "Developer Email Address"`) then `git commit --amend --author="Author Name "` can be shortened to `git commit --amend --reset-author` – Paul Sturm Jun 06 '18 at 18:30
  • 1
    git rebase -i --root to ammend the first commit – Sudip Bhandari Apr 28 '19 at 17:55
  • then finally how to merge those change into main branch? it refuse to merge when I was trying to do so – http8086 Dec 24 '20 at 22:17
  • 1
    I think the answer should be modified to include option `-p` for `rebase` and explain what it does. In addition, `git push -f` should be replaced with `git push --force-with-lease` to teach people to use the safe method by default. – Mikko Rantalainen Jan 15 '21 at 09:09
  • This leaves the old commits in the history, including the information we want to edit. – br4nnigan Mar 02 '21 at 08:14
  • 1
    Note that the `--reset-author` option also renews the author timestamp, whereas the `--author="Author Name "` option does not. – luizfls Apr 29 '21 at 03:35
  • 1
    `GIT_COMMITTER_NAME="Author Name" GIT_COMMITTER_EMAIL="author@email.com" git commit --amend --author="authorname " --no-edit` This gets committer name update too apart from author. – enthusiasticgeek Dec 31 '21 at 02:54
  • You don't actually need to change "pick" to "edit" then do the commit --amend on the commandline -- you can instead add "exec git commit --amend ..." to the interactive rebase script immediately after any "pick" of a commit you want to change. – Alice Purcell Jan 31 '23 at 17:32
  • fatal: You are in the middle of a rebase -- cannot amend. – Nike Feb 15 '23 at 04:28
737

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.

  3. Checkout the original branch.

  4. Replace the old commit with the new one locally:

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

     git filter-branch -- --all
    
  6. Remove the replacement for cleanliness:

     git replace -d 03f482d6
    
  7. Push the new history:

     git push --force-with-lease
    

    (Only use --force instead of --force-with-lease if the latter fails, and only after sanity checking with git log and/or git diff.)

Instead of steps 4–5 you can just rebase onto the new commit:

git rebase -i 42627abe
ib.
  • 27,830
  • 11
  • 80
  • 100
merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • 9
    Please put a note in there to re-checkout your original branch after step 2. – Benjamin Riggs Mar 11 '15 at 00:26
  • 1
    @Benjamin, That did not appear to be necessary when I did this. Are you sure? – merlin2011 Mar 11 '15 at 00:29
  • Well, I don't believe it's necessary for step 3, but I had to checkout my branch to reattach HEAD for step 4 to work when I tried. At least I think that's what happened to me... – Benjamin Riggs Mar 11 '15 at 05:49
  • 1
    @Benjamin, Actually that doesn't make sense because Step 4 touches _all_ refs regardless of where your `HEAD` currently is. – merlin2011 Mar 11 '15 at 05:51
  • 71
    This looks much clear alternative to the horrific `git rebase -i`. Never heard of this `git replace` thing before. +1 – FractalSpace Jul 08 '15 at 23:29
  • I have a few commits that need fixing. Can I combine multiple `git replace` and then issue a single `git filter-branch` (since it takes quite some time to run)? – rustyx Dec 06 '15 at 21:12
  • @rustyx, I see no reason why it would not work, but I have never tried it. – merlin2011 Dec 07 '15 at 07:50
  • 3
    For clean the refs/original/... backup see [here](http://stackoverflow.com/a/7654880/1342186) – alexis Mar 09 '16 at 17:54
  • 2
    This answer is just workaround for people with horrible git histories, which consist of rebase/merge/rebase/merge cycle done until the whole thing was no longer rebase-able (or readable for that matter). The `git rebase -i` is *the* right approach for everyone else. Nevertheless, this is a good workaround. – user1643723 Aug 18 '16 at 14:13
  • @RustyX yes you can – plugwash Jan 05 '17 at 12:24
  • 3
    any idea how to preserve the date on the commit? Now it's newer then the commits on top of it – Tudor Carean Jan 20 '17 at 12:51
  • 2
    @TudorCarean, I think there's a [separate question](http://stackoverflow.com/questions/454734/how-can-one-change-the-timestamp-of-an-old-commit-in-git) on that, and I would guess one of those solutions can be combined with this one. – merlin2011 Jan 20 '17 at 22:49
  • 1
    @merlin2011 step 4. Rewrite all future commits based on the replacement.Appreciate if you could have go more about it? – RaGa__M Apr 13 '17 at 07:19
  • @FallingFromBed, All future commit hashes are based on the commit hash of the ancestor commit, and the ancestor commit's hash is based on its author name. Thus, if we update on commit, we must update all commits that depend on it. – merlin2011 Apr 13 '17 at 07:22
  • 11
    I recommend using `--force-with-lease` instead of `-f`. It's safer. – Jay Bazuzi Jul 03 '17 at 17:14
  • 2
    @merlin2011, thanks for a great post. I have actually tried it myself and it works great, but with a small change: after `git replace -d` in `Step 5` a `git checkout master` command seems to be needed. Otherwise I'm still on an unnamed branch and `git push` doesn't work there. What are your thoughts on that? – Lukasz Czerwinski Jul 30 '17 at 19:14
  • 1
    @LukaszCzerwinski, I'm glad you enjoyed it! I think my push configuration may be different, so I did not have to checkout master before pushing. – merlin2011 Jul 30 '17 at 19:27
  • 1
    Re: "all future commits" part. I was attempting to change author only 3 commits back from HEAD, but _filter-branch_ started to change over 16000 commits, back to the beginning of repo time apparently. Is that really correct? – matt wilkie Nov 29 '17 at 23:20
  • @mattwilkie, are you sure those commits are actually changed? I think filter branch will examine every commit, but I don't think it changes every commit. – merlin2011 Nov 30 '17 at 16:41
  • 2
    @merlin2011 Oh, no I don't know if they were changed. I aborted the operation. I didn't/don't see why changing a commit in the last day would need to go back to the beginning of time and search forward from zero. – matt wilkie Dec 04 '17 at 22:11
  • @mattwilkie, If the commit you're trying to modify is very recent, I'd say you should probably be using the accepted answer to this question. I wrote this answer when I had to handle a messy history and the commit I wanted to update was a month old. I'd go as far as to say that if you have a completely linear history from the present time back to the point you need to change, there's no need to use `filter-branch`. – merlin2011 Dec 05 '17 at 00:15
  • 30
    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 '18 at 10:45
  • When i execute `git push --force-with-lease` show me `Everything up-to-date` – jose920405 Feb 14 '18 at 14:25
  • This worked for me, thanks! Please note that if you don't have a remote, you must skip the `git push` step and directly checkout your branch. – Tanase Butcaru Mar 12 '18 at 08:14
  • After using this method (only with local non-pushed commits, of course), github reports "entirely different commit histories" when trying to start a new pull request. Is this expectable or did I mess up anything? – Daniel García Rubio May 28 '18 at 15:38
  • 1
    @DanielGarcíaRubio, That is expected. Github tends to freak out when you do a pull request with divergent histories; you will find this to be the case with any answer that rewrites history. Unfortunately I don't know of a good workaround for this, since it seems to be a Github-internal issue. – merlin2011 Jun 02 '18 at 00:19
  • If you have to amend the author of multiple commits, this script will help: https://gist.github.com/philipjkim/f032887427227b6ec5fcde34ab406647 (all commands in the script are from this ansewer) – philipjkim Mar 13 '19 at 01:13
  • what happens to the tags ? :/ – Amir Ziarati Jan 16 '20 at 21:14
  • perfect implementation to amend git commit message/author, thanks @merlin2011 – deeptimancode Jun 14 '21 at 20:50
  • @merlin2011 Do we no longer need to delete the old commit if we just rebase? That is, shouldn't "Instead of 4-6 you can just rebase onto new commit:" read as "Instead of 4-5 you can just rebase onto new commit:"? – ShortM Sep 08 '21 at 10:36
  • 1
    @ShortM, I think you're correct. Will edit. To be clear, the `git replace -d` does not actually delete a commit. It deletes a replace ref. – merlin2011 Sep 09 '21 at 18:04
  • As per [this](https://stackoverflow.com/a/70892888/5049813), I needed to do a `git fetch` before my `git push --force-with-lease` (when I did, I saw a "forced update" to the main branch which seemed to do nothing) – Pro Q Oct 13 '22 at 20:47
  • I had created a repo with old creds, when I realized, I was looking for a way to update the root commit, but nothing worked. All of the `rebase -i` solutions wouldn't touch the root. This was the only thing that worked. Thx!! – Adrian Nov 27 '22 at 05:55
  • Technically, `rebase -i --root` should work to touch the root commit. – merlin2011 Nov 27 '22 at 23:52
  • This worked great to amend my authorship, but now my initial commit is showing as after my second commit! – Tom Wagstaff Jan 13 '23 at 16:19
  • @TomWagstaff This seems unexpected and I'm curious. Would you consider asking a new question with a minimum example of this effect and linking it? – merlin2011 Jan 14 '23 at 09:18
  • Sure, but let's just check we understand one another :-) - what I mean is that the datetime stamp for the "initial commit" is at a later time - the time I did the amendment - than the second commit, which has kept its original datetime. But the "order" e.g. in the commit tree is still correct – Tom Wagstaff Jan 17 '23 at 15:15
  • Ah, I think that is indeed expected behavior. If you want to fix the timestamp, you could try using this answer: https://stackoverflow.com/a/41997774/391161 – merlin2011 Jan 17 '23 at 23:38
361

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="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

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'
rekire
  • 47,260
  • 30
  • 167
  • 264
olivieradam666
  • 4,522
  • 2
  • 20
  • 25
  • 21
    This changes it in all commits, not just one. Funny thing, I did this less than 30 minutes ago. – Artjom B. Jun 09 '15 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. – Ruslan Gerasimov Aug 08 '16 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. – VasiliNovikov Jan 18 '18 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 '18 at 14:47
  • this works, but in my case, when i try to do push again.. starts uploading a big `mb`, as if trying to upload a full git history again – jose920405 Nov 07 '18 at 18:50
  • 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 '18 at 01:45
  • The link is broken now that Github revamped their helpdesk, and I can't seem to find the old article anywhere. This is the last surviving copy of this script, and it's the only one that works for me. – Qix - MONICA WAS MISTREATED Dec 29 '20 at 17:36
  • 1
    Be warned that doing `git push --force ...` will overwrite ANY changes that anybody else has done. In most cases you really want to use `--force-with-lease` instead of `--force` to avoid fatal mistakes. In addition, if the remote server is used by multiple developers, it must be understood that all developers will need to switch to new history once this is done. Otherwise you'll get total mess if people start merging between original and rewritten history. – Mikko Rantalainen Jan 15 '21 at 09:11
  • The link is now broken. This is the last snapshot of it on the Internet Archive: https://web.archive.org/web/20200823163529/https://docs.github.com/en/github/using-git/changing-author-info#changing-the-git-history-of-your-repository-using-a-script Note: the only difference from the answer is that GitHub suggests to use a clean bare repo for applying this trick and then delete it: ```git clone --bare ``` – Caliuf Nov 21 '21 at 12:08
357

If you just want to change the author of your last commit, you can do this:

  • Reset your email to the config globally:

    git config --global user.email example@email.com

  • Now reset the author of your commit without edit required:

    git commit --amend --reset-author --no-edit

Note this will also change the author timestamp.

Alice Purcell
  • 12,622
  • 6
  • 51
  • 57
pravsels
  • 3,756
  • 1
  • 13
  • 8
  • 5
    No, it's not. Look at the OP: `It's not last commit.` So how would they `amend` it? – underscore_d Aug 31 '17 at 13:25
  • 6
    This is great, it's a shame it's the last commit only, though. I needed it on the latest two, fortunately, so just did a `git reset HEAD~`, ran your suggested lines, then did the next commit manually again. Worked fine! – Matt Fletcher Sep 18 '17 at 10:56
  • 3
    Thanks! The --reset-author did the trick for me, since without it the author changes but the "commiter" stays with the old author details. – Lucas P. Nov 13 '18 at 11:23
  • 51
    To fix my last six commits: First set the correct author for current Git repo using `git config --local user.name FirstName LastName` and `git config --local user.email first.last@example.com`. Then apply to the last six commits using `git rebase --onto HEAD~6 --exec "git commit --amend --reset-author --no-edit" HEAD~6`. Finally push it to remote Git repo using `git push --force-with-lease`. – oHo Oct 14 '19 at 12:32
  • 2
    @oHo I wish this was an answer so I can upvote it. This was the only thing that solved the issue for me, huge pain in the ascot this has been. – Chris Nov 23 '21 at 13:06
  • 1
    Agree that @oHo example works, is simple, and lets you control how far back in commit history that you want to change. – Clark Updike Mar 16 '22 at 13:31
  • Amazing, exactly what I was looking for. I needed to change the author on the last commit, and this did it in the most simple of ways. – instanceof Jul 04 '22 at 13:27
  • 1
    @oHo This should be the best answer, simple and easy! – agfe2 Aug 10 '22 at 01:37
  • 1
    Hi @agfe2 OK I have pushed my comment as an answer. https://stackoverflow.com/a/73857920/938111 Please give me some feedback if you think the answer may be improved. Have a nice day ;-) – oHo Sep 26 '22 at 17:42
  • Hi @ClarkUpdike I have just converted my comment as an answer as you have suggested. https://stackoverflow.com/a/73857920/938111 What do you suggest to improve it? Have a nice day ;-) – oHo Sep 26 '22 at 17:44
  • Hi @Chris I have just converted my comment as an answer as you have suggested last year. https://stackoverflow.com/a/73857920/938111 Any other suggestion? Thanks, Have a nice day ;-) – oHo Sep 26 '22 at 17:46
133

You can change author of last commit using the command below.

git commit --amend --author="Author Name <email@address.com>"

However, if you want to change more than one commits author name, it's a bit tricky. You need to start an interactive rebase then mark commits as edit then amend them one by one and finish.

  1. Start rebasing with git rebase -i.

  2. Change the pick keyword to edit for the commits you want to change the author name.

  3. Then close the editor. For the beginners, hit Escape then type :wq and hit Enter.

Then you will see your terminal like nothing happened. Actually you are in the middle of an interactive rebase. Now it's time to amend your commit's author name using the command above. It will open the editor again. Quit and continue rebase with git rebase --continue. Repeat the same for the commit count you want to edit. You can make sure that interactive rebase finished when you get the No rebase in progress? message.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Fatih Acet
  • 28,690
  • 9
  • 51
  • 58
  • 1
    If you got multiple commits to change, instead of editing them individually, you can also let the `pick` action, and add after each line `exec git commit --no-edit --amend --author="MyNewAuthor "` – Pierre-Olivier Vares Feb 05 '20 at 09:05
  • I am not related to the site in the link, but I have used this link https://www.git-tower.com/learn/git/faq/change-author-name-email/, and then combined with this post (I used `rebase -i -p`), but at the end this is what I needed. Thanks – Aleks May 26 '21 at 17:58
  • For beginners run `git config --global core.editor nano` before running `git commit` and then you can use Ctrl+O, Ctrl+X to exit the editor. – Tom Kelly May 11 '22 at 04:48
68

The answers in the question to which you linked are good answers and cover your situation (the other question is more general since it involves rewriting multiple commits).

As an excuse to try out git filter-branch, I wrote a script to rewrite the Author Name and/or Author Email for a given commit:

#!/bin/sh

#
# Change the author name and/or email of a single commit.
#
# change-author [-f] commit-to-change [branch-to-rewrite [new-name [new-email]]]
#
#     If -f is supplied it is passed to "git filter-branch".
#
#     If <branch-to-rewrite> is not provided or is empty HEAD will be used.
#     Use "--all" or a space separated list (e.g. "master next") to rewrite
#     multiple branches.
#
#     If <new-name> (or <new-email>) is not provided or is empty, the normal
#     user.name (user.email) Git configuration value will be used.
#

force=''
if test "x$1" = "x-f"; then
    force='-f'
    shift
fi

die() {
    printf '%s\n' "$@"
    exit 128
}
targ="$(git rev-parse --verify "$1" 2>/dev/null)" || die "$1 is not a commit"
br="${2:-HEAD}"

TARG_COMMIT="$targ"
TARG_NAME="${3-}"
TARG_EMAIL="${4-}"
export TARG_COMMIT TARG_NAME TARG_EMAIL

filt='

    if test "$GIT_COMMIT" = "$TARG_COMMIT"; then
        if test -n "$TARG_EMAIL"; then
            GIT_AUTHOR_EMAIL="$TARG_EMAIL"
            export GIT_AUTHOR_EMAIL
        else
            unset GIT_AUTHOR_EMAIL
        fi
        if test -n "$TARG_NAME"; then
            GIT_AUTHOR_NAME="$TARG_NAME"
            export GIT_AUTHOR_NAME
        else
            unset GIT_AUTHOR_NAME
        fi
    fi

'

git filter-branch $force --env-filter "$filt" -- $br
Chris Johnsen
  • 214,407
  • 26
  • 209
  • 186
66

My 2019's comment converted into an answer:

To fix authoring on the last six commits

  1. First set the correct author for current Git repo

    git config --local user.name "FirstName LastName"
    git config --local user.email first.last@example.com
    
  2. Then apply the fix to the last six commits

    git rebase --onto HEAD~6 --exec "git commit --amend --reset-author --no-edit" HEAD~6
    
  3. Finally force push to the remote Git repo

    git push --force-with-lease
    
Antimonit
  • 2,846
  • 23
  • 34
oHo
  • 51,447
  • 27
  • 165
  • 200
  • 1
    In my case I had to quote the name, like `"FirstName LastName"`, just a heads up – Daniel Galarza Nov 22 '22 at 01:27
  • @DanielGalarza yep, multiple strings must be inside quotes – Ivandez Nov 28 '22 at 18:34
  • 1
    one more heads up, this will REWRITE the commit, which means it'll change the commit time as well. it happened as I wasn't aware initially when I ran the command. this is sweet and simple way if timestamp is not of concern. :) – Rajiv Mar 31 '23 at 13:36
  • Works really well! Needed to update my remote on Github, updated remotely instantly. Top-notch steps! – MarcolinoPT Jul 06 '23 at 16:36
56

Find a way that can change user quickly and has no side effect to others commits.

Simple and clear way:

git config user.name "New User"
git config user.email "newuser@gmail.com"

git log
git rebase -i 1f1357
# change the word 'pick' to 'edit', save and exit

git commit --amend --reset-author --no-edit
git rebase --continue

git push --force-with-lease

detailed operations

  • show commit logs and find out the commit id that ahead of your commit which you want to change:
git log
  • git rebase start from the chosed commit id to the recent reversely:
git config user.name "New User"
git config user.email "newuser@gmail.com"
git rebase -i 1f1357

# change word pick to edit, save and exit
edit 809b8f7 change code order 
pick 9baaae5 add prometheus monitor kubernetes
edit 5d726c3 fix liquid escape issue   
edit 3a5f98f update tags
pick 816e21c add prometheus monitor kubernetes
  • rebase will Stopped at next commit id, output:
Stopped at 809b8f7...  change code order 
You can amend the commit now, with
  git commit --amend 

Once you are satisfied with your changes, run

  git rebase --continue
  • comfirm and continue your rebase untill it successfully to refs/heads/master.
# each continue will show you an amend message
# use git commit --amend --reset-author --no-edit to comfirm
# use git rebase --skip to skip
git commit --amend --reset-author --no-edit
git rebase --continue
git commit --amend --reset-author --no-edit
...
git rebase --continue
Successfully rebased and updated refs/heads/master.
  • git push to update
git push --force-with-lease
NOZUONOHIGH
  • 1,892
  • 1
  • 20
  • 20
  • Does first solution works if commit I want to change is first commit and there are 3 commits after it ? @NOZUONOHIGH – strix25 Sep 12 '20 at 08:46
  • 1
    The danger here is that you change `user.name` and `user.email` in config but then don't change them back. If you don't change them back all _future_ commits will be by the new user too! This is why it might be better to use the `--author` flag on `git commit` instead. – Nick F Sep 15 '20 at 14:53
  • this one, the Simple version, worked like a charm – Jonatas CD Apr 14 '22 at 09:36
51

Commit before:

enter image description here

To fix author for all commits you can apply command from @Amber's answer:

git commit --amend --author="Author Name <email@address.com>"

Or to reuse your name and email you can just write:

git commit --amend --author=Eugen

Commit after the command:

enter image description here

For example to change all starting from 4025621:

enter image description here

You must run:

git rebase --onto 4025621 --exec "git commit --amend --author=Eugen" 4025621

Note: To include an author containing spaces such as a name and email address, the author must be surrounded by escaped quotes. For example:

git rebase --onto 4025621 --exec "git commit --amend --author=\"Foo Bar <foo@bar.com>\"" 4025621

or add this alias into ~/.gitconfig:

[alias]
    reauthor = !bash -c 'git rebase --onto $1 --exec \"git commit --amend --author=$2\" $1' --

And then run:

git reauthor 4025621 Eugen
Taylor D. Edmiston
  • 12,088
  • 6
  • 56
  • 76
Eugen Konkov
  • 22,193
  • 17
  • 108
  • 158
43

In furtherance to Eugen Konkov answer, to start from the root commit, use --root flag. The --no-edit flag is helpful too, because with it you are not prompted into an editor for each commit.

git rebase --root --exec "git commit --amend --author='name <email>' --no-edit"
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
korwalskiy
  • 927
  • 12
  • 12
19

When doing git rebase -i there is this interesting bit in the doc:

If you want to fold two or more commits into one, replace the command "pick" for the second and subsequent commits with "squash" or "fixup". If the commits had different authors, the folded commit will be attributed to the author of the first commit. The suggested commit message for the folded commit is the concatenation of the commit messages of the first commit and of those with the "squash" command, but omits the commit messages of commits with the "fixup" command.

  • If you have an history of A-B-C-D-E-F,
  • and you want to change commits B and D (= 2 commits),

then you can do:

  • git config user.name "Correct new name"
  • git config user.email "correct@new.email"
  • create empty commits (one for each commit):
    • you need a message for rebase purpose
    • git commit --allow-empty -m "empty"
  • start the rebase operation
    • git rebase -i B^
    • B^ selects the parent of B.
  • you will want to put one empty commit before each commit to modify
  • you will want to change pick to squash for those.

Example of what git rebase -i B^ will give you:

pick sha-commit-B some message
pick sha-commit-C some message
pick sha-commit-D some message
pick sha-commit-E some message
pick sha-commit-F some message
# pick sha-commit-empty1 empty
# pick sha-commit-empty2 empty

change that to:

# change commit B's author
pick sha-commit-empty1 empty
squash sha-commit-B some message
# leave commit C alone
pick sha-commit-C some message
# change commit D's author
pick sha-commit-empty2 empty
squash sha-commit-D some message
# leave commit E-F alone
pick sha-commit-E some message
pick sha-commit-F some message

It will prompt you to edit the messages:

# This is a combination of 2 commits.
# The first commit's message is:

empty

# This is the 2nd commit message:

...some useful commit message there...

and you can just remove the first few lines.

dnozay
  • 23,846
  • 6
  • 82
  • 104
19

There is one additional step to Amber's answer if you're using a centralized repository:

git push -f to force the update of the central repository.

Be careful that there are not a lot of people working on the same branch because it can ruin consistency.

Community
  • 1
  • 1
Fabian76
  • 389
  • 2
  • 11
11

If the commit that you want to change is not the last commit, then follow the below steps. If your commit is in different branch then first switch to that branch.

git checkout branch_name

Find commit before the commit that you want to change and find its hash. Then issue rebase command.

git rebase -i -p hash of commit

Then an editor will open and enter 'edit' for the commits that you want to change. Leave others with default 'pick' option. Once changed enter 'esc' key and wq! to exit.

Then issue git commit command with amendment option.

git commit --amend --author="Username email" --no-edit

Then issue the following command.

git rebase --continue

Once commit author is updated in the local repository, push the changes to the remote repository.

ChannaB
  • 439
  • 4
  • 13
10

SOLUTION

  1. Install git filter-repo (Git project recommends filter-repo over filter-branch)

    $ PACKAGE_TOOL install git-filter-repo
    
  2. Create a file .mailmap in the root of the git repository containing

    New Name <new@ema.il> <old@ema.il>
    
  3. Run git filter-repo --use-mailmap


MORE DETAILS

  • git-filter-repo lists this as an example in their docs
  • Instead of replacing both the name and the email like in the example above, take a look at additional examples in git mailmap documentation
  • Instead of using a file named .mailmap by default, you can specify mailmap file by invoking git filter-repo with argument --mailmap <filename>.
  • Many more examples on how to further filter branch/tag/whatever can be found in the git-filter-repo project's README.md.
maricn
  • 593
  • 1
  • 6
  • 21
  • 1
    `brew install git-filter-repo` works in a Mac. – marcelocra Aug 25 '22 at 16:34
  • Confirmed. `brew` install works for Mac users and running `git filter-repo --use-mailmap` does the magic! Have to mention that this method allows multiple author changes. Just press Enter and add many lines as you need: `New Name1 New Name2 ` – filoscoder Dec 21 '22 at 16:16
  • git-filter-repo is the best Git maintenance tool I've ever found. I love it so much and I'm grateful to find yet another cool thing to add. – Mattie Mar 13 '23 at 13:35
8

For the merge commit message, I found that I cannot amend it by using rebase, at least on gitlab. It shows the merge as a commit but I cannot rebase onto that #sha. I found this post is helpful.

git checkout <sha of merge>
git commit --amend # edit message
git rebase HEAD previous_branch

This three lines of code did the job for changing the merge commit message (like author).

Jason Liu
  • 749
  • 4
  • 16
  • 34
  • 1
    Thanks. This worked perfect. I made 2 commits, realized I didn't set the correct author info before doing so. Reverted the last commit simply by using `git commit --amend --author="Author " and the followed these steps to fix the one before it. – JustSomeQuickGuy May 17 '22 at 05:20
8

Using Interactive Rebase

git rebase -i -p <some HEAD before all of your bad commits>

Then mark all of your bad commits as "edit" in the rebase file, and when git asks you to amend each commit, do

git commit --amend --author "New Author Name <email@address.com>"

edit or just close the editor that opens, and then do

git rebase --continue

to continue the rebase.

You could skip opening the editor altogether here by appending --no-edit so that the command will be:

git commit --amend --author "New Author Name <email@address.com>" --no-edit && \
git rebase --continue

Single Commit

As some of the commenters have noted, if you just want to change the most recent commit, the rebase command is not necessary. Just do

git commit --amend --author "New Author Name <email@address.com>"

This will change the author to the name specified, but the committer will be set to your configured user in git config user.name and git config user.email. If you want to set the committer to something you specify, this will set both the author and the committer:

git -c user.name="New Author Name" -c user.email=email@address.com commit --amend --reset-author
Tejas Savaliya
  • 572
  • 7
  • 8
7

Steps to rename author name after commit pushed

  1. First type "git log" to get the commit id and more details
  2. git rebase i HEAD~10 (10 is the total commit to display on rebase)

    If you Get anything like below

    fatal: It seems that there is already a rebase-merge directory, and I wonder if you are in the middle of another rebase. If that is the case, please try

    git rebase (--continue | --abort | --skip) If that is not the case, please rm -fr ".git/rebase-merge" and run me again. I am stopping in case you still have something valuable there.

  3. Then type "git rebase --continue" or "git rebase --abort" as per your need

    • now your will rebase window opened, click "i" key from keyboard
    • then you will get list of commits to 10 [because we have passed 10 commit above] Like below

    pick 897fe9e simplify code a little

    pick abb60f9 add new feature

    pick dc18f70 bugfix

  4. Now you need to add below command just below of the commit you want to edit, like below

    pick 897fe9e simplify code a little exec git commit --amend --author 'Author Name <author.name@mail.com>' pick abb60f9 add new feature exec git commit --amend --author 'Author Name <author.name@mail.com>' pick dc18f70 bugfix exec git commit --amend --author 'Author Name <author.name@mail.com>'

    1. That's it, now just press ESC, :wq and you are all set

    2. Then git push origin HEAD:BRANCH NAME -f [please take care of -f Force push]

    like git push -f or git push origin HEAD: dev -f

Kirtikumar A.
  • 4,140
  • 43
  • 43
7

OPTIONAL: Make sure to stash your local changes if you don't want to send them to remote.

$ git status
$ git stash

Update the author for the last commit.

$ git log   // Old author in local and remote
$ git commit --amend --author="Author Name <email@address.com>"
$ git log   // New Author in local
$ git push origin <branch> --force-with-lease 
$ git log   // New Author in remote

Then, if you used git stash then recovers your staged changes

$ git stash pop
$ git status

Then, you should to update the configuration for the next commits of the current project.

$ git config user.name "Author Name"
$ git config user.email "<email@address.com>"

And check or also edit this with git config --edit


Clarification: In the rare case that you lose commits using $ ggpush -f you can recover them with reflog. Anyway using --force-with-lease you are protected even more than if you use only -f

GL

Braian Coronel
  • 22,105
  • 4
  • 57
  • 62
  • Downvoted because if there is a way to do something without git push -f, then do it without git push -f. – Seth Aug 17 '20 at 00:55
  • 1
    @Seth There I updated my answer using --with-lease. In what way do you refer anyway? I will try to optimize the commands. Thanks for the feedback – Braian Coronel Aug 17 '20 at 23:57
7

It could happen if you're missing settings on your machine, e.g., after a format, or when not having a Git configured correctly without setting up (correctly) these commands.

git config user.name "Author Name"
git config user.email "<email@address.com>"

Why not make your life simpler by following this article by Atlassian?

  1. git commit --amend --author="Author Name <email@address.com>"
  2. Unprotect your branch, if it's protected. In this example, it's master; therefore, it'll be protected by the source code repository
  3. git push origin master --force

That's the simplest scenario for the last commit. For picking up any "random" commit, you need:

  1. git rebase -i <Earlier Commit>.
  2. Change pick on edit on that commit, in which you're interested in
  3. git commit --amend --author="Author Name <email@address.com>"
  4. Unprotect your branch if it's protected. In this example, it's master; therefore, it'll be protected by the source code repository
  5. git push origin master --force

You can always git log in between to be sure where you are before you push.

Daniel Danielecki
  • 8,508
  • 6
  • 68
  • 94
6

There is a shortcut applicable to the most voted question: using exec instead of edit.

exec allows to run a command on a specified commit.
Using it allows to avoid using edit, exiting to a terminal and running the git command for each git commit.
This is especially helpful if you have to change multiple commits in the history.

The steps are:

  1. perform a rebase to an earlier commit (git rebase -i <earliercommit>)
  2. in the editor that opens up, add a line after each commit line you want to edit and add exec git commit --amend --author="Author Name <email@address.com>" --no-edit (or using --reset-author if you want to reset to the value set in the git config)
  3. save and exit - this will run the specified command for each commit, effectively changing the author

Example editor content (to change first 2 commits author):

pick 1fc6c95 Patch A
exec git commit --amend --author="Author Name <email@address.com>" --no-edit
pick 6b2481b Patch B
exec git commit --amend --author="Author Name <email@address.com>" --no-edit
pick dd1475d something I want to split
pick c619268 A fix for Patch B
pick fa39187 something to add to patch A
pick 4ca2acc i cant' typ goods
pick 7b36971 something to move before patch B

# Rebase 41a72e6..7b36971 onto 41a72e6
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
endorama
  • 498
  • 7
  • 15
6

As an addition to Eugen Konkov's answer, it is possible to keep the commit/author date. To only modify the author but not the date of the last three commits use

git rebase --onto HEAD~3 --exec 'GIT_COMMITTER_DATE="$(git log -n 1 --format=%aD)" git commit --amend --reset-author --no-edit --date="$(git log -n 1 --format=%aD)"' HEAD~3

then apply a force push

git push --force-with-lease
buergi
  • 6,039
  • 3
  • 19
  • 15
5

you can use these commands from official page of github

https://help.github.com/en/github/using-git/changing-author-info

here is the commands

#!/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

here u can change the old email to ur new user name and email address.

Mudassir Khan
  • 1,714
  • 1
  • 20
  • 25
5

One important thing to mention especially if you are deleting author information for privacy.

After operations from upper answers:

git commit --amend --author="Author Name <email@address.com>"
git push -f

The commits with old author info still exist in git local & remote cache.

As Github you can still access the commit by SHA1, with a warning "This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository."

Garbage collect can be executed within local repo:

git gc

Howerver for Github, which you cannot trigger this command, go to Support https://support.github.com/request/remove-data and submit Clear Cached Views ticket on your repo.

This last step is applied to all commit modify/remove operations.

Ref: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository#fully-removing-the-data-from-github

Warning: This article tells you how to make commits with sensitive data unreachable from any branches or tags in your repository on GitHub.com. However, those commits may still be accessible in any clones or forks of your repository, directly via their SHA-1 hashes in cached views on GitHub, and through any pull requests that reference them. You cannot remove sensitive data from other users' clones of your repository, but you can permanently remove cached views and references to the sensitive data in pull requests on GitHub by contacting GitHub Support.

V.E.O
  • 875
  • 8
  • 11
4

There is also a lazy approach to this problem, especially if you have more than one commit that you want to change. In my case, I had a new branch with several commits with a wrong author, so what helped me:

Go to your original branch:

git checkout develop

Create new branch from it:

git checkout -b myFeature develop 

Merge it without commit info as one commit:

git merge --no-commit --squash branchWrongAuthor

You might also want to stage changes:

git stage .

Change the name of the author and commit changes:

git commit --amend --author "New Author Name <New Author Email>" -m "new feature added"

And that's it, you can push the changes.

git push

You can delete the branch with a wrong author after that.

alexlz
  • 618
  • 1
  • 10
  • 24
4

Changing Your Committer Name & Email Globally:

$ git config --global user.name "John Doe"
$ git config --global user.email "john@doe.org"

Changing Your Committer Name & Email per Repository:

$ git config user.name "John Doe"
$ git config user.email "john@doe.org"

Changing the Author Information Just for the Next Commit:

$ git commit --author="John Doe <john@doe.org>"

Hint: For other situation and read more information read the post reference.

AmerllicA
  • 29,059
  • 15
  • 130
  • 154
3

The preferred answer, the one using git rebase -i is efficient, but as highlighted in this other answer, it becomes messy when there are merges around the commits to edit. And the use of git replace is smart but git filter-branch rewrites all the history of other branches and tags, which is not what we want in general.

I wanted to share an alternative to the first answer that remains easy even when there are merges. In my case, when I used git rebase -i <earlier-commit>, I got a conflict to solve first, before proceeding with the rebase. In fact, it is easier to use the break command rather than the edit one. And directly rebase on the commit we target.

Let's take an example and let's assume git log shows...

commit a12afg
...
commit dloe7a
...
commit gh7ag1
...   
commit qp3zaa
...

And let's say you want to update the author, message or commit signature for the commit gh7ag1. You can proceed with git rebase -i gh7ag1. In you editor, you will see:

pick dloe7a
pick a12afg

Just add a break command:

break
pick dloe7a
pick a12afg

Save (:wq with VI, Ctrl+O then Ctrl+X with nano). And now, you are back right after your commit. You can run git commit --amend to update the author, the message or the signature (e.g. git commit --amend -S --author="Your Name <your-email>"). Verify with git log --show-signature. If correct, you can go on with git rebase --continue.

And you can have as many break commands in the rebase. The continue will move to the next break, if there is one, or apply the remaining commits (provided they are marked as pick).

Vincent Zurczak
  • 211
  • 2
  • 5
2

If what you need to change is the AUTHOR OF THE LAST commit and no other is using your repository, you may undo your last commit with:

git push -f origin last_commit_hash:branch_name 

change the author name of your commit with:

git commit --amend --author "type new author here"

Exit the editor that opens and push again your code:

git push
yugr
  • 19,769
  • 3
  • 51
  • 96
pebox11
  • 3,377
  • 5
  • 32
  • 57