如何修改现有的、未推送的提交?描述一种修改尚未推送到上游的先前提交消息的方法。新消息继承原始提交的时间戳。这似乎合乎逻辑,但有没有办法也重新设定时间呢?
当前回答
在一个命令中处理所有这些建议的更好方法是
LC_ALL=C GIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"
这将把最后一次提交的提交日期和作者日期设置为“现在”。
其他回答
下面的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"
}
只需执行git commit - modify -reset-author -no-edit。对于较旧的提交,您可以执行交互式的重基操作,并选择要修改其日期的提交的编辑。
git rebase -i <ref>
然后使用——reset-author和——no-edit修改提交,将作者日期更改为当前日期:
git commit --amend --reset-author --no-edit
最后继续您的互动rebase:
git rebase --continue
在一个命令中处理所有这些建议的更好方法是
LC_ALL=C GIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"
这将把最后一次提交的提交日期和作者日期设置为“现在”。
我创建了这个npm包来更改旧的提交日期。
https://github.com/bitriddler/git-change-date
示例用法:
npm install -g git-change-date
cd [your-directory]
git-change-date
系统将提示您选择要修改的提交,然后输入新的日期。
如果你想通过指定的散列来更改提交,请运行git-change-date——hash=[hash]
修改上次提交的日期和时间的最简单方法
git commit --amend --date="12/31/2021 @ 14:00"