如何修改现有的、未推送的提交?描述一种修改尚未推送到上游的先前提交消息的方法。新消息继承原始提交的时间戳。这似乎合乎逻辑,但有没有办法也重新设定时间呢?
当前回答
下面是一个方便的别名,它将上一次提交的提交时间和作者时间都更改为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!"
其他回答
我为此写了一个脚本和Homebrew包。超级容易安装,你可以在GitHub PotatoLabs/git-redate页面上找到它。
语法:
git redate -c 3
你只需要运行git redate,你就可以在vim中编辑最近5次提交的所有日期(还有一个-c选项来决定你想要返回多少次提交,它默认是5)。如果你有任何问题,评论或建议,请告诉我!
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="$(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/
将最近5次提交的日期更新到当前日期(此方法不允许更新初始提交):
git rebase HEAD~5 --exec "git commit --amend --no-edit --date 'now'"
对于提交95f5074…15074db2后的所有提交:
git rebase 95f5074…15074db2 --exec "git commit --amend --no-edit --date 'now'"
对于所有的提交(包括初始提交):
git rebase --root --exec "git commit --amend --no-edit --date 'now'"
为交互模式添加-i。
执行git log——format=fuller——show-signature命令验证修改。
运行git push -f更新远程存储库(⚠️危险区域)
这是有影响的。例如:
提交id将会改变,因此您必须重新创建标记 您将失去原始签名 这将使用您的.gitconfig,这意味着您的密钥将用于签名提交(如果Git被配置为签名提交)
修改上次提交的日期和时间的最简单方法
git commit --amend --date="12/31/2021 @ 14:00"