How do I edit a commit's timestamp after (accidentally) pushing it to the remote repository?
I changed my system time clock to test behavior in my app, and forgot to set it back beforegit add -u .
and git push
. Now my GitHub repo shows multiple files that were edited "in a month".
I have followed the advice here – specifically the comments to the OP that recommend git push --force
, but unfortunately the edit dates stay the same.
Just do git commit --amend --reset-author --no-edit . For older commits, you can do an interactive rebase and choose edit for the commit whose date you want to modify.
The --force option for git push allows you to override this rule: the commit history on the remote will be forcefully overwritten with your own local history. This is a rather dangerous process, because it's very easy to overwrite (and thereby lose) commits from your colleagues.
The git commit --amend command is a convenient way to modify the most recent commit. It lets you combine staged changes with the previous commit instead of creating an entirely new commit. It can also be used to simply edit the previous commit message without changing its snapshot.
There are actually two different timestamps recorded by Git for each commit: the author date and the commit date. When the commit is created both the timestamps are set to the current time of the machine where the commit was made.
Well, that article is about changing the commit message, and then force pushing the change. But here in this case, you first need to fix the commit timestamp, before doing the git push --force
.
Read this article to see how to fix the timestamp: How can one change the timestamp of an old commit in Git?
And then force the push.
(Btw, on github, it may take a few refreshes before you see the change. Also, I assume you know that you are breaking the history and if somebody else cloned the repo, you broke them, etc etc, right? good!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With