1085

The answers to How to modify existing, unpushed commits? describe a way to amend previous commit messages that haven't yet been pushed upstream. The new messages inherit the timestamps of the original commits. This seems logical, but is there a way to also re-set the times?

Community
  • 1
  • 1
Dhskjlkakdh
  • 11,341
  • 5
  • 22
  • 17

30 Answers30

1172

You can do an interactive rebase and choose edit for the commit whose date you would like to alter. When the rebase process stops for amending the commit you type in for instance:

git commit --amend --date="Wed Feb 16 14:00 2011 +0100" --no-edit

P.S. --date=now will use the current time.

Afterward, you continue your interactive rebase.

To change the commit date instead of the author date:

GIT_COMMITTER_DATE="Wed Feb 16 14:00 2011 +0100" git commit --amend --no-edit

The lines above set an environment variable GIT_COMMITTER_DATE which is used in amending commit.

Everything is tested in Git Bash.

Wenfang Du
  • 8,804
  • 9
  • 59
  • 90
Paul Pladijs
  • 18,628
  • 5
  • 28
  • 31
  • 23
    @nschum --date="" and --data"non-date-text" all yield the same, taking the date of now. – Paul Pladijs Nov 18 '11 at 09:34
  • 15
    on git version 1.7.7.1 using --date="now" gives fatal: invalid date format: now – Aragorn Jan 10 '12 at 17:58
  • 9
    When the commit whose date you want to change is the most recent commit, you don't have to do the `rebase`, you can just do the `git commit --amend` – Eponymous May 02 '12 at 13:09
  • 7
    Instead of export GIT_COMMITTER_DATE="", try unset GIT_COMMITTER_DATE. – Mark E. Haase May 30 '12 at 13:39
  • `export GIT_COMMITTER_DATE=""` doesn't delete the environment variable, it sets it to the empty string; I don't know how git will deal with that. As @mehaase say, `unset GIT_COMMITTER_DATE` is better -- but combining them into a single command, as in Luke Ehresman's answer, is even better; the environment variable setting applies only to that command. – Keith Thompson Feb 14 '13 at 20:25
  • @mehaase: or, simply `GIT_COMMITTER_DATE="..." git commit --amend` – Ether Jul 10 '13 at 22:32
  • `GIT_AUTHOR_DATE` and `GIT_COMMITTER_DATE` are described at https://www.kernel.org/pub/software/scm/git/docs/git-commit.html in the 'date formats' section, fyi – Antoine Aug 08 '14 at 12:55
  • I tried to do that, but the interactive rebase doesn't let me edit the commits even though I did replace "pick" with "edit". Anything I could be doing wrong? I did `git rebase --interactive HEAD~7`, then edited the git-rebase-todo file (replace "pick" with "edit"), but every time I run `git rebase --continue` it just adds the next commit with no chance to modify it (kind of as if it was still a pick, but the console does read "edit"). When I try `git commit --amend --date="20160203T22:12:02"`, it tells me "fatal: You are in the middle of a cherry-pick -- cannot amend" – user14764 Feb 04 '16 at 13:17
  • 2
    I'm using --no-edit so that you can use in automated scripts! `+ var fixedDate = strftime(new Date(), "%c"); + var result = shelljs.exec("git commit --amend --date=\"" + fixedDate + "\" --no-edit");` – Marcello DeSales Feb 20 '16 at 00:19
  • Just to let you guys know, I've created a NodeJS CLI to easily modify the previous git commit in your current directory. You can find it at https://github.com/samholmes1337/git-date or run `npm install -g git-date` to install and `git date ` to run. – Sam Holmes Feb 29 '16 at 18:32
  • If you're an idiot like me, you missed the fact that the `--amend` commands *must be done inside a rebase*, not just willy-nilly from the command-line – xaphod Jul 05 '18 at 19:38
  • 2
    Thanks! I just use `--date=now` and it worked. No need to bother with guessing formats when I want the current time – coladict Nov 17 '20 at 18:54
  • Won't this change the commit date of all the commits, except for the one being edited, to the current date-time? @PaulPladijs – Ayush Mandowara Feb 13 '21 at 13:41
  • To be self-contained, I'd recommend to explain how to do the "interactive rebase". – Nike Feb 09 '23 at 02:56
  • For anyone trying to execute this on Powershell, but failing, you need to do this in two separate commands, 1st set the env variable like `$env:GIT_COMMITTER_DATE` and do `git commit --amend --no-edit` after – GooDeeJAY May 11 '23 at 15:03
649

Use git filter-branch with an env filter that sets GIT_AUTHOR_DATE and GIT_COMMITTER_DATE for the specific hash of the commit you're looking to fix.

This will invalidate that and all future hashes.

Example:

If you wanted to change the dates of commit 119f9ecf58069b265ab22f1f97d2b648faf932e0, you could do so with something like this:

git filter-branch --env-filter \
    'if [ $GIT_COMMIT = 119f9ecf58069b265ab22f1f97d2b648faf932e0 ]
     then
         export GIT_AUTHOR_DATE="Fri Jan 2 21:38:53 2009 -0800"
         export GIT_COMMITTER_DATE="Sat May 19 01:01:01 2007 -0700"
     fi'
