How to Change the Date of a Git Commit
Table of Contents
In this post, we'll learn how to change the date of a git commit.
In general, re-writing the history of git can be a bit tricky and should only be done when absolutely necessary.
Either way, if you're still interested, let's learn all the ways to change the date of a git commit.
Update the Date of the Last Commit to Current Date
To update the date of the last commit, you can use the following command:
BASHGIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"
This updates the environment variable GIT_COMMITTER_DATE
to $(date)
. Git then uses this variable when amending the commit.
From here, you can then just push the change.
Update the Date of the Last Commit to Any Date
If you want to update the date of the last commit to a specific date, you can use the following command:
BASHGIT_COMMITTER_DATE="Tue Fan 25 12:00 2022 +0000" git commit --amend --no-edit
Basically, instead of using the date
variable, you can manually set the date of the commit to any date you want.
Update the Date of Any Commit to Any Date
If you don't want to update the date of the last commit, you can rebase to the commit you want to edit, then go from there.
First, rebase to the commit you want to edit. You can either use an index number or a commit hash.
Here's using an index number:
BASHgit rebase -i HEAD~1
Here's using a commit hash:
BASHgit rebase <commit-hash>^ -i
Make sure to select e
or edit
from the menu.
After you quit, you can then update the commit date to any date you want.
If you want to set the commit's date to the current date:
BASHGIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"
If you want to set the commit's date to a specific date:
BASHGIT_COMMITTER_DATE="Tue Fan 25 12:00 2022 +0000" git commit --amend --no-edit
Remember that after you are done, you will want to continue the rebase:
BASHgit rebase --continue
Once you are finished, you can push the changes.
Conclusion
In this post, we've seen how to change the date of a git commit, both using today's date and a specific date.
We've also seen to edit any commit in the past by rebasing to the commit you want to edit, then updating the date of the commit.
Hopefully, you've learned all the ways to change the date of a git commit.
- How to Install Node on Windows, macOS and Linux
- Getting Started with Solid
- Managing PHP Dependencies with Composer
- Getting Started with Svelte
- How to deploy a .NET app using Docker
- How to deploy a Deno app using Docker
- Getting Started with Handlebars.js
- Getting User Location using JavaScript's Geolocation API
- Learn how to build a Slack Bot using Node.js
- Using Push.js to Display Web Browser Notifications
- Setting Up Stylus CSS Preprocessor
- Getting Started with Vuex: Managing State in Vue