Modifying the date of a commit

Mar 18, 2016 using tags git

To change the date of the most recent commit to now:

GIT_COMMITTER_DATE="`date`" git commit --amend --date "`date`"

You can of course specify a specific commit date:

MYDATE="Wed Feb 16 14:00 2016 -0400" GIT_COMMITTER_DATE="$MYDATE" git commit --amend --date "$MYDATE"

Another helpful tip for when you need to use a different date while committing (i.e. in non-rebase mode):

MYDATE="Wed Feb 16 14:00 2016 -0400" GIT_AUTHOR_DATE="$MYDATE" GIT_COMMITTER_DATE="$MYDATE" git commit

If you would rather add a git alias to do this (and you totally should!), here’s what your change date (git cd) alias could look like (credit Stack Overflow):

[alias]
  cd = "!d=\"$(date -d \"$1\")\" && shift && \
    git diff-index --cached --quiet HEAD --ignore-submodules -- && \
    GIT_COMMITTER_DATE=\"$d\" git commit --amend -C HEAD --date \"$d\"" \
    || echo >&2 "error: date change failed: index not clean!"

Now to change the date of the last commit:

git cd now  # update the last commit to the current time
git cd "1 hour ago"
git cd "Feb 1 12:33:00"
# ... etc

I’ve Googled this more than I care to admit so throwing this here for posterity