tbodt
  • 16,609
  • 6
  • 58
  • 83
Dustin
  • 89,080
  • 21
  • 111
  • 133
  • 7
    See "DATE FORMATS" https://www.kernel.org/pub/software/scm/git/docs/git-commit.html – Dustin Mar 14 '13 at 21:31
  • 11
    That **found** the correct value, but just setting those variables didn't actually seem to affect the date of the old commit. – IQAndreas May 10 '14 at 20:17
  • 48
    What do you mean by "This will invalidate that and all future hashes."? – EpicDavi May 12 '14 at 12:13
  • if you get Namespace refs/original/ not empty error, you can run rm - fr "$(git rev-parse --git-dir)/refs/original/" – sk8asd123 Jun 24 '14 at 17:31
  • 25
    EpicDavi: It means that you will have to force push to any remote repository, and anyone who has pulled the commit or any future commits will have to reset and pull, or delete and clone from scratch. As far as I know, there is no method that gets around this. – EriF89 Sep 09 '14 at 09:12
  • 1
    one question regarding the example values of AUTHOR/COMMITER date: What sense would it make for the author date to be after the commit date? When I look e.g. [here](http://alexpeattie.com/blog/working-with-dates-in-git/), the author date is explained as the date/time of the original "author's" commit, while commit date is the date this specific commit was applied - so under which circumstances would it make sense for the commit date to be before the author date? – codeling Sep 09 '14 at 19:35
  • @codeling I had this case lately. Consider rebasing an old branch (e.g. created a year ago) onto a recently created one. After the rebase the AUTHOR_DATE will have its original value from 2014, and the COMMITTER_DATE will reflect the date of rebase. Analogically if someone else was the author of the original commits, you'd have separate author (the original author) and commiter (the one who performed the rebase). This way you don't overwrite the original author and date with your data as the rebaser. – Aleksander Sep 21 '15 at 07:42
  • 1
    `GIT_AUTHOR_DATE` didn't work for me. had to use `--date="..."`. – Antoine Jun 26 '16 at 23:57
  • A convenient implementation of @Dustin 's approach for bash: ```sh function fix_commit_date { git filter-branch -f --env-filter "if [ \$GIT_COMMIT = $1 ] then export GIT_AUTHOR_DATE='$2' export GIT_COMMITTER_DATE='$2' fi" } ``` Add to your `~/.bashrc` or ~/.bash_profile` at your own risk. Then use: ```sh $ fix_commit_date "" ``` – joiggama May 18 '18 at 10:43
  • 2
    I don't want to wait for analyzing over 9000 commits! – Alex78191 Jul 12 '18 at 11:38
  • 8
    Just as a note for beginners, the short hash does not work in the if statement, use the long SHA-1 – 40detectives Mar 19 '19 at 15:57
  • It seems to be working. However I would like to patch multiple commits, but using this method, I can patch only one, then all subsequent patches on other commits seems to be ignored (the dates do not change despite of the executed command) – g.pickardou Mar 27 '20 at 11:28
  • Great answer. Unforunately this doesn't work on MacOS: `Rewrite 5385b98dafc3007cf73a54116e962c54362e7bb0 (1/11) (0 seconds passed, remaining 0 predicted) /Applications/Xcode.app/Contents/Developer/usr/libexec/git-core/git-filter-branch: line 405: [: 5385b98dafc3007cf73a54116e962c54362e7bb0: unary operator expected` – Daniel Gelling Apr 03 '20 at 05:27
  • I used this to un-restart commiter_date `git filter-branch --env-filter 'export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"'` – Brambor Aug 08 '20 at 19:28
  • Note: The Commit ID variable is the long hash version always. If you have only abbreviated commit IDs, [then expand them beforehand](https://stackoverflow.com/a/41717108/3894752), otherwise the check will fail. – pixelbrackets Aug 28 '20 at 15:13
  • So I ran this today (made a copy branch) but it ended up making me unable to merge to master since it rewrites the history with new commits. Doing a git rebase solved it but then all my commits on that branch had the current date. The way to fix It was to add the flag - `git rebase master --committer-date-is-author-date` – Kyle Roach Oct 13 '21 at 23:18
  • @codeling I think the answer to your (almost 8 year old question) is it doesn't make any sense to do that. I'm not sure if it was intentional but perhaps the example simply shows that you *can* do it if you wanted to. – TTT Apr 18 '22 at 19:34
454

A better way to handle all of these suggestions in one command is

LC_ALL=C GIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"

This will set the last commit's commit and author date to "right now."

sanmai
  • 29,083
  • 12
  • 64
  • 76
Luke Ehresman
  • 5,187
  • 1
  • 16
  • 20
  • 23
    This works great to edit specific commits during an interactive rebase. – friederbluemle Dec 09 '13 at 01:21
  • 3
    You could add an alias to the shell for it too – kaleissin Mar 14 '14 at 12:05
  • 18
    It seems that Git isn't locale-aware of date format, so to be completely correct, you'll have to make it something like this: `LANG= GIT_COMMITTER_DATE="\`date\`" git commit --amend --date "\`date\`"` – Michał Góral May 27 '15 at 19:53
  • I do that when I rebase and squash a branch, so it creates a single commit with an updated timestamp. – Luke Ehresman Dec 05 '16 at 14:39
  • Another solution to locale problem is `GIT_COMMITTER_DATE="\`date -R\`" git commit --amend --date "\`date -R\`"` – Josef Adamcik Sep 19 '17 at 14:02
  • Git isn't only used on systems with `bash`... This is useless in e.g. Powershell on Windows: `The term 'GIT_COMMITTER_DATE=date" git commit --amend --date date"' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.` – Alex Oct 17 '17 at 10:55
  • 17
    you can also just do `--date "now"`. Git >= 2 will interpret that. – wisbucky Mar 07 '18 at 22:17
  • You can also use `date --date="11 day ago"` to set a custom date – aemonge Apr 22 '20 at 08:33
  • 15
    What does `LC_ALL=C` do? – stwykd May 05 '20 at 06:25
  • 4
    @stwykd it ensures that `$(date)` uses a format that Git can understand, by temporarily overriding the system locale – pxeger Jul 04 '21 at 15:09
339

Just do git commit --amend --reset-author --no-edit. For older commits, you can do an interactive rebase and choose edit for the commit whose date you want to modify.

git rebase -i <ref>

Then amend the commit with --reset-author and --no-edit to change the author date to the current date:

git commit --amend --reset-author --no-edit

Finally continue with your interactive rebase:

git rebase --continue
gypaetus
  • 6,873
  • 3
  • 35
  • 45
213

I wrote a script and Homebrew package for this. Super easy to install, you can find it on GitHub PotatoLabs/git-redate page.

Syntax:

git redate -c 3

You just have to run git redate and you'll be able to edit all the dates in vim of the most recent 5 commits (there's also a -c option for how many commits you want to go back, it just defaults to 5). Let me know if you have any questions, comments, or suggestions!

enter image description here

kenorb
  • 155,785
  • 88
  • 678
  • 743
bigpotato
  • 26,262
  • 56
  • 178
  • 334
  • 5
    Great stuff, even though I had to use vim rather than nano – howdoyouturnthison Mar 25 '17 at 16:51
  • Thanks @Edmund for a great script. I couldn't see the date to edit in vi after I ran git redate -c. All I see is %cI | XXXXXXXXXXXXXXXX | Initial commit. Could you please help? Thanks – Kiem Nguyen Apr 22 '17 at 23:55
  • @KiemNguyen could you try just git redate (without the -c)? – bigpotato Apr 22 '17 at 23:57
  • Hi @Edmund I tried just git redate (without the -c) but it still doesn't show the date. It only show %cI as the date then my repo hash – Kiem Nguyen Apr 23 '17 at 04:16
  • @KiemNguyen Hm just pulled the file directly and ran it without being able to reproduce %cl issue. Did you perhaps install it through homebrew? I'll update the homebrew package to the latest version – bigpotato Apr 23 '17 at 05:55
  • @KiemNguyen update the homebrew package to v1.0.2, just tested and it still works – bigpotato Apr 23 '17 at 06:17
  • Awesome thanks @Edmund. I updated homebrew package then unlinked and reinstalled git-redate, now I can see the date now. – Kiem Nguyen Apr 26 '17 at 02:37
  • 7
    completely agree with Mina and @howdoyouturnthison here, why don't you make it editor agnostic via EDITOR environment variable? (also I'm on linux, not mac...) – ympostor Jun 21 '17 at 10:46
  • 3
    Thanks @Edmund! Just in case, you script has a problem with handling default value for COMMITS. If it's not set, the following code applies filters just to (I guess/found) the last commit. "git filter-branch -f --env-filter "$ENVFILTER" HEAD~$COMMITS..HEAD >/dev/null" – Grigory Entin Oct 30 '17 at 04:42
  • Hello, I'm trying to edit simultaneous dates that occured 230 commits ago, after changing date, the scripts fails with `/mingw32/bin/git: Argument list too long` Is there any workaround? Thanks – Julien M Feb 24 '18 at 22:56
  • useless, fails with error fatal: ambiguous argument 'HEAD~7..HEAD': unknown revision or path not in the working tree. – Ivan Castellanos Jul 08 '19 at 20:31
  • Works well, thanks for writing this. Would be great to have a screenshot or more documentation of how it works - lets you edit the dates of each commit in a text editor (setting author and committer date). However if you are in wrong directory or your working tree is unclean (uncommitted files), your edits are lost - so copy/paste them before exiting! – RichVel Nov 05 '19 at 10:42
  • Works like a charm, even with those "strange" editors (I'm totally Windows user:)). I couldn't make it work with Notepad++ but the usage is so simple that even Nano was sufficient for this job. Thank you. – Paweł Szczygielski Apr 03 '20 at 13:22
  • That's just wonderful! For some reason it didn't want to update the latest commit. When editing 3 commits, it didn't update the date for the oldest one, but I just used --commits 4, edited 3, and it worked. Thank you! – FreeNickname Jan 03 '21 at 21:56
  • This is SICK!!! Thank you, very convenient to use! – GooDeeJAY Jun 13 '22 at 12:57
  • Nope. Not working. – Hitesh Kumar Saini Dec 28 '22 at 06:41
179

Each commit is associated with two dates, the committer date and the author date. You can view these dates with:

git log --format=fuller

If you want to change the author date and the committer date of the last 6 commits, you can simply use an interactive rebase :

git rebase -i HEAD~6

.

pick c95a4b7 Modification 1
pick 1bc0b44 Modification 2
pick de19ad3 Modification 3
pick c110e7e Modification 4
pick 342256c Modification 5
pick 5108205 Modification 6

# Rebase eadedca..5108205 onto eadedca (6 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit

For all commits where you want to change the date, replace pick by edit (or just e), then save and quit your editor.

You can now amend each commit by specifying the author date and the committer date in ISO-8601 format:

GIT_COMMITTER_DATE="2017-10-08T09:51:07" git commit --amend --date="2017-10-08T09:51:07"

The first date is the commit date, the second one is the author date.

Then go to the next commit with :

git rebase --continue

Repeat the process until you amend all your commits. Check your progression with git status.

mat101
  • 170
  • 1
  • 1
  • 11
Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
  • 1
    I followed this and ended up on a 'detatched head'! – Simon H Apr 03 '17 at 06:16
  • 1
    @Simon H I've tested again my answer and it works well. You should have typed something different or you were already in a detached head. If you want to come back from a detached head do a `git checkout name-of-current-branch`. – Ortomala Lokni Apr 03 '17 at 07:21
  • 11
    This is the best and the easiest answer. Small tip: use `--no-edit ` in `git commit --amend --no-edit --date=2017-10-08T09:51:07` to keep the old commit message. – Mariusz Pawelski Apr 16 '18 at 11:16
  • 2
    You might also want to update `GIT_COMMITTER_DATE` as described here http://eddmann.com/posts/changing-the-timestamp-of-a-previous-git-commit/ – smihael Apr 26 '18 at 16:11
  • 2
    @smihael Thanks for the link. I've included your suggestion in my answer. – Ortomala Lokni Jul 31 '18 at 19:57
  • 2
    Great answer due to `git log --format=fuller` and the ability to change both dates in one command. – John Leuenhagen Feb 23 '20 at 10:08
  • 1
    Thank you very much, it helped me to better understand how the "rebase" works – JxDarkAngel May 18 '22 at 06:40
133
git commit --amend --date="now"
Harald Nordgren
  • 11,693
  • 6
  • 41
  • 65
112

How to Edit Multiple Commit Dates

Other answers aren't very convenient for editing several commit dates. I've come back to this question after a few years to share a technique.

To change the dates of the last 4 commits:

git rebase -i HEAD~4

Edit the rebase as follows, inserting exec lines to modify dates as needed:

pick 4ca564e Do something
exec git commit --amend --no-edit --date "1 Oct 2019 12:00:00 PDT"
pick 1670583 Add another thing
exec git commit --amend --no-edit --date "2 Oct 2019 12:00:00 PDT"
pick b54021c Add some tests
exec git commit --amend --no-edit --date "3 Oct 2019 12:00:00 PDT"
pick e8f6653 Fix the broken thing
exec git commit --amend --no-edit --date "4 Oct 2019 12:00:00 PDT"

Update (Sep. 2021):

If you want to see the original commit date in the rebase instruction list (Git 2.6+):

git config --add rebase.instructionFormat "[%ai] %s"

Then you'll see something like

pick 4f5a371f [2021-09-08 02:56:50 -0700] Add npm events
pick 67937227 [2021-09-09 03:05:42 -0700] Fixup
Matt Montag
  • 7,105
  • 8
  • 41
  • 47
  • 1
    Is it possible to use current date/time as parameter? – qwerty Dec 08 '19 at 18:55
  • 2
    Re. 'Is it possible to use current date/time as parameter?': "now" is understood as a valid date, so the exec lines above would become `exec git commit --amend --no-edit --date "now"` – Andrew Richards May 17 '20 at 19:01
  • As a addition I wrote a bash script which combines this answer (multiple commits) with the accepted answer (filter): https://gist.github.com/pixelbrackets/e2c2b451b77516e69377ecb4fd6f3a0d – pixelbrackets Sep 02 '20 at 08:19
  • for current date, you can do `exec git commit --amend --reset-author --no-edit` – sziraqui Feb 23 '22 at 09:38
47

Building on theosp's answer, I wrote a script called git-cdc (for change date commit) that I put in my PATH.

The name is important: git-xxx anywhere in your PATH allows you to type:

git xxx
# here
git cdc ... 

That script is in bash, even on Windows (since Git will be calling it from its msys environment)

#!/bin/bash
# commit
# date YYYY-mm-dd HH:MM:SS

commit="$1" datecal="$2"
temp_branch="temp-rebasing-branch"
current_branch="$(git rev-parse --abbrev-ref HEAD)"

date_timestamp=$(date -d "$datecal" +%s)
date_r=$(date -R -d "$datecal")

if [[ -z "$commit" ]]; then
    exit 0
fi

git checkout -b "$temp_branch" "$commit"
GIT_COMMITTER_DATE="$date_timestamp" GIT_AUTHOR_DATE="$date_timestamp" git commit --amend --no-edit --date "$date_r"
git checkout "$current_branch"
git rebase  --autostash --committer-date-is-author-date "$commit" --onto "$temp_branch"
git branch -d "$temp_branch"

With that, you can type:

git cdc @~ "2014-07-04 20:32:45"

That would reset author/commit date of the commit before HEAD (@~) to the specified date.

git cdc @~ "2 days ago"

That would reset author/commit date of the commit before HEAD (@~) to the same hour, but 2 days ago.


Ilya Semenov mentions in the comments:

For OS X you may also install GNU coreutils (brew install coreutils), add it to PATH (PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH") and then use "2 days ago" syntax.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    For me this only worked with quoting the date and the time into one quote: `git cdc @~ "2014-07-04 20:32:45` otherwise it would not recognize the time and hence obtain time 00:00:00 (it becomes the third argument). – peschü Nov 12 '14 at 09:40
  • 3
    For OS X you may also install GNU coreutils (`brew install coreutils`), add it to PATH (`PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"`) and then use "2 days ago" syntax. – Ilya Semenov Dec 04 '15 at 10:26
  • 2
    @IlyaSemenov Interesting. I have included your comment in the answer for more visibility. – VonC Dec 04 '15 at 11:47
  • I am trying to use your first example but I keep getting "fatal: invalid date format:". What date format is Mac OS X expecting? –  Jun 11 '16 at 19:25
  • @usbsnowcrash not sure on mac. Does the second example "`2 days ago`" work? – VonC Jun 11 '16 at 19:51
  • I installed coreutils and the second one worked for me –  Jun 12 '16 at 16:13
45

After reading all the answers I came up with a more succinct and convenient way of editing the date of multiple commits at once without the need of rebasing interactively:

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

It changes both the committer and author dates.

Pere Joan Martorell
  • 2,608
  • 30
  • 29
  • 2
    I liked your answer, I needed all local commits so I did: `git rebase origin/main --exec "git commit --amend --reset-author --no-edit"` – A1rPun Jun 14 '21 at 13:48
  • Great solution. But I experience a strange issue. When I change all the dates and push it to a remote branch with `git push -f -u origin bugfix` and see it on Bitbucket page it still contains the previous commit dates. However it's reproducible not only with this solution but with others too. It looks like Git still holds the previous dates somewhere. – ka3ak Nov 10 '22 at 14:14
  • 1
    @ka3ak I see a commit has 2 dates/timestamps, `AuthorDate` and `CommitDate`, which can be shown using `git log --pretty=fuller` command, as explained [here](https://learn.microsoft.com/en-us/azure/devops/repos/git/git-dates?view=azure-devops). I guess Bitbucket shows the `CommitDate` instead of the `AuthorDate`. In this particular case I suggest using this command (where `t` is the date you want to set):`t='2017-09-01 12:34:56'; GIT_COMMITTER_DATE="$t" git commit --amend --no-edit --date="$t"`. This command will change both the `AuthorDate` and the `CommitDate`. – Pere Joan Martorell Nov 16 '22 at 16:18
  • @PereJoanMartorell I'll try. But in the meantime I used the following approach: While being on branch `bugfix` I executed `git checkout -b bugfix_copy; git reset bugfix; git rebase ...(change commit dates) ; git push origin +bugfix_copy:bugfix`. After the last command even Bitbucket showed current time for each commit on its page. I used this approach also because I wanted to keep the branch `bugfix` whose commit dates would be unchanged. – ka3ak Nov 17 '22 at 17:20
  • Contrary to what the answer claims, this does not change both committer and author dates. I wanted to edit in a change to make it so but "there are too many pending edits, please try again later." Here is an amended solution that follows the same approach: `t='2023-06-01T13:12:04'; git rebase HEAD~2 --exec "GIT_COMMITTER_DATE='$t' git commit --amend --no-edit --date '$t'"`. Replace date string with `now` or whatever date you want to use instead. – Chrisuu Jun 02 '23 at 18:59
24

To change both the author date and the commit date:

GIT_COMMITTER_DATE="Wed Sep 23 9:40 2015 +0200" git commit --amend --date "Wed Sep 23 9:40 2015 +0200"
Jan H
  • 4,287
  • 5
  • 24
  • 34
  • 3
    I’d just add `--no-edit` to this command, to avoid changing the commit message. Good job! – fitojb Jan 01 '22 at 01:52
24

I created this npm package to change date of old commits.

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

Sample Usage:

npm install -g git-change-date
cd [your-directory]
git-change-date

You will be prompted to choose the commit you want to modify then to enter the new date.

If you want to change a commit by specific hash run this git-change-date --hash=[hash]

  • I just wanted to say that this is great and worked beautifully. Thank you, you saved me a great deal of time! – panza Apr 26 '20 at 14:35
  • 1
    @Kareem Elbahrawy I am getting following error: Please help me (Git + Windows 10) Command failed: cd C:\D\Projects\Git\xx-xx && git filter-branch -f --env-filter 'if [ $GIT_COMMIT = xxxxxx ] then export GIT_AUTHOR_DATE="Wed Jan 27 16:00:00 2021 +0530" export GIT_COMMITTER_DATE="Wed Jan 27 16:00:00 2021 +0530" fi' fatal: $GIT_COMMIT: no such path in the working tree. Use 'git -- ...' to specify paths that do not exist locally. – Rikin Patel Feb 01 '21 at 06:47
  • I just wanted to say that it stuck on simple count = 3 and modifying second commit. the error is stdout maxBuffer length exceeded. So I don't recommend this package. Just avoid it. – Alexey Sh. Jul 27 '23 at 07:20
22

if it is previous last commit.

git rebase  -i HEAD~2
git commit --amend --date=now

if you already push to orgin and can force use:

git push --force 

if you can't force the push and if it is pushed, you can't change the commit! .

Sérgio
  • 6,966
  • 1
  • 48
  • 53
22

The most simple way to modify the date and time of the last commit

git commit --amend --date="12/31/2021 @ 14:00"
Thomas Gotwig
  • 3,659
  • 3
  • 17
  • 15
  • 3
    This changes only the `AuthorDate`, not the `CommitDate`. You can see that it does not work by typing `git log --format=fuller`. – Rafał Sroka Jun 17 '21 at 13:52
  • 8
    To also change the commit date after this: `git rebase --committer-date-is-author-date HEAD^` – krubo May 18 '22 at 06:22
19

Here is a convenient alias that changes both commit and author times of the last commit to a time accepted by date --date:

[alias]
    cd = "!d=\"$(date -d \"$1\")\" && shift && GIT_COMMITTER_DATE=\"$d\" \
            git commit --amend --date \"$d\""

Usage: git cd <date_arg>

Examples:

git cd now  # update the last commit time to current time
git cd '1 hour ago'  # set time to 1 hour ago

Edit: Here is a more-automated version which checks that the index is clean (no uncommitted changes) and reuses the last commit message, or fails otherwise (fool-proof):

[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!"
eold
  • 5,972
  • 11
  • 56
  • 75
19

The following bash function will change the time of any commit on the current branch.

Be careful not to use if you already pushed the commit or if you use the commit in another branch.

# rewrite_commit_date(commit, date_timestamp)
#
# !! Commit has to be on the current branch, and only on the current branch !!
# 
# Usage example:
#
# 1. Set commit 0c935403 date to now:
#
#   rewrite_commit_date 0c935403
#
# 2. Set commit 0c935403 date to 1402221655:
#
#   rewrite_commit_date 0c935403 1402221655
#
rewrite_commit_date () {
    local commit="$1" date_timestamp="$2"
    local date temp_branch="temp-rebasing-branch"
    local current_branch="$(git rev-parse --abbrev-ref HEAD)"

    if [[ -z "$date_timestamp" ]]; then
        date="$(date -R)"
    else
        date="$(date -R --date "@$date_timestamp")"
    fi

    git checkout -b "$temp_branch" "$commit"
    GIT_COMMITTER_DATE="$date" git commit --amend --date "$date"
    git checkout "$current_branch"
    git rebase "$commit" --onto "$temp_branch"
    git branch -d "$temp_branch"
}
Thomas N
  • 35
  • 1
  • 6
theosp
  • 7,439
  • 3
  • 23
  • 24
  • 1
    You have a bug in there: `if [[ -z "$commit" ]]` -> `if [[ -z "$date_timestamp" ]]` – blueFast Apr 14 '16 at 08:31
  • Nice! I would recommend setting `GIT_COMMITTER_DATE=` at the end of the method to prevent any further manual commits to keep the date specified. – scharette Sep 26 '19 at 17:56
  • @loopkin, GIT_COMMITTER_DATE is set just for the "git commit" command so no need to clear it afterwards – nimrodm Apr 26 '20 at 05:05
  • @nimrodm, I just tested again and you are correct. Thanks for pointing that out. – scharette Apr 27 '20 at 13:44
13

Information from July 2022:

This one working amazing with current timestamp:

git commit --amend --date=now --no-edit

And this one - with any date format:

git commit --amend --date="Mon Jul 25 10:37:36 2022 +0300" --no-edit
yozhik
  • 4,644
  • 14
  • 65
  • 98
11

If you want to get the exact date of another commit (say you rebase edited a commit and want it to have the date of the original pre-rebase version):

git commit --amend --date="$(git show -s --format=%ai a383243)"

This corrects the date of the HEAD commit to be exactly the date of commit a383243 (include more digits if there are ambiguities). It will also pop up an editor window so you can edit the commit message.

That's for the author date which is what you care for usually - see other answers for the committer date.

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
11

If commit not yet pushed then I can use something like that:

git commit --amend --date=" Wed Mar 25 10:05:44 2020 +0300"

after that git bash opens editor with the already applied date so you need just to save it by typing in the VI editor command mode ":wq" and you can push it

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Alex Cumarav
  • 537
  • 5
  • 7
  • 2
    Just adding to the nice answer: if you don't want to edit the commit message (if you just want to change the commit date), use the `--no-edit` option. – Antônio Medeiros Jan 15 '20 at 01:53
  • Also, if the commit has already been pushed, you can still push the changed commit using `git push -f` (forced update). That may have side effects, though. (especially if many people have local clones of the repository) – Antônio Medeiros Jan 15 '20 at 01:56
9

Set the date of the last commit to the current date

GIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"

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"

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"

Source: https://codewithhugo.com/change-the-date-of-a-git-commit/

evolved
  • 1,850
  • 19
  • 40
8

If you want to perform the accepted answer (https://stackoverflow.com/a/454750/72809) in standard Windows command line, you need the following command:

git filter-branch -f --env-filter "if [ $GIT_COMMIT = 578e6a450ff5318981367fe1f6f2390ce60ee045 ]; then export GIT_AUTHOR_DATE='2009-10-16T16:00+03:00'; export GIT_COMMITTER_DATE=$GIT_AUTHOR_DATE; fi"

Notes:

  • It may be possible to split the command over multiple lines (Windows supports line splitting with the carret symbol ^), but I didn't succeed.
  • You can write ISO dates, saving a lot of time finding the right day-of-week and general frustration over the order of elements.
  • If you want the Author and Committer date to be the same, you can just reference the previously set variable.

Many thanks go to a blog post by Colin Svingen. Even though his code didn't work for me, it helped me find the correct solution.

Community
  • 1
  • 1
Peter
  • 2,874
  • 2
  • 31
  • 42
7

For those using Powershell

git rebase DESIRED_REF^ -i

$commitDateString = "2020-01-22T22:22:22"
$env:GIT_COMMITTER_DATE = $commitDateString
git commit --amend --date $commitDateString
$env:GIT_COMMITTER_DATE = ""

git rebase --continue

Credit to https://mnaoumov.wordpress.com/2012/09/23/git-change-date-of-commit/

K.Novichikhin
  • 339
  • 2
  • 8
6

For updating the date of the 5 last commits to the current date (this method doesn't allow to update the initial commit):

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

For all commits after commit 95f5074…15074db2:

git rebase 95f5074…15074db2 --exec "git commit --amend --no-edit --date 'now'"

For all commits (including the initial commit):

git rebase --root --exec "git commit --amend --no-edit --date 'now'"

Add -i for the interactive mode.

Run git log --format=fuller --show-signature to validate the changes.

Run git push -f to update the remote repository (⚠️Danger zone)

There are implications. For instance:

  • Commit IDs will change, so you will have to recreate tags
  • You will lose original signatures
  • This will use your .gitconfig, this means your key will be used for signing commits (if Git is configured to sign commits)
  • Instead of changing the date of the last 3 commits, is it possible to change the --date and env variable GIT_COMMITTER_DATE of only the THIRD commit with a single command? – the_RR Apr 08 '22 at 18:16
5

Edit the author date and the commit date of the last 3 commits:

git rebase -i HEAD~3 --committer-date-is-author-date --exec "git commit --amend --no-edit --date=now"

The --exec command is appended after each line in the rebase and you can choose the author date with the --date=..., the committer date will be the same of author date.

gfdevelop
  • 187
  • 2
  • 7
5
GIT_COMMITTER_DATE="Sun Nov 20 21:02 2022 +0530" git commit --amend --no-edit --date="Sun Nov 20 21:02 2022 +0530"
git push -f

Works everytime.

Ankit Shubham
  • 2,989
  • 2
  • 36
  • 61
2

I wanted to make sure that I update my code’s copyright comments at precisely midnight, and I didn’t want to risk a tiny delay with at or cron. So, I commited the code and then:

GIT_COMMITTER_DATE="Fri Jan 1 00:00:00 2021 +1000" git commit --amend --no-edit --date="Fri Jan 1 00:00:00 2021 +1000"

(Or perhaps even set the UTC offset to 0? Decisions… ) Now I can push!

Happy new year, everybody

Jens
  • 8,423
  • 9
  • 58
  • 78
2

In addition to Matt Montag's answer:

If you need to reset timestamp to current time after rebase command

git rebase -i HEAD~2

you can use one of these options

pick 4ca564e Do something
exec git commit --amend --no-edit --date=now
pick 1670583 Add another thing
exec git commit --amend --no-edit --reset-author

Both will work

mechnicov
  • 12,025
  • 4
  • 33
  • 56
1

There are already many great answers, but when I want to change date for multiple commits in one day or in one month, I don't find a proper answer. So I create a new script for this with explaintion, hope it will help someone:

#!/bin/bash

# change GIT_AUTHOR_DATE for commit at Thu Sep 14 13:39:41 2017 +0800
# you can change the data_match to change all commits at any date, one day or one month
# you can also do the same for GIT_COMMITTER_DATE

git filter-branch --force --env-filter '

date_match="^Thu, 14 Sep 2017 13+"              

# GIT_AUTHOR_DATE will be @1505367581 +0800, Git internal format 
author_data=$GIT_AUTHOR_DATE;                   
author_data=${author_data#@}                  
author_data=${author_data% +0800}                # author_data is 1505367581     

oneday=$((24*60*60))

# author_data_str will be "Thu, 14 Sep 2017 13:39:41 +0800", RFC2822 format
author_data_str=`date -R -d @$author_data`      

if [[ $author_data_str =~ $date_match ]];
then
    # remove one day from author_data
    new_data_sec=$(($author_data-$oneday))
    # change to git internal format based on new_data_sec
    new_data="@$new_data_sec +0800"             
    export GIT_AUTHOR_DATE="$new_data"
fi
' --tag-name-filter cat -- --branches --tags

The date will be changed:

AuthorDate: Wed Sep 13 13:39:41 2017 +0800
1

I have recently needed this and made my own script looking a lot like git-redate

However my scripts does the minimal modifications and takes a lot less time to rewrite (if you need to update) many commits as it does them all at once

change_git_history

Actually this allows to change commit messages too

Explaination:

The scripts concatenates a bunch of bash if-expression looking like so

Here are the ones modifiying the commit date

if [ "$GIT_COMMIT" = "$com_hash" ]; # com is commit
then
    export GIT_AUTHOR_DATE="$com_date";
    export GIT_COMMITTER_DATE="$com_date";
fi;

Here are the ones modifiying the commit message:

if [ true = false ]; # impossible
then
    : # pass
elif [ "$GIT_COMMIT" = "$com_hash" ];
then
    sed 's/.*/$com_msg_esc/g' # replace content with new content
else
    cat - # returns previous content
fi;

And we push all the update using

git filter-branch -f \
    --env-filter "$UPDATES" \
    --msg-filter "$MESSAGES" \
    -- "$REV"

(doc is here filter-branch man)

Xiaojiba
  • 116
  • 1
  • 8
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/30802372) – Simas Joneliunas Jan 16 '22 at 01:38
  • Thanks @SimasJoneliunas I've update the answer :) – Xiaojiba Jan 18 '22 at 13:25
0

TL;DR: Matching dates + re-creating GPG signatures

(Comment/edit if you know a workaround for the stripping to preserve the original signature.)


I'll bump this old thread because a feature of signing commits has been introduced and all of these git filter-branch and likes basically strip the signatures as specified in the docs:

... If the tag has a signature attached, the signature will be stripped. It is by definition impossible to preserve signatures. ... (source: --tag-name-filter )

But it'll also break the pretty Verified badge on a GitHub commit (and in other Git hosting places if implemented in the same way), so this will fix that as well. Partially.

Afaik it's not possible to mangle a (GPG) signature through git command in a way that it also contains the date of a commit instead of the date of signing in a simple way and therefore even if the dates for authoring and commit are moved, it'll still be the present date, example:

commit <hash>
gpg: Signature made Sun 25 Jul 2021 00:00:00 PM TZ
gpg:                using TYPE key KEY
gpg: Good signature from "Signer <email@domain.tld>"
Author:     Author <email@domain.tld>
AuthorDate: Sat Jan 1 00:00:00 2000 +0000
Commit:     Author <email@domain.tld>
CommitDate: Sat Jan 1 00:00:00 2000 +0000

So imagine you have a repo you want to sign from a certain commit (I'll go for the root commit; not recommended if somebody else works on the repo). The documentation for git commit says it pulls data from env vars too, if present, thus we have a place to put the input to.

To retrieve the data (can be set with git commit --date=...) we can take a look at git show --format=%ad, so for a raw date string that would be:

git show --format=%ad --no-patch
# Sat Jan 1 00:00:00 2000 +0000

So we have:

  • point of start
  • raw date string for each commit
  • GIT_COMMITTER_DATE to match the dates (author -> committer)

For rebasing let's do this:

git rebase --root <branch-name> --keep-empty --interactive

which will go for the root commit of a branch <branch-name>, preserve any empty commits created with git commit -m "empty" --allow-empty and ask you which commits to modify. There you change the desired commits from pick to edit (for my case that'd be marking all of them as edit), then you'll be dropped into a detached HEAD commit and from here the fun begins.

# or "while :"
while true
do
    GIT_COMMITTER_DATE=$(git show --format=%ad --no-patch) \
        git commit --amend --gpg-sign --no-edit --allow-empty
    git rebase --continue
done

(if you do not have user.signingkey specified, use --gpg-sign=<fingerprint>)

This will go through each of the edit-marked commit, set the committer's date to match the author's date, keep any empty commit, won't touch the overall patch body and will add a signature with a date of when the command was executed.

Once you see fatal: No rebase in progress? press Ctrl-C to stop the loop and check the logs to confirm that the dates match and the signatures are present everywhere with:

git log --pretty=fuller --show-signature

If everything is okay in the logs, simply issue git push --force and you're done. Now you should see that Verified badge for each commit.

Example with a real history tree. GitHub doesn't seem to care about the signature's date (no reference anywhere), but it'll still be present in the git log.

Peter Badida
  • 11,310
  • 10
  • 44
  • 90