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"
To create a new commit with the past date, use:
git commit --date="YYYY-MM-DD HH:MM:SS" -m "Your commit message here"
Conclusion:
Keep in mind that amending a commit means rewiring its history. Use this powerful feature responsibly.
Top comments (12)
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.
Yes. Thank you.
Thanks for this warning. Someone can make some mistakes!
Where is place of name the commit?
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.good @pradumnasaraf
Useful for when you don't want other members of the team to see your screwed up sleeping pattern and 4am redbull-infused commits.
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.
Thank you.
WOW. I din't knew about this. Thanks for sharing.
Thank you.
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