Reputation: 44306
How do I change the author for a range of commits?
Upvotes: 3168
Views: 1251735
Reputation: 1754
git-extras has some nice utilities, the one you could use is git-reauthor
/ git requthor
.
$ git reauthor \
--old-email [email protected] \
--correct-email [email protected] \
--correct-name 'Jack Foobar'
# Debian based
sudo apt install git-extras
# Fedora
sudo dnf install git-extras
Upvotes: 2
Reputation: 561
Proceed carefully.
Most used command is using filter but that will create issue sometimes when you are working on very important project. You can't take risk. Here is the 0 risk solution using git interactive editor.
Run below commands :-
git rebase -i HEAD~n
Here n is the number of commits that you want to edit. 1 for only last commit.
After running the command interactive rebase editor will appears, change the word pick next to each commit you want to edit to word edit.
your first line will look like
edit <commitId> <commit message>
After entering this you need to save so press ESC and then type :wq
and run it.
Now you are in the editor for selected commit, so run below command
git commit --amend --author="New Name <[email protected]>"
after amending the commit, if you want to continue to change other commit type below command and run
git rebase --continue
else force push this commit using below command.
git push --force origin HEAD:<branch name>
This will do your work. Process will create new commit with new commitId, last one will be edited to the newer one.
Upvotes: 1
Reputation: 69
It seems that no one have talked about the date in commit. Here is a shell script to keep the date in each commit:
#!/bin/sh
# git_rebase_reset_author.sh
# shellcheck disable=SC2016
git rebase "$@" \
--exec 'author_date="$(git log -1 HEAD --pretty=format:"%ad")" && git commit --amend --no-edit --reset-author --date="$author_date"'
You can add any parameters that supported by rebase
. For example:
# rebase since <sha>:
./git_rebase_reset_author.sh <sha>
# or start from the first commit:
./git_rebase_reset_author.sh --root
Here is the explanation to the scripts:
From man page of git-rebase(1)
:
OPTIONS
-x <cmd>, --exec <cmd>
Append "exec <cmd>" after each line creating a commit in the final history.
So, we can use it exec command automatically, instead of doing git rebase --continue
for each commit. For the command in --exec
, let's take apart:
author_date="$(git log -1 HEAD --pretty=format:"%ad")" && \
git commit --amend --no-edit --reset-author --date="$author_date"
We first get the author date by git log -1 HEAD --pretty=format:"%ad"
, and save it to variable of author_data
.
From man page of git-log(1)
, we can known the meaning of the parameters:
OPTIONS
Commit Limiting
-<number>, -n <number>, --max-count=<number>
Limit the number of commits to output.
Commit Formatting
--pretty[=<format>], --format=<format>
PRETTY FORMATS
• format:<format-string>
• Placeholders that expand to information extracted from the commit:
%ad
author date (format respects --date= option)
Then we do git commit --amend
to reset the author and change the date to above date.
Here is the man page of git-commit(1)
:
OPTIONS
--amend
Replace the tip of the current branch by creating a new commit.
--reset-author
When used with -C/-c/--amend options, or when committing after a conflicting cherry-pick, declare that the authorship of the resulting commit now belongs to the committer. This also renews the author timestamp.
--date=<date>
Override the author date used in the commit.
The shellcheck disable=SC2016
is:
Expressions don't expand in single quotes, use double quotes for that.
As we want to expand the expression ie get the author date at each commit, instead of at the time before doing rebase
. So we need to ignore the warning.
Upvotes: 0
Reputation: 10548
I use the following to rewrite the author for an entire repository, including tags and all branches:
git filter-branch --tag-name-filter cat --env-filter "
export GIT_AUTHOR_NAME='New name';
export GIT_AUTHOR_EMAIL='New email'
" -- --all
Then, as described in the MAN page of filter-branch, remove all original refs backed up by filter-branch
(this is destructive, backup first):
git for-each-ref --format="%(refname)" refs/original/ | \
xargs -n 1 git update-ref -d
Upvotes: 29
Reputation: 608
When committing to Git with different credentials, changing only the author's name and email won't affect the committer details. To change both the author and committer, set the following environment variables:
GIT_COMMITTER_NAME="YOUR_NAME"
GIT_COMMITTER_EMAIL="YOUR_EMAIL"
You can modify the committer name, author name, and email with the following git rebase command:
git rebase <BASE_COMMIT>~1 <END_COMMIT>
pick <commit-id> <commit-message>
exec GIT_COMMITTER_NAME="YOUR_NAME" GIT_COMMITTER_EMAIL="YOUR_EMAIL" git commit --amend --author="YOUR_NAME <YOUR_EMAIL>"
This command allows you to update both the committer and author details for a specific commit.
Upvotes: 0
Reputation: 91610
NOTE: This answer changes SHA1s, so take care when using it on a branch that has already been pushed. If you only want to fix the spelling of a name or update an old email, Git lets you do this without rewriting history using .mailmap
. See my other answer.
First, if you haven't already done so, you will likely want to fix your name in git-config:
git config --global user.name "New Author Name"
git config --global user.email "<[email protected]>"
This is optional, but it will also make sure to reset the committer name, too, assuming that's what you need.
To rewrite metadata for a range of commits using a rebase, do
git rebase -r <some commit before all of your bad commits> \
--exec 'git commit --amend --no-edit --reset-author'
--exec
will run the git commit
step after each commit is rewritten (as if you ran git commit && git rebase --continue
repeatedly).
If you also want to change your first commit (also called the 'root' commit), you will have to add --root
to the rebase call.
This will change both the committer and the author to your user.name
/user.email
configuration. If you did not want to change that config, you can use --author "New Author Name <[email protected]>"
instead of --reset-author
. Note that doing so will not update the committer -- just the author.
If you just want to change the most recent commit, a rebase is not necessary. Just amend the commit:
git commit --amend --no-edit --reset-author
git rebase -r --root --exec "git commit --amend --no-edit --reset-author"
-r,--rebase-merges
may not exist for you. As a replacement, you can use -p
. Note that -p
has serious issues and is now deprecated.
Upvotes: 2377
Reputation: 59
So, I had to edit email address for many past commits. Because I want to put the email of my main GitHub account so that it will show-up in the constribution graph, all within one account.
First make sure your remote is in sink with the local. Push and pull to make this possible. Then use rebase
git rebase -r --root --exec "git commit --amend --no-edit --reset-author"
You can check in your 'git log' that email got set appropriately. Now you need to push the updated history to the remote. Use:
git push -f
That '-f' is important otherwise you will get:
To github.com:UserName/RepoName.git
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'github.com:UserName/RepoName.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Upvotes: 3
Reputation: 1529
This is a more elaborated version of @Brian's version:
To change the author and committer, you can do this (with linebreaks in the string which is possible in bash):
git filter-branch --env-filter '
if [ "$GIT_COMMITTER_NAME" = "<Old name>" ];
then
GIT_COMMITTER_NAME="<New name>";
GIT_COMMITTER_EMAIL="<New email>";
GIT_AUTHOR_NAME="<New name>";
GIT_AUTHOR_EMAIL="<New email>";
fi' -- --all
You might get one of these errors:
If you want to force the run in spite of these errors, add the --force
flag:
git filter-branch --force --env-filter '
if [ "$GIT_COMMITTER_NAME" = "<Old name>" ];
then
GIT_COMMITTER_NAME="<New name>";
GIT_COMMITTER_EMAIL="<New email>";
GIT_AUTHOR_NAME="<New name>";
GIT_AUTHOR_EMAIL="<New email>";
fi' -- --all
A little explanation of the -- --all
option might be needed: It makes the filter-branch work on all revisions on all refs (which includes all branches). This means, for example, that tags are also rewritten and is visible on the rewritten branches.
A common "mistake" is to use HEAD
instead, which means filtering all revisions on just the current branch. And then no tags (or other refs) would exist in the rewritten branch.
Upvotes: 52
Reputation: 12852
filter-repo
:You can use the callbacks feature of git-filter-repo
(a recommended replacement for filter-branch
) to change the name and email associated with all the commits:
git filter-repo --name-callback 'return b"New Name"' --email-callback 'return b"[email protected]"'
This is more performant and potentially more reliable than solutions using filter-branch
.
Note that the above command changes the authors (and committer) of all commits, if you want to effectively "edit" a certain author, and only modify the commits of that specific author, then use the --commit-callback
option like this:
git filter-repo --commit-callback '
old_email = b"[email protected]"
new_email = b"[email protected]"
new_name = b"New Author"
if commit.author_email == old_email:
commit.author_email = new_email
commit.author_name = new_name
if commit.committer_email == old_email:
commit.committer_email = new_email
commit.committer_name = new_name
'
(Just change the old_email
, new_email
, and new_name
variables in the command above to the right values.)
Upvotes: 13
Reputation: 37802
A single command to change the author for the last N commits:
git rebase -i HEAD~N -x "git commit --amend --author 'Author Name <[email protected]>' --no-edit"
NOTES
HEAD~N
with the reference until where you want to rewrite your commits. This can be a hash, HEAD~4
, a branch name, ...--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 <[email protected]>' --no-edit
pick abb60f9 add new feature
exec git commit --amend --author 'Author Name <[email protected]>' --no-edit
pick dc18f70 bugfix
exec git commit --amend --author 'Author Name <[email protected]>' --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.
Note that if you already fixed the author information with git config user.name <your_name>
and git config user.email <your_email>
, you can also use this command:
git rebase -i HEAD~N -x "git commit --amend --reset-author --no-edit"
Upvotes: 103
Reputation: 49216
GitHub originally had a nice solution (broken link), which was the following shell script:
#!/bin/sh
git filter-branch --env-filter '
an="$GIT_AUTHOR_NAME"
am="$GIT_AUTHOR_EMAIL"
cn="$GIT_COMMITTER_NAME"
cm="$GIT_COMMITTER_EMAIL"
if [ "$GIT_COMMITTER_EMAIL" = "[email protected]" ]
then
cn="Your New Committer Name"
cm="Your New Committer Email"
fi
if [ "$GIT_AUTHOR_EMAIL" = "[email protected]" ]
then
an="Your New Author Name"
am="Your New Author Email"
fi
export GIT_AUTHOR_NAME="$an"
export GIT_AUTHOR_EMAIL="$am"
export GIT_COMMITTER_NAME="$cn"
export GIT_COMMITTER_EMAIL="$cm"
'
Upvotes: 125
Reputation: 27878
For a single commit:
git commit --amend --author="Author Name <[email protected]>"
(extracted from asmeurer's answer)
Upvotes: 196
Reputation: 42912
In the case where just the top few commits have bad authors, you can do this all inside git rebase -i
using the exec
command and the --amend
commit, as follows:
git rebase -i HEAD~6 # as required
which presents you with the editable list of commits:
pick abcd Someone else's commit
pick defg my bad commit 1
pick 1234 my bad commit 2
Then add exec ... --author="..."
lines after all lines with bad authors:
pick abcd Someone else's commit
pick defg my bad commit 1
exec git commit --amend --author="New Author Name <[email protected]>" -C HEAD
pick 1234 my bad commit 2
exec git commit --amend --author="New Author Name <[email protected]>" -C HEAD
save and exit editor (to run).
This solution may be longer to type than some others, but it's highly controllable - I know exactly what commits it hits.
Thanks to @asmeurer for the inspiration.
Upvotes: 211
Reputation: 6533
It happens when you do not have a $HOME/.gitconfig
initialized. You may fix this as:
git config --global user.name "you name"
git config --global user.email [email protected]
git commit --amend --reset-author
Tested with Git version 1.7.5.4.
Note that this fixes only the last commit.
Upvotes: 254
Reputation: 91610
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 https://schacon.github.io/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).
Upvotes: 72
Reputation: 214426
This answer uses
git-filter-branch
, for which the docs now give this warning:git filter-branch has a plethora of pitfalls that can produce non-obvious manglings of the intended history rewrite (and can leave you with little time to investigate such problems since it has such abysmal performance). These safety and performance issues cannot be backward compatibly fixed and as such, its use is not recommended. Please use an alternative history filtering tool such as git filter-repo. If you still need to use git filter-branch, please carefully read SAFETY (and PERFORMANCE) to learn about the land mines of filter-branch, and then vigilantly avoid as many of the hazards listed there as reasonably possible.
Changing the author (or committer) would require rewriting all of the history. If you're okay with that and think it's worth it then you should check out git filter-branch. The manual page includes several examples to get you started. Also note that you can use environment variables to change the name of the author, committer, dates, etc. -- see the "Environment Variables" section of the git manual page.
Specifically, you can fix all the wrong author names and emails for all branches and tags with this command (source: GitHub help):
#!/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
For using alternative history filtering tool git filter-repo, you can first install it and construct a git-mailmap
according to the format of gitmailmap.
Proper Name <[email protected]> Commit Name <[email protected]>
And then run filter-repo with the created mailmap:
git filter-repo --mailmap git-mailmap
Upvotes: 1581
Reputation: 3995
For reset ALL commits (including first commit) to current user and current timestamp:
git rebase --root --exec "git commit --amend --no-edit --date 'now' --reset-author"
Upvotes: 8
Reputation: 6752
A safer alternative to git's filter-branch
is filter-repo
tool as suggested by git docs here.
git filter-repo --commit-callback '
old_email = b"[email protected]"
correct_name = b"Your Correct Name"
correct_email = b"[email protected]"
if commit.committer_email == old_email :
commit.committer_name = correct_name
commit.committer_email = correct_email
if commit.author_email == old_email :
commit.author_name = correct_name
commit.author_email = correct_email
'
The above command mirrors the logic used in this script but uses filter-repo
instead of filter-branch
.
The code body after commit-callback
option is basically python code used for processing commits. You can write your own logic in python here. See more about commit
object and its attributes here.
Since filter-repo
tool is not bundled with git you need to install it separately.
See Prerequisties and Installation Guide
If you have a python env >= 3.5, you can use pip
to install it.
pip3 install git-filter-repo
Note: It is strongly recommended to try filter-repo
tool on a fresh clone. Also remotes are removed once the operation is done. Read more on why remotes are removed here. Also read the limitations of this tool under INTERNALS section.
Upvotes: 53
Reputation: 8408
I have tried the scripts above it did not work for me, this fixed my issue:
use Git's "filter-branch" command. It allows you to batch-process a (potentially large) number of commits with a script. You can run the below sample script in your repository (filling in real values for the old and new email and name):
git filter-branch --env-filter '
WRONG_EMAIL="[email protected]"
NEW_NAME="New Name Value"
NEW_EMAIL="[email protected]"
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
See more details here
Upvotes: 1
Reputation: 55
For all the commits, my solution:
git rebase -i --root -x "git commit --amend --author 'bedorlan <[email protected]>' --no-edit"
Upvotes: 3
Reputation: 91
If you want to (easily) change the author for the current branch I would use something like this:
# update author for everything since origin/master
git rebase \
-i origin/master \
--exec 'git commit --amend --no-edit --author="Author Name <[email protected]>"'
Upvotes: 4
Reputation: 25210
I would like to contribute with a modification of @Rognon answer. This answer is just another alternative in case the selected answer or others don't work for you (in my particular issue that was the case):
Objective: You will fix one or more authors with a correct one in the ALL the history, and you will get a clean history without duplicates. This method works by replacing 'master' branch with a 'clean' branch (its not using merge/rebase)
NOTE: Anyone using the "master" repository may need to checkout it again (after performing these steps) before pushing, as merge may fail.
We will use a new branch named "clean" to perform the operations (assuming you want to fix "master"):
git checkout -b clean
(be sure you are in the "clean" branch: git branch
)
Modify the following script (replacing the email addresses and names). Note that this script expects two wrong emails/authors (as example), so if you only need to fix a single author, you can remove the second part of the condition or leave it like that (as it will be ignored as it won't match).
Execute the script.
#/bin/bash
git filter-branch --force --commit-filter '
if [ "$GIT_COMMITTER_EMAIL" = "[email protected]" -o "$GIT_COMMITTER_EMAIL" = "[email protected]" ];
then
export GIT_COMMITTER_NAME="John Doe";
export GIT_AUTHOR_NAME="John Doe";
export GIT_COMMITTER_EMAIL="[email protected]";
export GIT_AUTHOR_EMAIL="[email protected]";
fi;
git commit-tree "$@"
' --tag-name-filter cat -- --all
It has to report: Ref 'refs/heads/clean' was rewritten
. If it reports "unchanged", maybe the email(s) entered in the script is wrong.
Confirm the history has been corrected with: git log
git push --set-upstream origin clean
git branch -d master
git branch -m clean master
git push --force origin master
Upvotes: 2
Reputation: 31217
For those under windows, you could also use the git-rocket-filter tool.
From the documentation:
Change commit author name and email:
git-rocket-filter --branch TestBranch --commit-filter '
if (commit.AuthorName.Contains("Jim")) {
commit.AuthorName = "Paul";
commit.AuthorEmail = "[email protected]";
}
Upvotes: 2
Reputation: 580
All the answers above rewrite the history of the repository.
As long as the name to change has not been used by multiple authors and especially if the repository has been shared and the commit is old I'd prefer to use .mailmap
, documented at https://git-scm.com/docs/git-shortlog.
It allows mapping incorrect names/emails to the correct one without modifying the repo history. You can use lines like:
Proper Name <[email protected]> <root@localhost>
Upvotes: 2
Reputation: 6333
You can use this as a alias so you can do:
git change-commits GIT_AUTHOR_NAME "old name" "new name"
or for the last 10 commits:
git change-commits GIT_AUTHOR_EMAIL "[email protected]" "[email protected]" HEAD~10..HEAD
Add to ~/.gitconfig:
[alias]
change-commits = "!f() { VAR=$1; OLD=$2; NEW=$3; shift 3; git filter-branch --env-filter \"if [[ \\\"$`echo $VAR`\\\" = '$OLD' ]]; then export $VAR='$NEW'; fi\" $@; }; f "
Source: https://github.com/brauliobo/gitconfig/blob/master/configs/.gitconfig
Hope it is useful.
Upvotes: 56
Reputation: 64923
The fastest, easiest way to do this is to use the --exec argument of git rebase:
git rebase -i -p --exec 'git commit --amend --reset-author --no-edit'
This will create a todo-list that looks like this:
pick ef11092 Blah blah blah
exec git commit --amend --reset-author --no-edit
pick 52d6391 Blah bloh bloo
exec git commit --amend --reset-author --no-edit
pick 30ebbfe Blah bluh bleh
exec git commit --amend --reset-author --no-edit
...
and this will work all automatically, which works when you have hundreds of commits.
Upvotes: 19
Reputation: 89
Try this out. It will do the same as above mentioned, but interactively.
bash <(curl -s https://raw.githubusercontent.com/majdarbash/git-author-change-script/master/run.sh)
Reference: https://github.com/majdarbash/git-author-change-script
Upvotes: 3
Reputation: 563
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:-"[email protected]"}
NEW_NAME=${2:-"your name"}
NEW_EMAIL=${3:-"[email protected]"}
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;
}
Upvotes: 4
Reputation: 24194
Change commit author name & email
by Amend
, then replacing old-commit with new-one
:
$ git checkout <commit-hash> # checkout to the commit need to modify
$ git commit --amend --author "name <[email protected]>" # change the author name and email
$ git replace <old-commit-hash> <new-commit-hash> # replace the old commit by new one
$ git filter-branch -- --all # rewrite all futures commits based on the replacement
$ git replace -d <old-commit-hash> # remove the replacement for cleanliness
$ git push -f origin HEAD # force push
Another way Rebasing
:
$ git rebase -i <good-commit-hash> # back to last good commit
# Editor would open, replace 'pick' with 'edit' before the commit want to change author
$ git commit --amend --author="author name <[email protected]>" # change the author name & email
# Save changes and exit the editor
$ git rebase --continue # finish the rebase
Upvotes: 19
Reputation: 702
This isn't an answer to your question, but rather a script you can use to avoid this in the future. It utilizes global hooks available since Git version 2.9 to check your email configuration based on the directory your in:
#!/bin/sh
PWD=`pwd`
if [[ $PWD == *"Ippon"* ]] # 1)
then
EMAIL=$(git config user.email)
if [[ $EMAIL == *"Work"* ]] # 2)
then
echo "";
else
echo "Email not configured to your Work email in the Work directory.";
git config user.email "[email protected]"
echo "Git email configuration has now been changed to \"$(git config user$
echo "\nPlease run your command again..."
echo ''
exit 1
fi;
elif [[ $PWD == *"Personal"* ]]
then
EMAIL=$(git config user.email)
if [[ $EMAIL == "[email protected]" ]]
then
echo "";
else
echo "Email is not configured to your personal account in the Personal di$
git config user.email "[email protected]"
echo "Git email configuration has now been changed to \"$(git config user$
echo "\nPlease run your command again..."
echo ''
exit 1;
fi;
fi;
It checks your current working directory, then verifies your git is configured to the correct email. If not, it changes it automatically. See the full details here.
Upvotes: -1