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


当前回答

如果commit还没有被推送,那么我可以使用这样的东西:

git commit --amend --date=" Wed Mar 25 10:05:44 2020 +0300"

之后,git bash打开编辑器,其中包含已经应用的日期,所以你只需要在VI编辑器命令模式中输入“:wq”来保存它,然后你可以推送它

其他回答

将最后一次提交的日期设置为当前日期

GIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"

将最后一次提交的日期设置为任意日期

GIT_COMMITTER_DATE="Mon 20 Aug 2018 20:19:19 BST" git commit --amend --no-edit --date "Mon 20 Aug 2018 20:19:19 BST"

将任意提交的日期设置为任意或当前日期

还原到之前所述的提交和停止修改:

Git rebase <commit-hash>^ -i 将pick替换为e (edit)并提交(第一个) 退出编辑器(在VIM中,ESC后跟:wq) :

GIT_COMMITTER_DATE="$(date)" git commit - modify -no-edit -date "$(date)" GIT_COMMITTER_DATE="Mon 20 Aug 2018 20:19:19 BST" git commit - modify -no-edit -date "Mon 20 Aug 2018 20:19:19 BST"

来源: https://codewithhugo.com/change-the-date-of-a-git-commit/

对于那些使用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/

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

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

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

如果commit还没有被推送,那么我可以使用这样的东西:

git commit --amend --date=" Wed Mar 25 10:05:44 2020 +0300"

之后,git bash打开编辑器,其中包含已经应用的日期,所以你只需要在VI编辑器命令模式中输入“:wq”来保存它,然后你可以推送它

下面的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"
}