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?
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?
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 protected]>" --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 protected]>"
git rebase --continue
D
git commit --amend --author="Author Name <[email protected]>"
againgit rebase --continue
git push -f
to update your origin with the updated commits.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.
git commit --amend --reset-author
--no-edit
option. git commit --amend --reset-author --no-edit
won't open an editor. Available since git 1.7.9.
git rebase -i --root
Commented
May 4, 2016 at 20:07
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.
Checkout the commit we are trying to modify:
git checkout 03f482d6
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
.
Checkout the original branch.
Replace the old commit with the new one locally:
git replace 03f482d6 42627abe
Rewrite all future commits based on the replacement:
git filter-branch -- --all
Remove the replacement for cleanliness:
git replace -d 03f482d6
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
git rebase -i
. Never heard of this git replace
thing before. +1
Commented
Jul 8, 2015 at 23:29
--force-with-lease
instead of -f
. It's safer.
Commented
Jul 3, 2017 at 17:14
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.
If you just want to change the author of your last commit, you can do this:
Reset author for the current repo:
git config --local user.name "Alex Smith"
git config --local user.email [email protected]
Now reset the author of your commit without edit required:
git commit --amend --reset-author --no-edit
Force push your changes without overwriting anyone else's commits:
git push --force-with-lease
Note that this will also change the author timestamp.
To do this for the last N commmits:
git rebase --onto HEAD~N --exec "git commit --amend --reset-author --no-edit" HEAD~N
It's not last commit.
So how would they amend
it?
Commented
Aug 31, 2017 at 13:25
git reset HEAD~
, ran your suggested lines, then did the next commit manually again. Worked fine!
Commented
Sep 18, 2017 at 10:56
git config --local user.name FirstName LastName
and git config --local user.email [email protected]
. 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
.
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="[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
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'
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.
Commented
Jan 18, 2018 at 17:33
git push -f
to force push changes to the repo.
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.
Commented
Jan 15, 2021 at 9:11
You can change author of last commit using the command below.
git commit --amend --author="Author Name <[email protected]>"
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.
Start rebasing with git rebase -i
.
Change the pick
keyword to edit
for the commits you want to change the author name.
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.
pick
action, and add after each line exec git commit --no-edit --amend --author="MyNewAuthor <[email protected]>"
Commented
Feb 5, 2020 at 9:05
rebase -i -p
), but at the end this is what I needed. Thanks
git config --global core.editor nano
before running git commit
and then you can use Ctrl+O, Ctrl+X to exit the editor.
Commented
May 11, 2022 at 4:48
First set the correct author for current Git repo
git config --local user.name "FirstName LastName"
git config --local user.email [email protected]
Then apply the fix to the last six commits
git rebase --onto HEAD~6 --exec "git commit --amend --reset-author --no-edit" HEAD~6
Finally force push to the remote Git repo
git push --force-with-lease
"FirstName LastName"
, just a heads up
Commented
Nov 22, 2022 at 1:27
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
Find a way that can change user quickly and has no side effect to others commits.
git config user.name "New User"
git config user.email "[email protected]"
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
git log
git config user.name "New User"
git config user.email "[email protected]"
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
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
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 --force-with-lease
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.
Commit before:
To fix author for all commits you can apply command from @Amber's answer:
git commit --amend --author="Author Name <[email protected]>"
Or to reuse your name and email you can just write:
git commit --amend --author=Eugen
Commit after the command:
For example to change all starting from 4025621
:
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 <[email protected]>\"" 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
git shortlog -e -s
.
Commented
Nov 1, 2018 at 16:36
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"
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.
A-B-C-D-E-F
,B
and D
(= 2 commits),then you can do:
git config user.name "Correct new name"
git config user.email "[email protected]"
git commit --allow-empty -m "empty"
git rebase -i B^
B^
selects the parent of B
.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.
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.
Try this :
git commit --amend --author="John Doe <[email protected]>" --no-edit
I believe simply this can help no need for extra stuff!
Thanks
SOLUTION
Install git filter-repo
(Git project recommends filter-repo
over filter-branch
)
$ PACKAGE_TOOL install git-filter-repo
Create a file .mailmap
in the root of the git repository containing
New Name <[email protected]> <[email protected]>
Run git filter-repo --use-mailmap
MORE DETAILS
git-filter-repo
lists this as an example in their docs.mailmap
by default, you can specify mailmap file by invoking git filter-repo
with argument --mailmap <filename>
.git-filter-repo
project's README.md.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 <[email protected]> <[email protected]> New Name2 <[email protected]> <[email protected]>
Commented
Dec 21, 2022 at 16:16
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.
git push --force-with-lease
Commented
Jun 27 at 9:09
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 protected]>"
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 protected]>" --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 protected]>"
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 [email protected] commit --amend --reset-author
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).
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 protected]>"
Why not make your life simpler by following this article by Atlassian?
git commit --amend --author="Author Name <[email protected]>"
master
; therefore, it'll be protected by the source code repositorygit push origin master --force
That's the simplest scenario for the last commit. For picking up any "random" commit, you need:
git rebase -i <Earlier Commit>.
pick
on edit
on that commit, in which you're interested ingit commit --amend --author="Author Name <[email protected]>"
master
; therefore, it'll be protected by the source code repositorygit push origin master --force
You can always git log
in between to be sure where you are before you push.
Optional: Make sure to stash your local changes if you don't want to send them to remote.
git status
git stash
git log // Old author in local and remote
git commit --amend --author="Author Name <[email protected]>"
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 protected]>"
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
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 <[email protected]>'
pick abb60f9 add new feature
exec git commit --amend --author 'Author Name <[email protected]>'
pick dc18f70 bugfix
exec git commit --amend --author 'Author Name <[email protected]>'
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
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
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:
git rebase -i <earliercommit>
)exec git commit --amend --author="Author Name <[email protected]>" --no-edit
(or using --reset-author
if you want to reset to the value set in the git config)Example editor content (to change first 2 commits author):
pick 1fc6c95 Patch A
exec git commit --amend --author="Author Name <[email protected]>" --no-edit
pick 6b2481b Patch B
exec git commit --amend --author="Author Name <[email protected]>" --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.
#
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="[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
here u can change the old email to ur new user name and email address.
One important thing to mention especially if you are deleting author information for privacy.
After operations from the previous answers:
git commit --amend --author="Author Name <[email protected]>"
git push -f
The commits with old author information still exist in the Git local and remote cache.
As GitHub, you can still access the commit by SHA-1, 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 the local repository:
git gc
However, for GitHub, which you cannot trigger this command, go to Support, How can we help with removing data from a repository you own?, and submit a Clear Cached Views ticket on your repository.
This last step is applied to all commit modify/remove operations.
Ref: 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.
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.
Changing Your Committer Name & Email Globally:
$ git config --global user.name "John Doe"
$ git config --global user.email "[email protected]"
Changing Your Committer Name & Email per Repository:
$ git config user.name "John Doe"
$ git config user.email "[email protected]"
Changing the Author Information Just for the Next Commit:
$ git commit --author="John Doe <[email protected]>"
Hint: For other situation and read more information read the post reference.
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
).
For those curious, I manage to encapsulate the essential logic of the leading answer using git replace
into a helper function compatible with bash
, zsh
, and other shells.
I created a gist where you can view it as well.
The below function cca
(shorthand for change-commit-author) makes some assumptions:
git
.[!WARNING] Do not use in the
main
branch of shared or published projects without confirming with other collaborators beforehand.The reason is this script performs some "destructive" actions:
- It rewrites all commit history, so if someone is working on a dev branch, they will have to rebase or else merge the changes of your branch into theirs.
- If other collaborators and authors use commit signature verification as outlined above, their commits will likely say "Verified". After running this script, all commits by other authors will say "Unverified" -- since the script effectively "rewrites" commit history -- and they will need to run a script to batch re-sign all their previous commits, which will likely rewrite commit history again when changes are pushed.
# Bash/Zsh Function to Change Author for Commit(s)
#
# Adapted from:
#
# - https://stackoverflow.com/a/28845565/10237506
# - https://docs.github.com/en/enterprise/2.18/user/github/using-git/changing-author-info
#
# Directions:
#
# Enter either an *Email* to rewrite author of all commits by that email,
# or a space-separated list of *Git Commit SHA(s)* to change the author of.
#
# Examples:
# cca [email protected]
# cca sha-1 sha-2
#
# To find Commit SHAs for a Git User with email `[email protected]`, use:
#
# $ git --no-pager log --format=format:%H --author='[email protected]'
#
# Alternatively, navigate to below link (substituting org, repo,
# and Git author) and then *Click "Copy"*
#
# https://github.com/ORG/REPO/commits?author=AUTHOR
#
function cca() {
NUM_ARGS=$#
if [ $NUM_ARGS -eq 0 ]; then
echo "Usage: $0 <AUTHOR_EMAIL_OR_COMMIT_SHA_LIST...>"
echo
echo "Bash/Zsh Function to Change Author for Commit(s)"
echo
echo Function requires one or more arguments.
echo
echo Enter either an email to rewrite author of all commits by that email,
echo "or a space-separated list of Git Commit SHA(s) to change the author of."
echo
echo Examples:
echo " $ $0 [email protected]"
echo " $ $0 abc123 xyz321"
echo
echo To find all Commit SHAs for a Git User with email \`[email protected]\`, use:
echo " $ git --no-pager log --format=format:%H --author='[email protected]'"
return 1
fi
# Current Branch
GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
GIT_NAME=$(git config --get user.name)
GIT_EMAIL=$(git config --get user.email)
echo "Branch: ${GIT_BRANCH}"
echo "Name: ${GIT_NAME}"
echo "Email: ${GIT_EMAIL}"
# Fix for error message from `git filter-branch`:
# Cannot create a new backup.
# A previous backup already exists in refs/original/
# Force overwriting the backup with -f
#
# Ref: https://stackoverflow.com/a/7654880/10237506
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
# Check for ONE argument, and that the argument is an EMAIL
# Partial Credits: https://stackoverflow.com/a/4501833/10237506
if [[ $NUM_ARGS -eq 1 && "$1" =~ .+@.+ ]]; then
GIT_OLD_EMAIL="$1"
echo "Old Email: ${GIT_OLD_EMAIL}"
echo '[ Replacing Commit Author - Old Email > Email ]'
# To change the name and/or email address recorded in existing commits,
# you must rewrite the entire history of your Git repository.
#
# Credits: https://docs.github.com/en/enterprise/2.18/user/github/using-git/changing-author-info
FILTER_BRANCH_SQUELCH_WARNING=1 \
GIT_OLD_EMAIL="$GIT_OLD_EMAIL" \
GIT_NAME="$GIT_NAME" \
GIT_EMAIL="$GIT_EMAIL" \
git filter-branch --env-filter '
if [ "$GIT_COMMITTER_EMAIL" = "$GIT_OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$GIT_NAME"
export GIT_COMMITTER_EMAIL="$GIT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$GIT_OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$GIT_NAME"
export GIT_AUTHOR_EMAIL="$GIT_EMAIL"
fi
' \
--commit-filter '
if [[ "$GIT_COMMITTER_EMAIL" = "$GIT_NAME" || "$GIT_AUTHOR_EMAIL" = "$GIT_EMAIL" ]]
then
git commit-tree -S "$@";
fi
' \
--tag-name-filter cat \
-- --branches --tags
else
# Checkout the commit we are trying to modify
#
# The `-c advice.detachedHead=false` parameter will allow you to suppress the warning.
# Ref: https://stackoverflow.com/a/45652159/10237506
for GIT_COMMIT_SHA in "$@"; do
git -c advice.detachedHead=false checkout "${GIT_COMMIT_SHA}"
# Make the author change (use `--no-edit` to skip prompt to update commit message)
#
# Update to use `--reset-author` as mentioned in below, so we can also
# set Git committer instead of just the author. Also, use existing committed date.
#
# Refs:
# - https://stackoverflow.com/a/74856838/10237506
# - https://stackoverflow.com/a/61217637/10237506
git \
-c user.name="${GIT_NAME}" \
-c user.email="${GIT_EMAIL}" \
commit -S \
--amend \
--reset-author \
--no-edit \
--date="$(git --no-pager log -1 --format='%aD')"
GIT_NEW_COMMIT_SHA=$(git --no-pager log -1 --format="%H")
# Checkout the original branch
git checkout "${GIT_BRANCH}"
echo "Commit SHA: ${GIT_COMMIT_SHA}"
echo "Commit SHA (NEW): ${GIT_NEW_COMMIT_SHA}"
# Replace the old commit with the new one locally
git replace "${GIT_COMMIT_SHA}" "${GIT_NEW_COMMIT_SHA}"
done
# Rewrite all commits based on the replacement
#
# Also, sign the commits authored by this user if possible,
# and correct committer date for rewritten commit above.
#
# Refs:
# - https://stackoverflow.com/a/41883164/10237506
# - https://stackoverflow.com/a/24820045/10237506
FILTER_BRANCH_SQUELCH_WARNING=1 \
GIT_NAME=$GIT_NAME \
GIT_EMAIL=$GIT_EMAIL \
git filter-branch \
--env-filter \
'export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"' \
--commit-filter '
if [[ "$GIT_COMMITTER_EMAIL" = "$GIT_NAME" || "$GIT_AUTHOR_EMAIL" = "$GIT_EMAIL" ]]
then
git commit-tree -S "$@";
else
git commit-tree "$@";
fi
' \
-- --all
# Remove the replacement for cleanliness
for GIT_COMMIT_SHA in "$@"; do
git replace -d "${GIT_COMMIT_SHA}"
done
fi
# Seems to be needed for `--force-with-lease` to work!
git fetch
# Push the new history
if ! git push --force-with-lease; then
# Only use `--force` instead of `--force-with-lease`
# if the latter fails, and only after sanity checking
# with git log and/or git diff.
echo 'Git Push with `--force-with-lease` failed.'
# Ref: https://stackoverflow.com/a/14741036/10237506
if git diff --exit-code && [[ -z $(git status -uno --porcelain) ]]; then
echo Git Status/Diff - OK
N=5
echo [ Git Log - Showing Top ${N} Commits ]
git --no-pager log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit -n ${N}
echo
# Confirm w/ user to use `--force` for `git push`
printf '%s' 'Proceed with `git push --force`? [y/N] '
# Read Character, compatible with `zsh` and `bash`
# https://stackoverflow.com/a/30022297/10237506
read_char() {
stty -icanon -echo
eval "$1=\$(dd bs=1 count=1 2>/dev/null)"
stty icanon echo
}
read_char REPLY
echo "$REPLY" # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]; then
git push -f
echo 'Git Push with `--force` successful!'
fi
else
echo Git Status/Diff - ERROR, aborting.
return 1
fi
else
echo 'Git Push with `--force-with-lease` successful!'
fi
}
Steps:
Open ~/.zshrc
(or ~/.bashrc
for Bash), and paste in the function definition above.
Use source ~/.zshrc
to reload changes into current shell session.
Enter either an Email to rewrite author of all commits by that email, or a space-separated list of Git Commit SHA(s) to change the author of.
Examples:
cca [email protected]
cca sha-1 sha-2
To find Commit SHAs for a Git User with email [email protected]
, use:
$ git --no-pager log --format=format:%H --author='[email protected]'
Alternatively, navigate to below link (substituting org, repo, and Git author) and then Click "Copy"
https://github.com/ORG/REPO/commits?author=AUTHOR
N.B. If
git push --force-with-lease
fails, the function automatically checks status ofgit diff
andgit status
to confirm everything is OK.It then prints the most recent commits from
git log
, then ultimately prompts user to continue withgit push --force
-- which even the linked answer notes, should be used with caution here.In most of my cases, without
git fetch
it would fallback togit push -f
, as using--force-with-lease
never worked otherwise ):
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
If you want to change the author for all commits that were committed by a certain author all at once:
Run the following command to iterate through all commits and update the author information according to the provided conditions:
git filter-branch --env-filter '
if [ "$GIT_AUTHOR_EMAIL" = "Older Author Email" ]; then
export GIT_AUTHOR_NAME="New Author Name"
export GIT_AUTHOR_EMAIL="New Author Email""
fi
' -- --all
Then forcefully push the branch.
git push --force origin branch-name
user
, so what I needed wasgit commit --amend --reset-author
filter-repo
solution, as it does everything for you using a Git recommended tool.git commit config --global user.name "your name"
andgit commit config --global user.email "your email"
, and then I didgit commit --amend --reset-author