如何修改现有的、未推送的提交?描述一种修改尚未推送到上游的先前提交消息的方法。新消息继承原始提交的时间戳。这似乎合乎逻辑,但有没有办法也重新设定时间呢?
当前回答
基于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天前"语法。
其他回答
在一个命令中处理所有这些建议的更好方法是
LC_ALL=C GIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"
这将把最后一次提交的提交日期和作者日期设置为“现在”。
如果commit还没有被推送,那么我可以使用这样的东西:
git commit --amend --date=" Wed Mar 25 10:05:44 2020 +0300"
之后,git bash打开编辑器,其中包含已经应用的日期,所以你只需要在VI编辑器命令模式中输入“:wq”来保存它,然后你可以推送它
在阅读了所有的答案后,我想出了一个更简洁和方便的方法来编辑多次提交的日期,而不需要交互式地重基:
git rebase HEAD~4 --exec "git commit --amend --no-edit --date 'now'"
它同时更改提交者和作者的日期。
下面是一个方便的别名,它将上一次提交的提交时间和作者时间都更改为date接受的时间——date:
[alias]
cd = "!d=\"$(date -d \"$1\")\" && shift && GIT_COMMITTER_DATE=\"$d\" \
git commit --amend --date \"$d\""
使用方法:git cd <date_arg>
例子:
git cd now # update the last commit time to current time
git cd '1 hour ago' # set time to 1 hour ago
编辑: 下面是一个更自动化的版本,它检查索引是否干净(没有未提交的更改),并重用最后的提交消息,否则会失败(万无一失):
[alias]
cd = "!d=\"$(date -d \"$1\")\" && shift && \
git diff-index --cached --quiet HEAD --ignore-submodules -- && \
GIT_COMMITTER_DATE=\"$d\" git commit --amend -C HEAD --date \"$d\"" \
|| echo >&2 "error: date change failed: index not clean!"
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配置存储为存储库的一部分?
- 如何修改GitHub拉请求?
- 如何在Github和本地删除最后n次提交?
- 我如何调试git/git-shell相关的问题?
- 错误:无法使用rebase进行拉取:您有未分阶段的更改
- Git隐藏未缓存:如何把所有未分期的变化?
- 真实的恶魔
- Mercurial:如何修改上次提交?
- 如何从另一个分支获得更改
- Git:权限被拒绝(publickey)致命-无法从远程存储库读取。克隆Git存储库时
- git reflog和log有什么区别?
- git推挂在Total line之后
- 重命名git子模块
- 结合Git存储库的前两次提交?
- Xcode 6 gitignore文件应该包括什么?