如何修改现有的、未推送的提交?描述一种修改尚未推送到上游的先前提交消息的方法。新消息继承原始提交的时间戳。这似乎合乎逻辑,但有没有办法也重新设定时间呢?
当前回答
对于那些使用Powershell
git rebase DESIRED_REF^ -i
$commitDateString = "2020-01-22T22:22:22"
$env:GIT_COMMITTER_DATE = $commitDateString
git commit --amend --date $commitDateString
$env:GIT_COMMITTER_DATE = ""
git rebase --continue
来源:https://mnaoumov.wordpress.com/2012/09/23/git-change-date-of-commit/
其他回答
使用带有env过滤器的git filter-branch,该过滤器为您希望修复的提交的特定散列设置GIT_AUTHOR_DATE和GIT_COMMITTER_DATE。
这将使该哈希值和所有未来哈希值无效。
例子:
如果你想改变提交119f9ecf58069b265ab22f1f97d2b648faf932e0的日期,你可以这样做:
git filter-branch --env-filter \
'if [ $GIT_COMMIT = 119f9ecf58069b265ab22f1f97d2b648faf932e0 ]
then
export GIT_AUTHOR_DATE="Fri Jan 2 21:38:53 2009 -0800"
export GIT_COMMITTER_DATE="Sat May 19 01:01:01 2007 -0700"
fi'
在一个命令中处理所有这些建议的更好方法是
LC_ALL=C GIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"
这将把最后一次提交的提交日期和作者日期设置为“现在”。
基于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天前"语法。
编辑作者日期和最近3次提交的提交日期:
git rebase -i HEAD~3 --committer-date-is-author-date --exec "git commit --amend --no-edit --date=now"
——exec命令附加在rebase中的每一行之后,您可以使用——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"
}