如何修改现有的、未推送的提交?描述一种修改尚未推送到上游的先前提交消息的方法。新消息继承原始提交的时间戳。这似乎合乎逻辑,但有没有办法也重新设定时间呢?
当前回答
除了Matt Montag的回答:
如果需要在rebase命令后将时间戳重置为当前时间
git rebase -i HEAD~2
您可以使用这些选项之一
pick 4ca564e Do something
exec git commit --amend --no-edit --date=now
pick 1670583 Add another thing
exec git commit --amend --no-edit --reset-author
两者都可以
其他回答
下面的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
每次提交都与两个日期相关联,提交者日期和作者日期。你可以用以下方法查看这些日期:
git log --format=fuller
如果你想改变最近6次提交的作者日期和提交者日期,你可以简单地使用交互式rebase:
git rebase -i HEAD~6
.
pick c95a4b7 Modification 1
pick 1bc0b44 Modification 2
pick de19ad3 Modification 3
pick c110e7e Modification 4
pick 342256c Modification 5
pick 5108205 Modification 6
# Rebase eadedca..5108205 onto eadedca (6 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
对于您想要更改日期的所有提交,将pick替换为edit(或只是e),然后保存并退出编辑器。
你现在可以通过指定作者日期和ISO-8601格式的提交日期来修改每次提交:
GIT_COMMITTER_DATE="2017-10-08T09:51:07" git commit --amend --date="2017-10-08T09:51:07"
第一个日期是提交日期,第二个日期是作者日期。
然后进行下一次提交:
git rebase --continue
重复该过程,直到修改所有提交。用git状态检查你的进程。
在一个命令中处理所有这些建议的更好方法是
LC_ALL=C GIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"
这将把最后一次提交的提交日期和作者日期设置为“现在”。
您可以进行交互式的更改,并选择编辑您想更改日期的提交。当rebase进程停止修改你输入的提交时,例如:
git commit --amend --date="Wed Feb 16 14:00 2011 +0100" --no-edit
注:date=now将使用当前时间。
之后,您将继续您的交互rebase。
修改提交日期而不是作者日期:
GIT_COMMITTER_DATE="Wed Feb 16 14:00 2011 +0100" git commit --amend --no-edit
上面的代码行设置了一个环境变量GIT_COMMITTER_DATE,该变量用于修改提交。
一切都在Git Bash中测试。
推荐文章
- 如何将git配置存储为存储库的一部分?
- 如何修改GitHub拉请求?
- 如何在Github和本地删除最后n次提交?
- 我如何调试git/git-shell相关的问题?
- 错误:无法使用rebase进行拉取:您有未分阶段的更改
- Git隐藏未缓存:如何把所有未分期的变化?
- 真实的恶魔
- Mercurial:如何修改上次提交?
- 如何从另一个分支获得更改
- Git:权限被拒绝(publickey)致命-无法从远程存储库读取。克隆Git存储库时
- git reflog和log有什么区别?
- git推挂在Total line之后
- 重命名git子模块
- 结合Git存储库的前两次提交?
- Xcode 6 gitignore文件应该包括什么?