如何修改现有的、未推送的提交?描述一种修改尚未推送到上游的先前提交消息的方法。新消息继承原始提交的时间戳。这似乎合乎逻辑,但有没有办法也重新设定时间呢?
当前回答
将最近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被配置为签名提交)
其他回答
如果commit还没有被推送,那么我可以使用这样的东西:
git commit --amend --date=" Wed Mar 25 10:05:44 2020 +0300"
之后,git bash打开编辑器,其中包含已经应用的日期,所以你只需要在VI编辑器命令模式中输入“:wq”来保存它,然后你可以推送它
您可以进行交互式的更改,并选择编辑您想更改日期的提交。当rebase进程停止修改你输入的提交时,例如:
git commit --amend --date="Wed Feb 16 14:00 2011 +0100" --no-edit
注:date=now将使用当前时间。
之后,您将继续您的交互rebase。
修改提交日期而不是作者日期:
GIT_COMMITTER_DATE="Wed Feb 16 14:00 2011 +0100" git commit --amend --no-edit
上面的代码行设置了一个环境变量GIT_COMMITTER_DATE,该变量用于修改提交。
一切都在Git Bash中测试。
编辑作者日期和最近3次提交的提交日期:
git rebase -i HEAD~3 --committer-date-is-author-date --exec "git commit --amend --no-edit --date=now"
——exec命令附加在rebase中的每一行之后,您可以使用——date=…,投稿日期与作者日期一致。
我创建了这个npm包来更改旧的提交日期。
https://github.com/bitriddler/git-change-date
示例用法:
npm install -g git-change-date
cd [your-directory]
git-change-date
系统将提示您选择要修改的提交,然后输入新的日期。
如果你想通过指定的散列来更改提交,请运行git-change-date——hash=[hash]
将最后一次提交的日期设置为当前日期
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/