400

I found myself amending my commits quite often. I don't stash so much because I tend to forget I did so, especially when I want to save what I did before I leave or before a weekend, so I do a "draft" commit. Only thing is, when I amend the commit, it is still set to the original author date. Is there a (simple) way to update it when amending?

1
  • 6
    This is especially useful when you perform interactive rebase and expect GitHub to display the commits in the Pull Request in tree order instead of date order.
    – Sukima
    Mar 15, 2016 at 18:22

5 Answers 5

451

You can change the author date with the --date parameter to git commit. So, if you want to amend the last commit, and update its author date to the current date and time, you can do:

git commit --amend --date="$(date -R)"

(The -R parameter to date tells it to output the date in RFC 2822 format. This is one of the date formats understood by git commit.)

14
  • 31
    FTR, looks like on OS X, date doesn't know -R. Using date without options did the job anyway
    – ksol
    Feb 2, 2012 at 10:29
  • 8
    Note that if you want this as an alias you need to add a ! and escape the quotation marks, e.g. can = !git commit --amend --date=\"$(date -R)\" Oct 25, 2012 at 14:40
  • 9
    date -R doesn't work on OSX, use date +%s instead
    – stash
    Feb 4, 2014 at 19:20
  • 4
    Idem for FreeBSD: git commit --amend --date="$(date +%s)"
    – Dereckson
    Feb 12, 2014 at 9:25
  • 170
    git commit --amend --date=now works also. No need to invoke a subshell with the date(1) command. You can see it in the source code of git(1) in date.c 'now' is a special value which is converted using date_now() function. Same for 'noon', 'yesterday', 'midnight', 'tea' and others listed in the structure array special[].
    – sbz
    Jul 29, 2016 at 0:18
410

As of Git v2.1.4 (tested on Debian 8 (Jessie))

git commit --amend --date=now
1
  • 47
    And if you don't want to see the commit message again, add --no-edit.
    – Henrik N
    Jan 29, 2020 at 12:55
309

Another way to do this is

git commit --amend --reset-author

This does change the commit author as well as the date - but if it was originally your unpushed commit then that's a no-op.

You can also add --no-edit if you want to update the date on multiple commits but you want the commit messages to stay untouched. This way you will not be prompted to edit the message for each commit.

1
  • 5
    Single copy-paste: git commit --amend --reset-author --no-edit
    – AgentRev
    May 27, 2022 at 20:45
22

I like Mark's answer and used it myself several times, but now I'm on OS X and date -R is not supported. But everything is much easier than original answer made us think, just use empty string!

git commit --date= --amend

UPDATE:

You can also try

git commit --date="$(date)" --amend

Or in new versions of git

git commit --date=now --amend
4
  • 7
    That doesn't work. Git aborts with the error fatal: invalid date format:
    – Nikos C.
    Feb 5, 2016 at 12:56
  • 1
    Worked for me (on Windows, Git 1.9.4)
    – Csq
    Feb 7, 2016 at 23:32
  • 1
    Better to use --date="$(date)" in OS X. The option -R is GNUism. Feb 1, 2017 at 0:44
  • I don't know whether to find it funny to see stackoverflow answers getting more and more flooded with "does not work on my Macintosh" comments. I mean, who cares ? Mar 14 at 19:16
3

I created this npm package if someone still looking for a simple way to change dates of multiple commits.

https://github.com/bitriddler/git-change-date

Usage:

npm install -g git-change-date
cd [your-directory]
git-change-date
1
  • 5
    I appreciate automating things like this for better workflow. Thank you for the npm package. @Urda why discourage people from automating things if that solution makes their workflow better? Sep 22, 2020 at 1:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.