I was writing a simple script in the school computer, and committing the changes to Git (in a repo that was in my pendrive, cloned from my computer at home). After several c
A single command 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"
NOTES
--no-edit
flag makes sure the git commit --amend
doesn't ask an extra confirmationgit 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
exec git commit --amend --author 'Author Name <author.name@mail.com>' --no-edit
pick abb60f9 add new feature
exec git commit --amend --author 'Author Name <author.name@mail.com>' --no-edit
pick dc18f70 bugfix
exec git commit --amend --author 'Author Name <author.name@mail.com>' --no-edit
You can then still modify some lines to see where you want to change the author. This gives you a nice middle ground between automation and control: you see the steps that will run, and once you save everything will be applied at once.
If the commits you want to fix are the latest ones, and just a couple of them, you can use a combination of git reset
and git stash
to go back an commit them again after configuring the right name and email.
The sequence will be something like this (for 2 wrong commits, no pending changes):
git config user.name <good name>
git config user.email <good email>
git reset HEAD^
git stash
git reset HEAD^
git commit -a
git stash pop
git commit -a
Using interactive rebase, you can place an amend command after each commit you want to alter. For instance:
pick a07cb86 Project tile template with full details and styling
x git commit --amend --reset-author -Chead
I want to add my Example too. I want to create a bash_function with given parameter.
this works in mint-linux-17.3
# $1 => email to change, $2 => new_name, $3 => new E-Mail
function git_change_user_config_for_commit {
# defaults
WRONG_EMAIL=${1:-"you_wrong_mail@hello.world"}
NEW_NAME=${2:-"your name"}
NEW_EMAIL=${3:-"new_mail@hello.world"}
git filter-branch -f --env-filter "
if [ \$GIT_COMMITTER_EMAIL = '$WRONG_EMAIL' ]; then
export GIT_COMMITTER_NAME='$NEW_NAME'
export GIT_COMMITTER_EMAIL='$NEW_EMAIL'
fi
if [ \$GIT_AUTHOR_EMAIL = '$WRONG_EMAIL' ]; then
export GIT_AUTHOR_NAME='$NEW_NAME'
export GIT_AUTHOR_EMAIL='$NEW_EMAIL'
fi
" --tag-name-filter cat -- --branches --tags;
}
I should point out that if the only problem is that the author/email is different from your usual, this is not a problem. The correct fix is to create a file called .mailmap
at the base of the directory with lines like
Name you want <email you want> Name you don't want <email you don't want>
And from then on, commands like git shortlog
will consider those two names to be the same (unless you specifically tell them not to). See http://schacon.github.com/git/git-shortlog.html for more information.
This has the advantage of all the other solutions here in that you don't have to rewrite history, which can cause problems if you have an upstream, and is always a good way to accidentally lose data.
Of course, if you committed something as yourself and it should really be someone else, and you don't mind rewriting history at this point, changing the commit author is probably a good idea for attribution purposes (in which case I direct you to my other answer here).
run git rebase -i <sha1 or ref of starting point>
mark all commits that you want to change with edit
(or e
)
loop the following two commands until you have processed all the commits:
git commit --amend --reuse-message=HEAD --author="New Author <new@author.email>"
;
git rebase --continue
This will keep all the other commit information (including the dates).
The --reuse-message=HEAD
option prevents the message editor from launching.