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


当前回答

修改作者日期和提交日期:

GIT_COMMITTER_DATE="Wed Sep 23 9:40 2015 +0200" git commit --amend --date "Wed Sep 23 9:40 2015 +0200"

其他回答

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

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

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

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

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

下面的bash函数将更改当前分支上任何提交的时间。

如果您已经推送了提交,或者在另一个分支中使用提交,请注意不要使用。

# 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"
}

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日志中。

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

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/