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


当前回答

编辑作者日期和最近3次提交的提交日期:

git rebase -i HEAD~3 --committer-date-is-author-date --exec "git commit --amend --no-edit --date=now"

——exec命令附加在rebase中的每一行之后,您可以使用——date=…,投稿日期与作者日期一致。

其他回答

编辑作者日期和最近3次提交的提交日期:

git rebase -i HEAD~3 --committer-date-is-author-date --exec "git commit --amend --no-edit --date=now"

——exec命令附加在rebase中的每一行之后,您可以使用——date=…,投稿日期与作者日期一致。

如果是上一次提交。

git rebase  -i HEAD~2
git commit --amend --date=now

如果你已经推送到orgin并且可以强制使用:

git push --force 

如果你不能强制推送,如果它被推送,你就不能改变提交!.

我创建了这个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 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="Sun Nov 20 21:02 2022 +0530" git commit --amend --no-edit --date="Sun Nov 20 21:02 2022 +0530"
git push -f

每次工作。