如何修改现有的、未推送的提交?描述一种修改尚未推送到上游的先前提交消息的方法。新消息继承原始提交的时间戳。这似乎合乎逻辑,但有没有办法也重新设定时间呢?
当前回答
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
每次工作。
其他回答
基于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天前"语法。
如果你想获得另一个提交的确切日期(假设你重新编辑了一个提交,并希望它具有原始的预重新编辑版本的日期):
git commit --amend --date="$(git show -s --format=%ai a383243)"
这将更正HEAD提交的日期,使其与提交a383243的日期完全一致(如果有歧义,则包括更多数字)。它还会弹出一个编辑器窗口,以便您可以编辑提交消息。
这是作者日期,这是你通常关心的-查看提交者日期的其他答案。
修改上次提交的日期和时间的最简单方法
git commit --amend --date="12/31/2021 @ 14:00"
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
每次工作。
编辑作者日期和最近3次提交的提交日期:
git rebase -i HEAD~3 --committer-date-is-author-date --exec "git commit --amend --no-edit --date=now"
——exec命令附加在rebase中的每一行之后,您可以使用——date=…,投稿日期与作者日期一致。
推荐文章
- 如何将git配置存储为存储库的一部分?
- 如何修改GitHub拉请求?
- 如何在Github和本地删除最后n次提交?
- 我如何调试git/git-shell相关的问题?
- 错误:无法使用rebase进行拉取:您有未分阶段的更改
- Git隐藏未缓存:如何把所有未分期的变化?
- 真实的恶魔
- Mercurial:如何修改上次提交?
- 如何从另一个分支获得更改
- Git:权限被拒绝(publickey)致命-无法从远程存储库读取。克隆Git存储库时
- git reflog和log有什么区别?
- git推挂在Total line之后
- 重命名git子模块
- 结合Git存储库的前两次提交?
- Xcode 6 gitignore文件应该包括什么?