Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update git commit author, but keep original date when amending?

Tags:

git

If the commits are already made and pushed to the repository, and if want to change an author for some particular commit I can do it like this:

git commit --amend --reset-author

But, that will change the original committed date.

How can I reset author, but keep the original committer date?

like image 901
Aleks Avatar asked Dec 23 '16 12:12

Aleks


People also ask

What happens if you -- Amend modify a commit after a push?

If you amend the HEAD commit and push usually (without --force) then surprisingly it does not fail. HEAD commit message is updated with the changed commit Id. It means other commit IDs except HEAD remains intact.

Does amend Change commit ID?

You can change the most recent commit message using the git commit --amend command. In Git, the text of the commit message is part of the commit. Changing the commit message will change the commit ID--i.e., the SHA1 checksum that names the commit. Effectively, you are creating a new commit that replaces the old one.


1 Answers

As mentioned in some other answers you would probably use:

git commit --amend --reset-author --no-edit --date="<old-date>"

While this works it's a lot of manual copying or typing to get the old date in place. You might want to get the date automatically, by getting only the date of the last entry in the log:

git log -n 1 --format=%aD

Combine the two and use some shell magic:

git commit --amend --reset-author --no-edit --date="$(git log -n 1 --format=%aD)"

This automatically sets the date of the last commit in the log, aka the one to be amended, as date of the new commit with the changed author.

Now changing the author on a larger amount of commits, say because you forgot to set the author in the cloned git repo, an interactive rebase is your friend:

git rebase -i <commit before wrong author and email>

You then change all commits you want to adjust from pick to edit and save the file. Git stops on every commit to be edited and you rerun:

git commit --amend --reset-author --no-edit --date="$(git log -n 1 --format=%aD)" && \
    git rebase --continue

If it's a reasonable small number of commit you can repeat this command using the shells arrow-up key until until the rebase finishes. If there is a larger number of commits, that typing arrow-up + return becomes too tedious you might want to create a small shell script that repeats the above command until the rebase finishes.

like image 142
Holger Böhnke Avatar answered Oct 06 '22 08:10

Holger Böhnke