如何更改 Git 中旧提交的时间戳?

发布于 2024-07-12 22:40:35 字数 208 浏览 5 评论 0原文

The answers to How to modify existing, unpushed commits? describe a way to amend previous commit messages that haven't yet been pushed upstream. The new messages inherit the timestamps of the original commits. This seems logical, but is there a way to also re-set the times?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

行雁书 2024-07-19 22:40:35

您可以进行交互式变基,并为要更改其日期的提交选择edit。 当变基过程停止修改您输入的提交时,例如:

git commit --amend --date="Wed Feb 16 14:00 2011 +0100" --no-edit

PS --date=now 将使用当前时间。

之后,您继续交互式变基。

要更改提交日期而不是作者日期:

GIT_COMMITTER_DATE="Wed Feb 16 14:00 2011 +0100" git commit --amend --no-edit

上面的行设置了一个环境变量 GIT_COMMITTER_DATE,用于修改提交。

一切都在 Git Bash 中进行测试。

You can do an interactive rebase and choose edit for the commit whose date you would like to alter. When the rebase process stops for amending the commit you type in for instance:

git commit --amend --date="Wed Feb 16 14:00 2011 +0100" --no-edit

P.S. --date=now will use the current time.

Afterward, you continue your interactive rebase.

To change the commit date instead of the author date:

GIT_COMMITTER_DATE="Wed Feb 16 14:00 2011 +0100" git commit --amend --no-edit

The lines above set an environment variable GIT_COMMITTER_DATE which is used in amending commit.

Everything is tested in Git Bash.

马蹄踏│碎落叶 2024-07-19 22:40:35

git filter-branch 与 env 过滤器结合使用,为您要修复的提交的特定哈希设置 GIT_AUTHOR_DATEGIT_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'

Use git filter-branch with an env filter that sets GIT_AUTHOR_DATE and GIT_COMMITTER_DATE for the specific hash of the commit you're looking to fix.

This will invalidate that and all future hashes.

Example:

If you wanted to change the dates of commit 119f9ecf58069b265ab22f1f97d2b648faf932e0, you could do so with something like this:

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'
眼泪都笑了 2024-07-19 22:40:35

在一个命令中处理所有这些建议的更好方法是,

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

这会将最后一次提交的提交和作者日期设置为“现在”。

A better way to handle all of these suggestions in one command is

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

This will set the last commit's commit and author date to "right now."

我不会写诗 2024-07-19 22:40:35

只需执行 git commit --amend --reset-author --no-edit 即可。 对于较旧的提交,您可以执行交互式变基并为要修改其日期的提交选择编辑

git rebase -i <ref>

然后使用 --reset-author--no-edit 修改提交,将作者日期更改为当前日期:

git commit --amend --reset-author --no-edit

最后继续交互式变基:

git rebase --continue

Just do git commit --amend --reset-author --no-edit. For older commits, you can do an interactive rebase and choose edit for the commit whose date you want to modify.

git rebase -i <ref>

Then amend the commit with --reset-author and --no-edit to change the author date to the current date:

git commit --amend --reset-author --no-edit

Finally continue with your interactive rebase:

git rebase --continue
海之角 2024-07-19 22:40:35

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

语法:

git redate -c 3

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

输入图像描述这里

I wrote a script and Homebrew package for this. Super easy to install, you can find it on GitHub PotatoLabs/git-redate page.

Syntax:

git redate -c 3

You just have to run git redate and you'll be able to edit all the dates in vim of the most recent 5 commits (there's also a -c option for how many commits you want to go back, it just defaults to 5). Let me know if you have any questions, comments, or suggestions!

enter image description here

妄想挽回 2024-07-19 22:40:35

每次提交都与两个日期相关联:提交者日期作者日期。 您可以通过以下方式查看这些日期:

git log --format=fuller

如果您想更改最后 6 次提交的作者日期和提交者日期,您可以简单地使用交互式 rebase :

git rebase -i HEAD~6

pick c95a4b7 Modification 1
pick 1bc0b44 Modification 2
pick de19ad3 Modification 3
pick c110e7e Modification 4
pick 342256c Modification 5
pick 5108205 Modification 6

# Rebase eadedca..5108205 onto eadedca (6 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit

对于要更改日期的所有提交,请将 pick 替换为 edit(或仅 e),然后保存并退出编辑器。

现在,您可以通过以 ISO-8601 格式指定作者日期和提交者日期来修改每个提交:

GIT_COMMITTER_DATE="2017-10-08T09:51:07" git commit --amend --date="2017-10-08T09:51:07"

第一个日期 (GIT_COMMITTER_DATE=) 是提交日期,第二个日期 (--date=) 是作者日期

然后使用 : 转到下一个提交:

git rebase --continue

重复此过程,直到修改所有提交。 使用 git status 检查您的进度。

Each commit is associated with two dates, the committer date and the author date. You can view these dates with:

git log --format=fuller

If you want to change the author date and the committer date of the last 6 commits, you can simply use an interactive rebase :

git rebase -i HEAD~6

.

pick c95a4b7 Modification 1
pick 1bc0b44 Modification 2
pick de19ad3 Modification 3
pick c110e7e Modification 4
pick 342256c Modification 5
pick 5108205 Modification 6

# Rebase eadedca..5108205 onto eadedca (6 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit

For all commits where you want to change the date, replace pick by edit (or just e), then save and quit your editor.

You can now amend each commit by specifying the author date and the committer date in ISO-8601 format:

GIT_COMMITTER_DATE="2017-10-08T09:51:07" git commit --amend --date="2017-10-08T09:51:07"

The first date (GIT_COMMITTER_DATE=) is the commit date, the second one (--date=) is the author date.

Then go to the next commit with :

git rebase --continue

Repeat the process until you amend all your commits. Check your progression with git status.

仲春光 2024-07-19 22:40:35
git commit --amend --date="now"
git commit --amend --date="now"
谁的新欢旧爱 2024-07-19 22:40:35

如何编辑多个提交日期

其他答案对于编辑多个提交日期不太方便。 几年后我再次回到这个问题来分享一项技术。

要更改最后 4 次提交的日期:

git rebase -i HEAD~4

按如下方式编辑变基,插入 exec 行以根据需要修改日期:

pick 4ca564e Do something
exec git commit --amend --no-edit --date "1 Oct 2019 12:00:00 PDT"
pick 1670583 Add another thing
exec git commit --amend --no-edit --date "2 Oct 2019 12:00:00 PDT"
pick b54021c Add some tests
exec git commit --amend --no-edit --date "3 Oct 2019 12:00:00 PDT"
pick e8f6653 Fix the broken thing
exec git commit --amend --no-edit --date "4 Oct 2019 12:00:00 PDT"

更新(2021 年 9 月):

如果您想在rebase 指令列表 (Git 2.6+):

git config --add rebase.instructionFormat "[%ai] %s"

然后你会看到类似的东西

pick 4f5a371f [2021-09-08 02:56:50 -0700] Add npm events
pick 67937227 [2021-09-09 03:05:42 -0700] Fixup

How to Edit Multiple Commit Dates

Other answers aren't very convenient for editing several commit dates. I've come back to this question after a few years to share a technique.

To change the dates of the last 4 commits:

git rebase -i HEAD~4

Edit the rebase as follows, inserting exec lines to modify dates as needed:

pick 4ca564e Do something
exec git commit --amend --no-edit --date "1 Oct 2019 12:00:00 PDT"
pick 1670583 Add another thing
exec git commit --amend --no-edit --date "2 Oct 2019 12:00:00 PDT"
pick b54021c Add some tests
exec git commit --amend --no-edit --date "3 Oct 2019 12:00:00 PDT"
pick e8f6653 Fix the broken thing
exec git commit --amend --no-edit --date "4 Oct 2019 12:00:00 PDT"

Update (Sep. 2021):

If you want to see the original commit date in the rebase instruction list (Git 2.6+):

git config --add rebase.instructionFormat "[%ai] %s"

Then you'll see something like

pick 4f5a371f [2021-09-08 02:56:50 -0700] Add npm events
pick 67937227 [2021-09-09 03:05:42 -0700] Fixup
贩梦商人 2024-07-19 22:40:35

阅读所有答案后,我想出了一种更简洁、更方便的方法来一次编辑多个提交的日期,而无需交互地重新设定:

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

它会更改提交者和作者日期。

After reading all the answers I came up with a more succinct and convenient way of editing the date of multiple commits at once without the need of rebasing interactively:

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

It changes both the committer and author dates.

月下凄凉 2024-07-19 22:40:35

基于 theosp答案,我编写了一个名为 git-cdc 的脚本(用于更改日期提交),并将其放入我的 PATH 中。

名称很重要: PATH 中的任何位置的 git-xxx 都允许您键入:

git xxx
# here
git cdc ... 

该脚本位于 bash 中,即使在 Windows 上也是如此(因为 Git 将从其 中调用它a href="https://stackoverflow.com/a/3144417/6309">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 days ago”语法。

Building on theosp's answer, I wrote a script called git-cdc (for change date commit) that I put in my PATH.

The name is important: git-xxx anywhere in your PATH allows you to type:

git xxx
# here
git cdc ... 

That script is in bash, even on Windows (since Git will be calling it from its msys environment)

#!/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"

With that, you can type:

git cdc @~ "2014-07-04 20:32:45"

That would reset author/commit date of the commit before HEAD (@~) to the specified date.

git cdc @~ "2 days ago"

That would reset author/commit date of the commit before HEAD (@~) to the same hour, but 2 days ago.


Ilya Semenov mentions in the comments:

For OS X you may also install GNU coreutils (brew install coreutils), add it to PATH (PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH") and then use "2 days ago" syntax.

百变从容 2024-07-19 22:40:35

2022 年 7 月的信息:

这个在当前时间戳下运行得非常好:

git commit --amend --date=now --no-edit

而这个 - 使用任何日期格式:

git commit --amend --date="Mon Jul 25 10:37:36 2022 +0300" --no-edit

Information from July 2022:

This one working amazing with current timestamp:

git commit --amend --date=now --no-edit

And this one - with any date format:

git commit --amend --date="Mon Jul 25 10:37:36 2022 +0300" --no-edit
木落 2024-07-19 22:40:35

要更改作者日期和提交日期:

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

To change both the author date and the commit date:

GIT_COMMITTER_DATE="Wed Sep 23 9:40 2015 +0200" git commit --amend --date "Wed Sep 23 9:40 2015 +0200"
随波逐流 2024-07-19 22:40:35

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

I created this npm package to change date of old commits.

https://github.com/bitriddler/git-change-date

Sample Usage:

npm install -g git-change-date
cd [your-directory]
git-change-date

You will be prompted to choose the commit you want to modify then to enter the new date.

If you want to change a commit by specific hash run this git-change-date --hash=[hash]

夏の忆 2024-07-19 22:40:35

如果是之前的最后一次提交。

git rebase  -i HEAD~2
git commit --amend --date=now

如果您已经推送到原始位置并且可以强制使用:

git push --force 

如果您无法强制推送并且如果已推送,则无法更改提交! 。

if it is previous last commit.

git rebase  -i HEAD~2
git commit --amend --date=now

if you already push to orgin and can force use:

git push --force 

if you can't force the push and if it is pushed, you can't change the commit! .

巾帼英雄 2024-07-19 22:40:35

修改上次提交的日期和时间的最简单方法

git commit --amend --date="12/31/2021 @ 14:00"

The most simple way to modify the date and time of the last commit

git commit --amend --date="12/31/2021 @ 14:00"
朮生 2024-07-19 22:40:35

这是一个方便的别名,可将上次提交的提交时间和作者时间更改为 date --date 接受的时间:

[alias]
    cd = "!d=\"$(date -d \"$1\")\" && shift && GIT_COMMITTER_DATE=\"$d\" \
            git commit --amend --date \"$d\""

用法:git cd

示例:

git cd now  # update the last commit time to current time
git cd '1 hour ago'  # set time to 1 hour ago

编辑:
这是一个更自动化的版本,它检查索引是否干净(没有未提交的更改)并重用最后的提交消息,否则会失败(万无一失):

[alias]
    cd = "!d=\"$(date -d \"$1\")\" && shift && \
        git diff-index --cached --quiet HEAD --ignore-submodules -- && \
        GIT_COMMITTER_DATE=\"$d\" git commit --amend -C HEAD --date \"$d\"" \
        || echo >&2 "error: date change failed: index not clean!"

Here is a convenient alias that changes both commit and author times of the last commit to a time accepted by date --date:

[alias]
    cd = "!d=\"$(date -d \"$1\")\" && shift && GIT_COMMITTER_DATE=\"$d\" \
            git commit --amend --date \"$d\""

Usage: git cd <date_arg>

Examples:

git cd now  # update the last commit time to current time
git cd '1 hour ago'  # set time to 1 hour ago

Edit:
Here is a more-automated version which checks that the index is clean (no uncommitted changes) and reuses the last commit message, or fails otherwise (fool-proof):

[alias]
    cd = "!d=\"$(date -d \"$1\")\" && shift && \
        git diff-index --cached --quiet HEAD --ignore-submodules -- && \
        GIT_COMMITTER_DATE=\"$d\" git commit --amend -C HEAD --date \"$d\"" \
        || echo >&2 "error: date change failed: index not clean!"
拔了角的鹿 2024-07-19 22:40:35

以下 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"
}

The following bash function will change the time of any commit on the current branch.

Be careful not to use if you already pushed the commit or if you use the commit in another branch.

# 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"
}
生来就爱笑 2024-07-19 22:40:35

如果您想获取另一个提交的确切日期(假设您变基编辑了一个提交并希望它具有原始预变基版本的日期):

git commit --amend --date="$(git show -s --format=%ai a383243)"

这会将 HEAD 提交的日期纠正为准确 提交 a383243 的日期(如果有歧义,请包括更多数字)。 它还会弹出一个编辑器窗口,以便您可以编辑提交消息。

这是您通常关心的作者日期 - 请参阅有关提交者日期的其他答案。

If you want to get the exact date of another commit (say you rebase edited a commit and want it to have the date of the original pre-rebase version):

git commit --amend --date="$(git show -s --format=%ai a383243)"

This corrects the date of the HEAD commit to be exactly the date of commit a383243 (include more digits if there are ambiguities). It will also pop up an editor window so you can edit the commit message.

That's for the author date which is what you care for usually - see other answers for the committer date.

焚却相思 2024-07-19 22:40:35

如果提交尚未推送,那么我可以使用类似的东西:

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

之后 git bash 打开具有已应用日期的编辑器,因此您只需通过在 VI 编辑器命令模式中键入“:wq”来保存它,然后就可以推送它

If commit not yet pushed then I can use something like that:

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

after that git bash opens editor with the already applied date so you need just to save it by typing in the VI editor command mode ":wq" and you can push it

南笙 2024-07-19 22:40:35

用于将最近 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'"

对于所有提交(包括初始提交) commit):

git rebase --root --exec "git commit --amend --no-edit --date 'now'"

添加 -i 为交互模式。

运行 git log --format=fuller --show-signature 来验证更改。

运行 git push -f 来更新远程存储库(⚠️危险区域)

有影响。 例如:

  • 提交 ID 将更改,因此您必须重新创建标签
  • 您将丢失原始签名
  • 这将使用您的 .gitconfig,这意味着您的密钥将用于签名提交(如果 Git 配置为签署承诺)

For updating the date of the 5 last commits to the current date (this method doesn't allow to update the initial commit):

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

For all commits after commit 95f5074…15074db2:

git rebase 95f5074…15074db2 --exec "git commit --amend --no-edit --date 'now'"

For all commits (including the initial commit):

git rebase --root --exec "git commit --amend --no-edit --date 'now'"

Add -i for the interactive mode.

Run git log --format=fuller --show-signature to validate the changes.

Run git push -f to update the remote repository (⚠️Danger zone)

There are implications. For instance:

  • Commit IDs will change, so you will have to recreate tags
  • You will lose original signatures
  • This will use your .gitconfig, this means your key will be used for signing commits (if Git is configured to sign commits)
与之呼应 2024-07-19 22:40:35
GIT_COMMITTER_DATE="Sun Nov 20 21:02 2022 +0530" git commit --amend --no-edit --date="Sun Nov 20 21:02 2022 +0530"
git push -f

每次都有效。

GIT_COMMITTER_DATE="Sun Nov 20 21:02 2022 +0530" git commit --amend --no-edit --date="Sun Nov 20 21:02 2022 +0530"
git push -f

Works everytime.

北陌 2024-07-19 22:40:35

将上次提交的日期设置为当前日期

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"

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

变基到所述提交之前并停止修改:

  1. git rebase^ -i
  2. 在提交(第一个提交)的行上将 pick 替换为 e (编辑)
  3. 退出编辑器(ESC在 VIM 中后跟 :wq
  4. 或者:
  • 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"

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

Set the date of the last commit to the current date

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

Set the date of the last commit to an arbitrary 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"

Set the date of an arbitrary commit to an arbitrary or current date

Rebase to before said commit and stop for amendment:

  1. git rebase <commit-hash>^ -i
  2. Replace pick with e (edit) on the line with that commit (the first one)
  3. quit the editor (ESC followed by :wq in VIM)
  4. Either:
  • 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"

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

a√萤火虫的光℡ 2024-07-19 22:40:35

如果您想在标准 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 日期,从而节省大量时间来查找正确的星期几,并避免因元素顺序而产生的普遍挫败感。
  • 如果您希望作者和提交者日期相同,则可以引用之前设置的变量。

非常感谢 博客文章科林·斯温根。 尽管他的代码对我不起作用,但它帮助我找到了正确的解决方案。

If you want to perform the accepted answer (https://stackoverflow.com/a/454750/72809) in standard Windows command line, you need the following command:

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"

Notes:

  • It may be possible to split the command over multiple lines (Windows supports line splitting with the carret symbol ^), but I didn't succeed.
  • You can write ISO dates, saving a lot of time finding the right day-of-week and general frustration over the order of elements.
  • If you want the Author and Committer date to be the same, you can just reference the previously set variable.

Many thanks go to a blog post by Colin Svingen. Even though his code didn't work for me, it helped me find the correct solution.

橘香 2024-07-19 22:40:35

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

For those using 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

Credit to https://mnaoumov.wordpress.com/2012/09/23/git-change-date-of-commit/

你是年少的欢喜 2024-07-19 22:40:35

编辑作者日期和最近 3 次提交的提交日期:

git rebase -i HEAD~3 --committer-date-is-author-date --exec "git commit --amend --no-edit --date=now"

--exec 命令附加在 rebase 中的每一行之后,您可以使用 --date= 选择作者日期...,提交者日期将与作者日期相同。

Edit the author date and the commit date of the last 3 commits:

git rebase -i HEAD~3 --committer-date-is-author-date --exec "git commit --amend --no-edit --date=now"

The --exec command is appended after each line in the rebase and you can choose the author date with the --date=..., the committer date will be the same of author date.

梦回梦里 2024-07-19 22:40:35

除了 Matt Montag 的回答

如果您需要将时间戳重置为当前时间 在 rebase 命令之后,

git rebase -i HEAD~2

您可以使用以下选项之一,

pick 4ca564e Do something
exec git commit --amend --no-edit --date=now
pick 1670583 Add another thing
exec git commit --amend --no-edit --reset-author

两者都可以使用

In addition to Matt Montag's answer:

If you need to reset timestamp to current time after rebase command

git rebase -i HEAD~2

you can use one of these options

pick 4ca564e Do something
exec git commit --amend --no-edit --date=now
pick 1670583 Add another thing
exec git commit --amend --no-edit --reset-author

Both will work

七禾 2024-07-19 22:40:35

已经有很多很好的答案,但是当我想更改一天或一个月内多次提交的日期时,我找不到正确的答案。 因此,我为此创建了一个带有解释的新脚本,希望它能帮助某人:

#!/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

There are already many great answers, but when I want to change date for multiple commits in one day or in one month, I don't find a proper answer. So I create a new script for this with explaintion, hope it will help someone:

#!/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

The date will be changed:

AuthorDate: Wed Sep 13 13:39:41 2017 +0800
枫林﹌晚霞¤ 2024-07-19 22:40:35

我想确保在午夜精确地更新代码的版权注释,并且我不想冒 atcron。 所以,我提交了代码,然后:(

GIT_COMMITTER_DATE="Fri Jan 1 00:00:00 2021 +1000" git commit --amend --no-edit --date="Fri Jan 1 00:00:00 2021 +1000"

或者甚至可能将 UTC 偏移设置为 0?决定……)现在我可以推送了!

大家新年快乐

I wanted to make sure that I update my code’s copyright comments at precisely midnight, and I didn’t want to risk a tiny delay with at or cron. So, I commited the code and then:

GIT_COMMITTER_DATE="Fri Jan 1 00:00:00 2021 +1000" git commit --amend --no-edit --date="Fri Jan 1 00:00:00 2021 +1000"

(Or perhaps even set the UTC offset to 0? Decisions… ) Now I can push!

Happy new year, everybody ????

零時差 2024-07-19 22:40:35

我最近需要这个,并使我自己的脚本看起来很像 git-redate

但是我的脚本做了最小的修改,并且重写(如果您需要更新)许多提交所需的时间要少得多,因为它一次完成所有提交

change_git_history

实际上这也允许更改提交消息

解释:

脚本连接了一堆 bash if 表达式,看起来像这样

以下是修改提交日期的内容

if [ "$GIT_COMMIT" = "$com_hash" ]; # com is commit
then
    export GIT_AUTHOR_DATE="$com_date";
    export GIT_COMMITTER_DATE="$com_date";
fi;

以下是修改提交消息的内容:

if [ true = false ]; # impossible
then
    : # pass
elif [ "$GIT_COMMIT" = "$com_hash" ];
then
    sed 's/.*/$com_msg_esc/g' # replace content with new content
else
    cat - # returns previous content
fi;

我们使用

git filter-branch -f \
    --env-filter "$UPDATES" \
    --msg-filter "$MESSAGES" \
    -- "$REV"

(doc 在这里 过滤分支人)

I have recently needed this and made my own script looking a lot like git-redate

However my scripts does the minimal modifications and takes a lot less time to rewrite (if you need to update) many commits as it does them all at once

change_git_history

Actually this allows to change commit messages too

Explaination:

The scripts concatenates a bunch of bash if-expression looking like so

Here are the ones modifiying the commit date

if [ "$GIT_COMMIT" = "$com_hash" ]; # com is commit
then
    export GIT_AUTHOR_DATE="$com_date";
    export GIT_COMMITTER_DATE="$com_date";
fi;

Here are the ones modifiying the commit message:

if [ true = false ]; # impossible
then
    : # pass
elif [ "$GIT_COMMIT" = "$com_hash" ];
then
    sed 's/.*/$com_msg_esc/g' # replace content with new content
else
    cat - # returns previous content
fi;

And we push all the update using

git filter-branch -f \
    --env-filter "$UPDATES" \
    --msg-filter "$MESSAGES" \
    -- "$REV"

(doc is here filter-branch man)

隔纱相望 2024-07-19 22:40:35

TL;DR: 匹配日期 + 重新创建 GPG 签名


(如果您知道剥离以保留原始签名的解决方法,请评论/编辑。)


我会推翻这个旧线程,因为已经引入了签名提交的功能,并且所有这些 git filter-branch 和 likes 基本上都剥离了文档中指定的签名:

...如果标签附加了签名,则签名将被删除。 根据定义,保存签名是不可能的。 ...(来源:--tag-name-filter

但是它也会破坏漂亮的 已验证 GitHub 提交上的徽章(以及其他 Git 托管位置,如果以相同方式实现),因此这也将解决该问题。 部分。

据我所知,不可能通过 git 命令破坏 (GPG) 签名,使其包含提交日期签名日期以简单的方式,因此即使创作和提交的日期发生变化,它'仍然是当前日期,例如:

commit <hash>
gpg: Signature made Sun 25 Jul 2021 00:00:00 PM TZ
gpg:                using TYPE key KEY
gpg: Good signature from "Signer <[email protected]>"
Author:     Author <[email protected]>
AuthorDate: Sat Jan 1 00:00:00 2000 +0000
Commit:     Author <[email protected]>
CommitDate: Sat Jan 1 00:00:00 2000 +0000

假设您有一个想要从某个提交签名的存储库(我将选择根提交;如果其他人在该存储库上工作,则不推荐)。 git commit 的文档它会提取数据也来自环境变量(如果存在),因此我们有一个地方可以放置输入。

要检索数据(可以使用 git commit --date=... 设置),我们可以查看 git show --format=%ad,因此对于原始日期字符串将是:

git show --format=%ad --no-patch
# Sat Jan 1 00:00:00 2000 +0000

因此,我们有:

  • 每个提交的起始
  • 原始日期字符串
  • GIT_COMMITTER_DATE 来匹配日期(作者 -> 提交者)

对于变基,让我们这样做:

git rebase --root <branch-name> --keep-empty --interactive

这将用于分支的根提交 ,保留使用 git commit -m "empty" --allow-empty 创建的任何空提交,并询问您要修改哪些提交。 在那里,您将所需的提交从 pick 更改为 edit (对于我的情况,会将所有提交标记为 edit),然后您'将被放入一个独立的 HEAD 提交中,乐趣从这里开始。

# or "while :"
while true
do
    GIT_COMMITTER_DATE=$(git show --format=%ad --no-patch) \
        git commit --amend --gpg-sign --no-edit --allow-empty
    git rebase --continue
done

(如果您没有指定 user.signingkey,请使用 --gpg-sign=

这将遍历每个edit 标记的提交,设置提交者的日期以匹配作者的日期,保留任何空提交,不会触及整个补丁主体,并将添加带有执行命令的日期的签名。

一旦您看到 fatal: No rebase inprogress?,请按 Ctrl-C 停止循环并检查日志以确认日期匹配并且签名随处可见:

git log --pretty=fuller --show-signature

如果日志中一切正常,只需发出 git push --force 即可完成。 现在您应该会看到每个提交的 Verified 徽章。

真实历史树示例。 GitHub 似乎并不关心签名的日期(没有任何参考),但它仍然会出现在 git log 中。

TL;DR: Matching dates + re-creating GPG signatures


(Comment/edit if you know a workaround for the stripping to preserve the original signature.)


I'll bump this old thread because a feature of signing commits has been introduced and all of these git filter-branch and likes basically strip the signatures as specified in the docs:

... If the tag has a signature attached, the signature will be stripped. It is by definition impossible to preserve signatures. ... (source: --tag-name-filter )

But it'll also break the pretty Verified badge on a GitHub commit (and in other Git hosting places if implemented in the same way), so this will fix that as well. Partially.

Afaik it's not possible to mangle a (GPG) signature through git command in a way that it also contains the date of a commit instead of the date of signing in a simple way and therefore even if the dates for authoring and commit are moved, it'll still be the present date, example:

commit <hash>
gpg: Signature made Sun 25 Jul 2021 00:00:00 PM TZ
gpg:                using TYPE key KEY
gpg: Good signature from "Signer <[email protected]>"
Author:     Author <[email protected]>
AuthorDate: Sat Jan 1 00:00:00 2000 +0000
Commit:     Author <[email protected]>
CommitDate: Sat Jan 1 00:00:00 2000 +0000

So imagine you have a repo you want to sign from a certain commit (I'll go for the root commit; not recommended if somebody else works on the repo). The documentation for git commit says it pulls data from env vars too, if present, thus we have a place to put the input to.

To retrieve the data (can be set with git commit --date=...) we can take a look at git show --format=%ad, so for a raw date string that would be:

git show --format=%ad --no-patch
# Sat Jan 1 00:00:00 2000 +0000

So we have:

  • point of start
  • raw date string for each commit
  • GIT_COMMITTER_DATE to match the dates (author -> committer)

For rebasing let's do this:

git rebase --root <branch-name> --keep-empty --interactive

which will go for the root commit of a branch <branch-name>, preserve any empty commits created with git commit -m "empty" --allow-empty and ask you which commits to modify. There you change the desired commits from pick to edit (for my case that'd be marking all of them as edit), then you'll be dropped into a detached HEAD commit and from here the fun begins.

# or "while :"
while true
do
    GIT_COMMITTER_DATE=$(git show --format=%ad --no-patch) \
        git commit --amend --gpg-sign --no-edit --allow-empty
    git rebase --continue
done

(if you do not have user.signingkey specified, use --gpg-sign=<fingerprint>)

This will go through each of the edit-marked commit, set the committer's date to match the author's date, keep any empty commit, won't touch the overall patch body and will add a signature with a date of when the command was executed.

Once you see fatal: No rebase in progress? press Ctrl-C to stop the loop and check the logs to confirm that the dates match and the signatures are present everywhere with:

git log --pretty=fuller --show-signature

If everything is okay in the logs, simply issue git push --force and you're done. Now you should see that Verified badge for each commit.

Example with a real history tree. GitHub doesn't seem to care about the signature's date (no reference anywhere), but it'll still be present in the git log.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文