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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/454734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:05:19  来源:igfitidea点击:

How can one change the timestamp of an old commit in Git?

gittimestampcommitgit-rewrite-history

提问by Dhskjlkakdh

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?

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

采纳答案by Dustin

Use git filter-branchwith an env filter that sets GIT_AUTHOR_DATEand GIT_COMMITTER_DATEfor the specific hash of the commit you're looking to fix.

git filter-branch与 env 过滤器一起使用,该过滤器设置GIT_AUTHOR_DATEGIT_COMMITTER_DATE用于您要修复的提交的特定哈希。

This will invalidate that and all future hashes.

这将使该哈希和所有未来的哈希无效。

Example:

例子:

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

如果你想改变commit的日期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'

回答by Paul Pladijs

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

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

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

Afterwards you continue your interactive rebase.

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

UPDATE(in response to the comment of studgeek): to change the commit date instead of the author date:

更新(响应studeek的评论):更改提交日期而不是作者日期:

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

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

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

Everything is tested in Git Bash.

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

回答by Luke Ehresman

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."

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

回答by gypaetus

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

就做git commit --amend --reset-author --no-edit。对于较旧的提交,您可以进行交互式 rebase 并选择edit要修改其日期的提交。

git rebase -i <ref>

Then amend the commit with --reset-authorand --no-editto change the author date to the current date:

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

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

Finally continue with your interactive rebase:

最后继续您的交互式变基:

git rebase --continue

回答by bigpotato

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

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

Syntax:

句法:

git redate -c 3

You just have to run git redateand you'll be able to edit all the dates in vim of the most recent 5 commits (there's also a -coption 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!

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

enter image description here

在此处输入图片说明

回答by Ortomala Lokni

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 :

如果要更改最后 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

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

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

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

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

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

The first date is the commit date, the second one 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.

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

回答by Harald Nordgren

git commit --amend --date="now"

回答by VonC

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

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

The name is important: git-xxxanywhere in your PATHallows you to type:

名称很重要:git-xxxPATH可以在任何地方输入:

git xxx
# here
git cdc ... 

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

该脚本在 bash 中,即使在 Windows 上(因为 Git 将从其msys 环境中调用它)

#!/bin/bash
# commit
# date YYYY-mm-dd HH:MM:SS

commit="" datecal=""
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.

这会将 HEAD ( @~)之前提交的作者/提交日期重置为指定日期。

git cdc @~ "2 days ago"

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

这会将 HEAD ( @~)之前提交的作者/提交日期重置为同一小时,但 2 天前。



Ilya Semenovmentions in the comments:

Ilya Semenov在评论中提到:

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.

对于 OS X,您还可以安装 GNU coreutils( brew install coreutils),将其添加到PATH( PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"),然后使用“ 2 days ago”语法。

回答by Matt Montag

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:

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

git rebase -i HEAD~4

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

按如下方式编辑变基,插入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"

回答by Sérgio

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! .

如果你不能强制推送,如果它被推送,你就不能改变提交!.