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


当前回答

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

每次工作。

其他回答

基于theosp的答案,我写了一个名为git-cdc(用于更改日期提交)的脚本,并将其放在我的PATH中。

这个名字很重要:git-xxx在你的PATH中允许你输入:

git xxx
# here
git cdc ... 

该脚本在bash中,即使在Windows上(因为Git将从其msys环境中调用它)

#!/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"

有了它,你可以输入:

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

这将把HEAD(@~)之前提交的作者/提交日期重置为指定的日期。

git cdc @~ "2 days ago"

这将把HEAD(@~)之前提交的作者/提交日期重置为同一小时,但是是2天前。


Ilya Semenov在评论中提到:

对于OS X,您也可以安装GNU coreutils (brew install coreutils),将其添加到PATH (PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"),然后使用"2天前"语法。

如果你想获得另一个提交的确切日期(假设你重新编辑了一个提交,并希望它具有原始的预重新编辑版本的日期):

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

这将更正HEAD提交的日期,使其与提交a383243的日期完全一致(如果有歧义,则包括更多数字)。它还会弹出一个编辑器窗口,以便您可以编辑提交消息。

这是作者日期,这是你通常关心的-查看提交者日期的其他答案。

修改上次提交的日期和时间的最简单方法

git commit --amend --date="12/31/2021 @ 14:00"
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

每次工作。

编辑作者日期和最近3次提交的提交日期:

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

——exec命令附加在rebase中的每一行之后,您可以使用——date=…,投稿日期与作者日期一致。