如何修改现有的、未推送的提交?描述一种修改尚未推送到上游的先前提交消息的方法。新消息继承原始提交的时间戳。这似乎合乎逻辑,但有没有办法也重新设定时间呢?
当前回答
2022年7月起:
这是一个惊人的工作与当前时间戳:
git commit --amend --date=now --no-edit
还有这个——任何日期格式:
git commit --amend --date="Mon Jul 25 10:37:36 2022 +0300" --no-edit
其他回答
如何编辑多个提交日期
其他答案对于编辑多个提交日期不太方便。几年之后,我又回到这个问题上来分享一个技巧。
更改最近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_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
每次工作。
在阅读了所有的答案后,我想出了一个更简洁和方便的方法来编辑多次提交的日期,而不需要交互式地重基:
git rebase HEAD~4 --exec "git commit --amend --no-edit --date 'now'"
它同时更改提交者和作者的日期。
使用带有env过滤器的git filter-branch,该过滤器为您希望修复的提交的特定散列设置GIT_AUTHOR_DATE和GIT_COMMITTER_DATE。
这将使该哈希值和所有未来哈希值无效。
例子:
如果你想改变提交119f9ecf58069b265ab22f1f97d2b648faf932e0的日期,你可以这样做:
git filter-branch --env-filter \
'if [ $GIT_COMMIT = 119f9ecf58069b265ab22f1f97d2b648faf932e0 ]
then
export GIT_AUTHOR_DATE="Fri Jan 2 21:38:53 2009 -0800"
export GIT_COMMITTER_DATE="Sat May 19 01:01:01 2007 -0700"
fi'
我最近需要这个,并使我自己的脚本看起来很像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 restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别
- GitHub -致命:无法读取用户名https://github.com':没有这样的文件或目录
- 撤消git平分错误
- 使用.gitconfig配置diff工具
- 如何配置Mac OS X术语,使git有颜色?
- Visual Studio Code: .git文件夹/文件隐藏
- 如何使用yyyyMMddHHmmss格式格式化当前时间?
- “node_modules”文件夹应该包含在git存储库中吗
- 为什么git-rebase给了我合并冲突,而我所做的只是压缩提交?
- 当我试图推到原点时,为什么Git告诉我“没有这样的远程‘原点’”?