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


当前回答

我最近需要这个,并使我自己的脚本看起来很像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"

(医生在这里)

其他回答

2022年7月起:

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

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

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

git commit --amend --date="Mon Jul 25 10:37:36 2022 +0300" --no-edit
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

每次工作。

只需执行git commit - modify -reset-author -no-edit。对于较旧的提交,您可以执行交互式的重基操作,并选择要修改其日期的提交的编辑。

git rebase -i <ref>

然后使用——reset-author和——no-edit修改提交,将作者日期更改为当前日期:

git commit --amend --reset-author --no-edit

最后继续您的互动rebase:

git rebase --continue

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

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 commit --amend --date="now"