如何修改现有的、未推送的提交?描述一种修改尚未推送到上游的先前提交消息的方法。新消息继承原始提交的时间戳。这似乎合乎逻辑,但有没有办法也重新设定时间呢?


当前回答

将最后一次提交的日期设置为当前日期

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"

将任意提交的日期设置为任意或当前日期

还原到之前所述的提交和停止修改:

Git rebase <commit-hash>^ -i 将pick替换为e (edit)并提交(第一个) 退出编辑器(在VIM中,ESC后跟:wq) :

GIT_COMMITTER_DATE="$(date)" git commit - modify -no-edit -date "$(date)" GIT_COMMITTER_DATE="Mon 20 Aug 2018 20:19:19 BST" git commit - modify -no-edit -date "Mon 20 Aug 2018 20:19:19 BST"

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

其他回答

我最近需要这个,并使我自己的脚本看起来很像git-redate

然而,我的脚本做了最小的修改,需要更少的时间来重写(如果你需要更新)许多提交,因为它一次都做了

change_git_history

实际上,这也允许更改提交消息

解释:

脚本连接了一堆bash if-expression,就像这样

这些是修改提交日期的

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

下面是修改提交消息的代码:

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;

我们用

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

(医生在这里)

如果commit还没有被推送,那么我可以使用这样的东西:

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

之后,git bash打开编辑器,其中包含已经应用的日期,所以你只需要在VI编辑器命令模式中输入“:wq”来保存它,然后你可以推送它

如果你想在标准的Windows命令行中执行接受的答案(https://stackoverflow.com/a/454750/72809),你需要以下命令:

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"

注:

可以将命令拆分为多行(Windows支持用斜杠符号^拆分行),但我没有成功。 您可以编写ISO日期,从而节省大量查找合适的星期几的时间,避免在元素顺序方面遇到麻烦。 如果您希望Author和Committer日期相同,则只需引用前面设置的变量即可。

非常感谢Colin Svingen的博客文章。尽管他的代码对我不起作用,但它帮助我找到了正确的解决方案。

对于那些使用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

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

2022年7月起:

这是一个惊人的工作与当前时间戳:

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

还有这个——任何日期格式:

git commit --amend --date="Mon Jul 25 10:37:36 2022 +0300" --no-edit