Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 31 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ugultopu/0b6412674073a5b603f8227cb108441c to your computer and use it in GitHub Desktop.
Save ugultopu/0b6412674073a5b603f8227cb108441c to your computer and use it in GitHub Desktop.

As far as I can tell, you can't do it conveniently. That is, git-rebase does not give you an option to preserve the committer date. Unless you give the --ignore-date (or its alias, --reset-author-date) option, it will always preserve the author date. However, there is no way to make git-rebase preserve the committer date, unless some manual script is crafted.

The best you can do is to make the committer date equal to the author date. Recently (in 2020 Q4), git-rebase --interactive has gained the ability to use the --committer-date-is-author-date flag with the interactive rebase. Before that, there was no way of influencing the committer date at all with the interactive rebase. Note that this flag does not preserve the committer date. It merely makes the committer date equal to the author date.

You might be thinking "well, isn't that effectively preserving the committer date, since normally the committer date is always equal to the author date?". Normally, you would be correct. However, there are a lot of scenarios where committer date is not equal to author date. For example, someone might have written a patch in 2021-03-05 and submitted it the same day. The patch might make its way into the repository only on 2021-03-15 (after reviewing, etc.) In this scenario, the author date would be 2021-03-05 but the committer date would be 2021-03-15.

Now, if you rebase that commit on 2021-04-10, by default, the committer date would be 2021-04-10. If you use the --committer-date-is-author-date option, the committer date would be 2021-03-05. However, as you can observe, in neither scenario the commiter date would be kept as 2021-03-15, which is the original commit's date.

In short, as far as I can tell, as of now (2021 Q1), there is no way of preserving the committer date in Git, not at least using a command line option. One might be able to craft a custom program or script that accomplishes this.

References

@Ark-kun
Copy link

Ark-kun commented Mar 1, 2022

You can replace rebase with cherry-picking.
Then, for each commit you can get the committer date, store it in a variable, then use it when cherry-picking.

@dharmaturtle
Copy link

IMO this one liner is easy:

git -c rebase.instructionFormat='%s%nexec GIT_COMMITTER_DATE="%cD" git commit --amend --no-edit' rebase -i

From: https://old.reddit.com/r/git/comments/jp59k5/rebase_without_changing_commit_timestamps/

@yurenchen000
Copy link

IMO this one liner is easy:

git -c rebase.instructionFormat='%s%nexec GIT_COMMITTER_DATE="%cD" git commit --amend --no-edit' rebase -i

From: https://old.reddit.com/r/git/comments/jp59k5/rebase_without_changing_commit_timestamps/

this worked, saved my life 👍 ⚡

@leewp14
Copy link

leewp14 commented Apr 24, 2023

IMO this one liner is easy:

git -c rebase.instructionFormat='%s%nexec GIT_COMMITTER_DATE="%cD" git commit --amend --no-edit' rebase -i

From: https://old.reddit.com/r/git/comments/jp59k5/rebase_without_changing_commit_timestamps/

This works perfectly, thanks for sharing!

@yeasy
Copy link

yeasy commented Jun 8, 2023

Enhance the code to keep the COMMITTER's name and email, too.

git -c rebase.instructionFormat='%s%nexec GIT_COMMITTER_DATE="%cD" GIT_COMMITTER_NAME="%cn" GIT_COMMITTER_EMAIL="%ce" git commit --amend --no-edit' rebase -i <CODE_BASE>

@developerTested
Copy link

git -c rebase.instructionFormat='%s%nexec GIT_COMMITTER_DATE="%cD" git commit --amend --no-edit' rebase -i

Thank you very much and i added --date to keep date in timestamp of commit

Here the command i used

git -c rebase.instructionFormat='%s%nexec GIT_COMMITTER_DATE="%cD" git commit --amend --no-edit --reset-author --date="%cD"' rebase --root

It set same commit time as before and updated the username and email because i needed it 🎉

@anthonyisenseearm
Copy link

developerTested's version did exactly what I needed it to do. Rebase in Windows Git Bash reset author and email to git config's author/email while maintaining original commit dates on all commits. Thank you, all!

@TheSast
Copy link

TheSast commented Jan 27, 2024

Combined version of yeasy's and developerTested's commands for easy copying:

git -c rebase.instructionFormat='%s%nexec GIT_COMMITTER_DATE="%cD" GIT_COMMITTER_NAME="%cn" GIT_COMMITTER_EMAIL="%ce"  git commit --amend --no-edit --reset-author --date="%cD"' rebase -i

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment