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
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@address.com>"
$ 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@address.com>"
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
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 "correct@new.email"
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.
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
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).
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 amend them one by one and finish.
Start rebasing with git rebase -i
. It will show you something like this.
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.
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 "newuser@gmail.com"
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 "newuser@gmail.com"
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