How to Change the Date of a Git Commit

Updated onbyAlan Morel
How to Change the Date of a Git Commit

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:

BASH
GIT_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:

BASH
GIT_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:

BASH
git rebase -i HEAD~1

Here's using a commit hash:

BASH
git 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:

BASH
GIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"

If you want to set the commit's date to a specific date:

BASH
GIT_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:

BASH
git 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.

To learn more about web development, founding a start-up, and bootstrapping a SaaS, follow me on X!
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.