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
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...
git rebase -i B
(here is an example of what you will see after executing the git rebase -i B command)
A
, use git rebase -i --root
C
and D
from pick
to edit
:wq
).C
git commit --amend --author="Author Name <email@address.com>"
git rebase --continue
D
git commit --amend --author="Author Name <email@address.com>"
againgit rebase --continue
git push -f
to update your origin with the updated commits.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
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.
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"
Steps to rename author name after commit pushed
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.
Then type "git rebase --continue" or "git rebase --abort" as per your need
pick 897fe9e simplify code a little
pick abb60f9 add new feature
pick dc18f70 bugfix
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>'
That's it, now just press ESC, :wq and you are all set
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
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.