DEV Community

Cover image for Commit with a Past Date and Time in Git
Pradumna Saraf
Pradumna Saraf

Posted on

Commit with a Past Date and Time in Git

As developers, we sometimes want to change past Git commits. Although true time travel is impossible, Git offers a neat way to commit with past dates and times. Here's how:

To change the last commit date and time, use this command:

git commit --amend --date="YYYY-MM-DD HH:MM:SS" -m "Your commit message here"
Enter fullscreen mode Exit fullscreen mode

To create a new commit with the past date, use:

git commit --date="YYYY-MM-DD HH:MM:SS" -m "Your commit message here"
Enter fullscreen mode Exit fullscreen mode

Conclusion:

Keep in mind that amending a commit means rewiring its history. Use this powerful feature responsibly.

Top comments (12)

Collapse
 
gianpiero_errigo profile image
Gianpiero Errigo

Beware that --amend will not modify last commit, it will replace it with a new one, changing repo history.
That could be tricky if someone already cloned the branch.

Collapse
 
pradumnasaraf profile image
Pradumna Saraf

Yes. Thank you.

Collapse
 
gadrawingz profile image
Gad Iradufasha

Thanks for this warning. Someone can make some mistakes!

Collapse
 
noelgamaliel profile image
DIAMONA

Where is place of name the commit?

Collapse
 
pradumnasaraf profile image
Pradumna Saraf

When you don't specify the -m flag for the commit message, a text editor like Vim will open up for you to enter the commit message.

However, I will edit the article and include the -m flag directly in the command itself.

Collapse
 
noelgamaliel profile image
DIAMONA
Collapse
 
jaywilkinson profile image
James Wilkinson

Useful for when you don't want other members of the team to see your screwed up sleeping pattern and 4am redbull-infused commits.

Collapse
 
priddybroderick profile image
Priddybroderick

Thank you for sharing this helpful tip for changing past Git commits! As developers, having the ability to adjust commit dates and times can be valuable for keeping our version history accurate and organized.
It's essential to remember that amending a commit can impact the commit history lane, so we should use this feature responsibly and with caution.

Collapse
 
pradumnasaraf profile image
Pradumna Saraf

Thank you.

Collapse
 
bullbiased profile image
Bull

WOW. I din't knew about this. Thanks for sharing.

Collapse
 
pradumnasaraf profile image
Pradumna Saraf

Thank you.

Collapse
 
gadrawingz profile image
Gad Iradufasha • Edited

Time traveling ain't possible but this way is useful in most cases on behalf of the developer!

More info like this:

Check this answer on Stackoverflow