如何修改现有的、未推送的提交?描述一种修改尚未推送到上游的先前提交消息的方法。新消息继承原始提交的时间戳。这似乎合乎逻辑,但有没有办法也重新设定时间呢?


当前回答

在阅读了所有的答案后,我想出了一个更简洁和方便的方法来编辑多次提交的日期,而不需要交互式地重基:

git rebase HEAD~4 --exec "git commit --amend --no-edit --date 'now'"

它同时更改提交者和作者的日期。

其他回答

修改作者日期和提交日期:

GIT_COMMITTER_DATE="Wed Sep 23 9:40 2015 +0200" git commit --amend --date "Wed Sep 23 9:40 2015 +0200"

将最近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被配置为签名提交)

基于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天前"语法。

我创建了这个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]

我为此写了一个脚本和Homebrew包。超级容易安装,你可以在GitHub PotatoLabs/git-redate页面上找到它。

语法:

git redate -c 3

你只需要运行git redate,你就可以在vim中编辑最近5次提交的所有日期(还有一个-c选项来决定你想要返回多少次提交,它默认是5)。如果你有任何问题,评论或建议,请告诉我!