如何修改现有的、未推送的提交?描述一种修改尚未推送到上游的先前提交消息的方法。新消息继承原始提交的时间戳。这似乎合乎逻辑,但有没有办法也重新设定时间呢?
当前回答
2022年7月起:
这是一个惊人的工作与当前时间戳:
git commit --amend --date=now --no-edit
还有这个——任何日期格式:
git commit --amend --date="Mon Jul 25 10:37:36 2022 +0300" --no-edit
其他回答
下面是一个方便的别名,它将上一次提交的提交时间和作者时间都更改为date接受的时间——date:
[alias]
cd = "!d=\"$(date -d \"$1\")\" && shift && GIT_COMMITTER_DATE=\"$d\" \
git commit --amend --date \"$d\""
使用方法:git cd <date_arg>
例子:
git cd now # update the last commit time to current time
git cd '1 hour ago' # set time to 1 hour ago
编辑: 下面是一个更自动化的版本,它检查索引是否干净(没有未提交的更改),并重用最后的提交消息,否则会失败(万无一失):
[alias]
cd = "!d=\"$(date -d \"$1\")\" && shift && \
git diff-index --cached --quiet HEAD --ignore-submodules -- && \
GIT_COMMITTER_DATE=\"$d\" git commit --amend -C HEAD --date \"$d\"" \
|| echo >&2 "error: date change failed: index not clean!"
修改作者日期和提交日期:
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="now"
我最近需要这个,并使我自己的脚本看起来很像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"
(医生在这里)
修改上次提交的日期和时间的最简单方法
git commit --amend --date="12/31/2021 @ 14:00"