如何修改现有的、未推送的提交?描述一种修改尚未推送到上游的先前提交消息的方法。新消息继承原始提交的时间戳。这似乎合乎逻辑,但有没有办法也重新设定时间呢?
当前回答
如何编辑多个提交日期
其他答案对于编辑多个提交日期不太方便。几年之后,我又回到这个问题上来分享一个技巧。
更改最近4次提交的日期。
git rebase -i HEAD~4
编辑rebase如下,插入exec行以根据需要修改日期:
pick 4ca564e Do something
exec git commit --amend --no-edit --date "1 Oct 2019 12:00:00 PDT"
pick 1670583 Add another thing
exec git commit --amend --no-edit --date "2 Oct 2019 12:00:00 PDT"
pick b54021c Add some tests
exec git commit --amend --no-edit --date "3 Oct 2019 12:00:00 PDT"
pick e8f6653 Fix the broken thing
exec git commit --amend --no-edit --date "4 Oct 2019 12:00:00 PDT"
更新(2021年9月):
如果你想在rebase指令列表(Git 2.6+)中查看原始提交日期:
git config --add rebase.instructionFormat "[%ai] %s"
然后你会看到
pick 4f5a371f [2021-09-08 02:56:50 -0700] Add npm events
pick 67937227 [2021-09-09 03:05:42 -0700] Fixup
其他回答
只需执行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
将最近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被配置为签名提交)
基于theosp的答案,我写了一个名为git-cdc(用于更改日期提交)的脚本,并将其放在我的PATH中。
这个名字很重要:git-xxx在你的PATH中允许你输入:
git xxx
# here
git cdc ...
该脚本在bash中,即使在Windows上(因为Git将从其msys环境中调用它)
#!/bin/bash
# commit
# date YYYY-mm-dd HH:MM:SS
commit="$1" datecal="$2"
temp_branch="temp-rebasing-branch"
current_branch="$(git rev-parse --abbrev-ref HEAD)"
date_timestamp=$(date -d "$datecal" +%s)
date_r=$(date -R -d "$datecal")
if [[ -z "$commit" ]]; then
exit 0
fi
git checkout -b "$temp_branch" "$commit"
GIT_COMMITTER_DATE="$date_timestamp" GIT_AUTHOR_DATE="$date_timestamp" git commit --amend --no-edit --date "$date_r"
git checkout "$current_branch"
git rebase --autostash --committer-date-is-author-date "$commit" --onto "$temp_branch"
git branch -d "$temp_branch"
有了它,你可以输入:
git cdc @~ "2014-07-04 20:32:45"
这将把HEAD(@~)之前提交的作者/提交日期重置为指定的日期。
git cdc @~ "2 days ago"
这将把HEAD(@~)之前提交的作者/提交日期重置为同一小时,但是是2天前。
Ilya Semenov在评论中提到:
对于OS X,您也可以安装GNU coreutils (brew install coreutils),将其添加到PATH (PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"),然后使用"2天前"语法。
如果你想在标准的Windows命令行中执行接受的答案(https://stackoverflow.com/a/454750/72809),你需要以下命令:
git filter-branch -f --env-filter "if [ $GIT_COMMIT = 578e6a450ff5318981367fe1f6f2390ce60ee045 ]; then export GIT_AUTHOR_DATE='2009-10-16T16:00+03:00'; export GIT_COMMITTER_DATE=$GIT_AUTHOR_DATE; fi"
注:
可以将命令拆分为多行(Windows支持用斜杠符号^拆分行),但我没有成功。 您可以编写ISO日期,从而节省大量查找合适的星期几的时间,避免在元素顺序方面遇到麻烦。 如果您希望Author和Committer日期相同,则只需引用前面设置的变量即可。
非常感谢Colin Svingen的博客文章。尽管他的代码对我不起作用,但它帮助我找到了正确的解决方案。
在阅读了所有的答案后,我想出了一个更简洁和方便的方法来编辑多次提交的日期,而不需要交互式地重基:
git rebase HEAD~4 --exec "git commit --amend --no-edit --date 'now'"
它同时更改提交者和作者的日期。