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 t
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 toPATH
(PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
) and then use "2 days ago
" syntax.
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"
Afterwards you continue your interactive rebase.
UPDATE (in response to the comment of studgeek): to change the commit date instead of the author date:
GIT_COMMITTER_DATE="Wed Feb 16 14:00 2011 +0100" git commit --amend
The lines above set an environment variable GIT_COMMITTER_DATE which is used in amend commit.
Everything is tested in Git Bash.
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
.
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! .
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
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'