How to Update the date of arbitrary commit

Commits in git are immutable and each commit maintains the two different dates that is

  • AuthorDate
  • CommitDate

When you amend the commit, author date won’t be changed but commitDate will be updated to current date and time

To list commits along with AuthorDate and CommitDate use the following command

git log --format=fuller

Set the authorDate of last commit

If the date of the commit is supposed to be current date, then

git commit --amend --date="now"

Else you can set the commit date to specific date and time as follows

git commit --amend --no-edit --date "Mon 20 Aug 2018 20:19:19 BST"
git filter-branch --env-filter 'export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"'

To change Date for multiple commits (both author and commit date’s) try the following command

git rebase HEAD~5 --exec "git commit --amend --no-edit --date 'now'"

Above command changes the AuthorDate and CommitDate for last 5 commits

Or, interactively rebase as follows

git rebase HEAD~5

you will be presented with rebase interface based on the 5 different commits using default text editor (ex: vim). Replace the “pick” before the commits you want to modify by “e” (or “edit”) and exit the editor.

For each commit set the date to as required

git commit --amend --date "November 2nd 2023 09:00"

then continue,

git rebase --continue

Repeat this process for all commits


Another quick way to change the date for last commit

GIT_COMMITTER_DATE="Thu Sep 19 12:00 2020 +0530" git commit --amend --date="Thu Sep 19 12:00 2020 +0530"

After you update the commit date either AuthorDate or CommitDate or both, remote will refuse to update if you try to push because new commit hash will be generated for the updated commit. In such a case you use force option -f

git push -f origin branch 

Use -f option only if you know what you are doing else do not put remote on 🔥🔥🔥🔥

Default image
neotam
Naveen T aka neotam. Programming language agnostic, Software architect, Python expert, Networking & DevOps engineer & consultant with 7+ years of experience in creating serious web applications, real time event-driven non blocking applications and database driven applications ranging from small scale to enterprise grade. website
Leave a Reply