I want to change the author of one specific commit in the history. It's not the last commit.
I know about this question - How do I change the author of a commit in git?
But I am thinking about something, where I identify the commit by hash or short-hash.
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: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
When doing
git rebase -i
there is this interesting bit in the doc:A-B-C-D-E-F
,B
andD
(= 2 commits),then you can do:
git config user.name "Correct new name"
git config user.email "correct@new.email"
git commit --allow-empty -m "empty"
git rebase -i B^
B^
selects the parent ofB
.pick
tosquash
for those.Example of what
git rebase -i B^
will give you:change that to:
It will prompt you to edit the messages:
and you can just remove the first few lines.
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 ammend them one by one and finish.
Start rebasing with
git rebase -i
. It will show you something like this.Change the
pick
keyword toedit
for the commits you want to change the author name.Then close the editor. For the beginners, hit
Escape
then type:wq
and hitEnter
.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 theNo rebase in progress?
message.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.
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, and42627abe
is the commit with the new author.Checkout the commit we are trying to modify.
Make the author change.
Now we have a new commit with hash assumed to be
42627abe
.Checkout the original branch.
Replace the old commit with the new one locally.
Rewrite all future commits based on the replacement.
Remove the replacement for cleanliness.
Push the new history (only use --force if the below fails, and only after sanity checking with
git log
and/orgit diff
).Instead of 4-6 you can just rebase onto new commit: