DEV Community

loading...
Cover image for Change the date of a git commit

Change the date of a git commit

Hugo Di Francesco
Developer, JavaScript, CSS and web. Writing at Code with Hugo.
Originally published at codewithhugo.com on ・1 min read

One of the greatest and worst things with git is that you can rewrite the history. Here’s a sneaky way of abusing that, I can’t think of a legitimate reason to do this.

As with anything, thanks StackOverflow for all the options I can pick from 👍.

Set the date of the last commit to the current date

GIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"
Enter fullscreen mode Exit fullscreen mode

Set the date of the last commit to an arbitrary date

GIT_COMMITTER_DATE="Mon 20 Aug 2018 20:19:19 BST" git commit --amend --no-edit --date "Mon 20 Aug 2018 20:19:19 BST"
Enter fullscreen mode Exit fullscreen mode

Set the date of an arbitrary commit to an arbitrary or current date

Rebase to before said commit and stop for amendment:

  1. git rebase <commit-hash>^ -i
  2. Replace pick with e (edit) on the line with that commit (the first one)
  3. quit the editor (ESC followed by :wq in VIM)
  4. Either:
    • GIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"
    • GIT_COMMITTER_DATE="Mon 20 Aug 2018 20:19:19 BST" git commit --amend --no-edit --date "Mon 20 Aug 2018 20:19:19 BST"

See here for more information around rebasing and editing in git: Split an existing git commit.

Sean Mungur

Discussion (1)

Collapse
sprzedwojski profile image
Szymon Przedwojski

Thanks, it worked.
As for a use case, I just had 4 commits that I rebased and squashed together. The resulting final commit had the date of the first of the 4 commits, which was a week ago.
And I wanted to push the final commit with today's date, which is a legitimate reason to me ;)