406

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136
ksol
  • 11,835
  • 5
  • 37
  • 64
  • 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 '16 at 18:22

6 Answers6

454

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.)

CharlesB
  • 86,532
  • 28
  • 194
  • 218
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • 31
    FTR, looks like on OS X, `date` doesn't know `-R`. Using `date` without options did the job anyway – ksol Feb 02 '12 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)\"` – Fabian Steeg Oct 25 '12 at 14:40
  • 9
    `date -R` doesn't work on OSX, use `date +%s` instead – stash Feb 04 '14 at 19:20
  • 4
    Idem for FreeBSD: `git commit --amend --date="$(date +%s)"` – Dereckson Feb 12 '14 at 09:25
  • So you commit first and then run this? – EpicDavi May 12 '14 at 15:38
  • `git commit --amend --date="'$(date -R)'"` generates this error for me in bash (Debian7): `error: unknown option 'amend --no-edit --date='Tue, 10 Mar 2015 17:53:06 -0700''` and `git commit --amend --date="$(date +%s)"` generates this error: `error: unknown option 'amend --date=1426035358'`. can anyone tell me why? – maurice Mar 11 '15 at 01:01
  • 1
    @maurice: I don't think those can be exactly the commands you typed: in the first instance, for example, the output you quote suggests you'd also supplied `--no-edit`. I can reproduce what you're seeing under bash in Debian 7 only if i put a ``\`` before the spaces after options or put quotes from `amend` to the end of `date` - in other words, escaping so that bash is treating everything after the first `--` as one option argument. – Mark Longair Mar 21 '15 at 13:56
  • @stash Unless you're using the GNU implementation of date. ;) – Nicolas De Jay Aug 19 '15 at 01:04
  • 1
    @EpicDavi yes, commit first and run it after :) – maxime1992 Oct 28 '15 at 09:52
  • How can I pass it the date of another commit from git show ? – Mr_and_Mrs_D May 13 '16 at 20:27
  • 171
    `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](https://github.com/git/git/blob/master/date.c#L938) '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 '16 at 00:18
  • How can I retroactively do this if I already have commits amended and commit dates all over the place? – Aaron Franke Oct 02 '19 at 01:42
  • 1
    The `--no-edit` flag is also handy (not for original question necessarily but related questions certainly). – Cameron Tacklind Feb 11 '21 at 21:54
  • The general applicability of this comment might be limited, but if ending here because of `git commit --amend --date='now'` appears to not be working. Do make sure the clock is actually correctly set on the machine where you are running the command. – sampi Sep 20 '22 at 18:28
420

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

git commit --amend --date=now
mja
  • 1,273
  • 1
  • 20
  • 22
Kamal
  • 7,160
  • 2
  • 21
  • 12
314

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Rup
  • 33,765
  • 9
  • 83
  • 112
24

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
JLarky
  • 9,833
  • 5
  • 36
  • 37
4

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
  • 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? – Llama D'Attore Sep 22 '20 at 01:35
2

There are two dates on a commit

  • The author date records when the commit was originally made; it can be specified with --date command-line argument
  • The commit date is updated changed every time a commit is modified; it can be specified using GIT_COMMITTER_DATE environment varable

To change both of then you can do this:

GIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"
starfry
  • 9,273
  • 7
  • 66
  • 96