如何修改现有的、未推送的提交?描述一种修改尚未推送到上游的先前提交消息的方法。新消息继承原始提交的时间戳。这似乎合乎逻辑,但有没有办法也重新设定时间呢?
当前回答
修改上次提交的日期和时间的最简单方法
git commit --amend --date="12/31/2021 @ 14:00"
其他回答
已经有很多很好的答案,但是当我想在一天或一个月内更改多次提交的日期时,我找不到合适的答案。所以我为此创建了一个新的脚本,希望它能帮助到一些人:
#!/bin/bash
# change GIT_AUTHOR_DATE for commit at Thu Sep 14 13:39:41 2017 +0800
# you can change the data_match to change all commits at any date, one day or one month
# you can also do the same for GIT_COMMITTER_DATE
git filter-branch --force --env-filter '
date_match="^Thu, 14 Sep 2017 13+"
# GIT_AUTHOR_DATE will be @1505367581 +0800, Git internal format
author_data=$GIT_AUTHOR_DATE;
author_data=${author_data#@}
author_data=${author_data% +0800} # author_data is 1505367581
oneday=$((24*60*60))
# author_data_str will be "Thu, 14 Sep 2017 13:39:41 +0800", RFC2822 format
author_data_str=`date -R -d @$author_data`
if [[ $author_data_str =~ $date_match ]];
then
# remove one day from author_data
new_data_sec=$(($author_data-$oneday))
# change to git internal format based on new_data_sec
new_data="@$new_data_sec +0800"
export GIT_AUTHOR_DATE="$new_data"
fi
' --tag-name-filter cat -- --branches --tags
日期将有所更改:
AuthorDate: Wed Sep 13 13:39:41 2017 +0800
2022年7月起:
这是一个惊人的工作与当前时间戳:
git commit --amend --date=now --no-edit
还有这个——任何日期格式:
git commit --amend --date="Mon Jul 25 10:37:36 2022 +0300" --no-edit
下面的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"
}
如果你想在标准的Windows命令行中执行接受的答案(https://stackoverflow.com/a/454750/72809),你需要以下命令:
git filter-branch -f --env-filter "if [ $GIT_COMMIT = 578e6a450ff5318981367fe1f6f2390ce60ee045 ]; then export GIT_AUTHOR_DATE='2009-10-16T16:00+03:00'; export GIT_COMMITTER_DATE=$GIT_AUTHOR_DATE; fi"
注:
可以将命令拆分为多行(Windows支持用斜杠符号^拆分行),但我没有成功。 您可以编写ISO日期,从而节省大量查找合适的星期几的时间,避免在元素顺序方面遇到麻烦。 如果您希望Author和Committer日期相同,则只需引用前面设置的变量即可。
非常感谢Colin Svingen的博客文章。尽管他的代码对我不起作用,但它帮助我找到了正确的解决方案。
对于那些使用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/