如何修改现有的、未推送的提交?描述一种修改尚未推送到上游的先前提交消息的方法。新消息继承原始提交的时间戳。这似乎合乎逻辑,但有没有办法也重新设定时间呢?
当前回答
下面的bash函数将更改当前分支上任何提交的时间。
如果您已经推送了提交,或者在另一个分支中使用提交,请注意不要使用。
# rewrite_commit_date(commit, date_timestamp)
#
# !! Commit has to be on the current branch, and only on the current branch !!
#
# Usage example:
#
# 1. Set commit 0c935403 date to now:
#
# rewrite_commit_date 0c935403
#
# 2. Set commit 0c935403 date to 1402221655:
#
# rewrite_commit_date 0c935403 1402221655
#
rewrite_commit_date () {
local commit="$1" date_timestamp="$2"
local date temp_branch="temp-rebasing-branch"
local current_branch="$(git rev-parse --abbrev-ref HEAD)"
if [[ -z "$date_timestamp" ]]; then
date="$(date -R)"
else
date="$(date -R --date "@$date_timestamp")"
fi
git checkout -b "$temp_branch" "$commit"
GIT_COMMITTER_DATE="$date" git commit --amend --date "$date"
git checkout "$current_branch"
git rebase "$commit" --onto "$temp_branch"
git branch -d "$temp_branch"
}
其他回答
2022年7月起:
这是一个惊人的工作与当前时间戳:
git commit --amend --date=now --no-edit
还有这个——任何日期格式:
git commit --amend --date="Mon Jul 25 10:37:36 2022 +0300" --no-edit
基于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-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"
(医生在这里)
将最近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被配置为签名提交)
修改作者日期和提交日期:
GIT_COMMITTER_DATE="Wed Sep 23 9:40 2015 +0200" git commit --amend --date "Wed Sep 23 9:40 2015 +0200"
推荐文章
- 如何将git配置存储为存储库的一部分?
- 如何修改GitHub拉请求?
- 如何在Github和本地删除最后n次提交?
- 我如何调试git/git-shell相关的问题?
- 错误:无法使用rebase进行拉取:您有未分阶段的更改
- Git隐藏未缓存:如何把所有未分期的变化?
- 真实的恶魔
- Mercurial:如何修改上次提交?
- 如何从另一个分支获得更改
- Git:权限被拒绝(publickey)致命-无法从远程存储库读取。克隆Git存储库时
- git reflog和log有什么区别?
- git推挂在Total line之后
- 重命名git子模块
- 结合Git存储库的前两次提交?
- Xcode 6 gitignore文件应该包括什么?