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


当前回答

TL;DR:匹配日期+重新创建GPG签名

(如果你知道一个可以保留原始签名的方法,请评论/编辑。)


我将碰到这个旧线程,因为签名提交的特性已经引入,所有这些git过滤器分支和喜欢基本上剥离了文档中指定的签名:

... 如果标签附加了签名,则签名将被剥离。根据定义,不可能保存签名. ...(来源:——tag-name-filter)

但它也会破坏GitHub提交上漂亮的验证徽章(如果以同样的方式实现,在其他Git托管位置也会如此),所以这也会修复这个问题。部分。

Afaik,不可能通过git命令来破坏一个(GPG)签名,它也包含了提交的日期,而不是简单的签名日期,因此即使编辑和提交的日期被移动了,它仍然是当前的日期,例如:

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

假设你有一个回购,你想从某个commit(我用根commit;如果其他人负责回购,则不建议使用)。git提交的文档说,如果存在,它也会从env vars中提取数据,因此我们有一个地方可以输入。

要检索数据(可以使用git commit——date=…设置),我们可以查看git show——format=%ad,因此对于原始日期字符串,它将是:

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

所以我们有:

起点 每次提交的原始日期字符串 GIT_COMMITTER_DATE来匹配日期(author -> committer)

对于重基,我们这样做:

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

这将用于一个分支<branch-name>的根提交,保留任何用git commit -m "empty"——allow-empty创建的空提交,并询问你要修改哪个提交。在这里,您可以将所需的提交从pick更改为edit(对于我的情况,将所有这些都标记为edit),然后您将被放入一个分离的HEAD提交,从这里开始乐趣。

# 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

(如果您没有用户名。指定签名密钥,使用——gpg-sign=<指纹>)

这将遍历每个编辑标记的提交,将提交者的日期设置为与作者的日期匹配,保留任何空提交,不会触及整个补丁体,并将添加带有命令执行日期的签名。

一旦你看到致命:没有调整进度?按Ctrl-C停止循环,检查日志,确认日期匹配,签名无处不在:

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

如果日志中一切正常,只需发出git push——force,就完成了。现在您应该看到每个提交的Verified徽章。

一个真实历史树的例子。GitHub似乎并不关心签名的日期(在任何地方都没有引用),但它仍然会出现在git日志中。

其他回答

基于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天前"语法。

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

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

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

在阅读了所有的答案后,我想出了一个更简洁和方便的方法来编辑多次提交的日期,而不需要交互式地重基:

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

它同时更改提交者和作者的日期。

如何编辑多个提交日期

其他答案对于编辑多个提交日期不太方便。几年之后,我又回到这个问题上来分享一个技巧。

更改最近4次提交的日期。

git rebase -i HEAD~4

编辑rebase如下,插入exec行以根据需要修改日期:

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"

更新(2021年9月):

如果你想在rebase指令列表(Git 2.6+)中查看原始提交日期:

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

然后你会看到

pick 4f5a371f [2021-09-08 02:56:50 -0700] Add npm events
pick 67937227 [2021-09-09 03:05:42 -0700] Fixup

除了Matt Montag的回答:

如果需要在rebase命令后将时间戳重置为当前时间

git rebase -i HEAD~2

您可以使用这些选项之一

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

两者都可以