如何更改 Git 中旧提交的时间戳?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
您可以进行交互式变基,并为要更改其日期的提交选择
edit
。 当变基过程停止修改您输入的提交时,例如:PS
--date=now
将使用当前时间。之后,您继续交互式变基。
要更改提交日期而不是作者日期:
上面的行设置了一个环境变量
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: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:
The lines above set an environment variable
GIT_COMMITTER_DATE
which is used in amending commit.Everything is tested in Git Bash.
将
git filter-branch
与 env 过滤器结合使用,为您要修复的提交的特定哈希设置GIT_AUTHOR_DATE
和GIT_COMMITTER_DATE
。这将使该哈希值和所有未来的哈希值无效。
示例:
如果您想更改 提交
119f9ecf58069b265ab22f1f97d2b648faf932e0
的日期,你可以这样做:Use
git filter-branch
with an env filter that setsGIT_AUTHOR_DATE
andGIT_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:在一个命令中处理所有这些建议的更好方法是,
这会将最后一次提交的提交和作者日期设置为“现在”。
A better way to handle all of these suggestions in one command is
This will set the last commit's commit and author date to "right now."
只需执行 git commit --amend --reset-author --no-edit 即可。 对于较旧的提交,您可以执行交互式变基并为要修改其日期的提交选择
编辑
。然后使用
--reset-author
和--no-edit
修改提交,将作者日期更改为当前日期:最后继续交互式变基:
Just do
git commit --amend --reset-author --no-edit
. For older commits, you can do an interactive rebase and chooseedit
for the commit whose date you want to modify.Then amend the commit with
--reset-author
and--no-edit
to change the author date to the current date:Finally continue with your interactive rebase:
我为此编写了一个脚本和 Homebrew 包。 安装超级简单,你可以在 GitHub 上找到它
PotatoLabs/git-redate
页面。语法:
你只需运行 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:
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!每次提交都与两个日期相关联:提交者日期和作者日期。 您可以通过以下方式查看这些日期:
如果您想更改最后 6 次提交的作者日期和提交者日期,您可以简单地使用交互式 rebase :
。
对于要更改日期的所有提交,请将
pick
替换为edit
(或仅e
),然后保存并退出编辑器。现在,您可以通过以 ISO-8601 格式指定作者日期和提交者日期来修改每个提交:
第一个日期 (
GIT_COMMITTER_DATE=
) 是提交日期,第二个日期 (--date=
) 是作者日期。然后使用 : 转到下一个提交:
重复此过程,直到修改所有提交。 使用
git status
检查您的进度。Each commit is associated with two dates, the committer date and the author date. You can view these dates with:
If you want to change the author date and the committer date of the last 6 commits, you can simply use an interactive rebase :
.
For all commits where you want to change the date, replace
pick
byedit
(or juste
), 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:
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 :
Repeat the process until you amend all your commits. Check your progression with
git status
.如何编辑多个提交日期
其他答案对于编辑多个提交日期不太方便。 几年后我再次回到这个问题来分享一项技术。
要更改最后 4 次提交的日期:
按如下方式编辑变基,插入
exec
行以根据需要修改日期:更新(2021 年 9 月):
如果您想在rebase 指令列表 (Git 2.6+):
然后你会看到类似的东西
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:
Edit the rebase as follows, inserting
exec
lines to modify dates as needed:Update (Sep. 2021):
If you want to see the original commit date in the rebase instruction list (Git 2.6+):
Then you'll see something like
阅读所有答案后,我想出了一种更简洁、更方便的方法来一次编辑多个提交的日期,而无需交互地重新设定:
它会更改提交者和作者日期。
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:
It changes both the committer and author dates.
基于 theosp 的 答案,我编写了一个名为 git-cdc 的脚本(用于更改日期提交),并将其放入我的 PATH 中。
名称很重要:
PATH
中的任何位置的git-xxx
都允许您键入:该脚本位于 bash 中,即使在 Windows 上也是如此(因为 Git 将从其
中调用它a href="https://stackoverflow.com/a/3144417/6309">msys 环境)
这样,您可以输入:
这将重置 HEAD 之前提交的作者/提交日期 (
@ ~
) 到指定日期。这会将 HEAD (
@~
) 之前提交的作者/提交日期重置为同一时间,但为 2 天前。Ilya Semenov 提到在评论中:
Building on theosp's answer, I wrote a script called
git-cdc
(for change date commit) that I put in myPATH
.The name is important:
git-xxx
anywhere in yourPATH
allows you to type:That script is in bash, even on Windows (since Git will be calling it from its msys environment)
With that, you can type:
That would reset author/commit date of the commit before HEAD (
@~
) to the specified date.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:
2022 年 7 月的信息:
这个在当前时间戳下运行得非常好:
而这个 - 使用任何日期格式:
Information from July 2022:
This one working amazing with current timestamp:
And this one - with any date format:
要更改作者日期和提交日期:
To change both the author date and the commit date:
我创建了这个 npm 包来更改旧提交的日期。
https://github.com/bitriddler/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:
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]
如果是之前的最后一次提交。
如果您已经推送到原始位置并且可以强制使用:
如果您无法强制推送并且如果已推送,则无法更改提交! 。
if it is previous last commit.
if you already push to orgin and can force use:
if you can't force the push and if it is pushed, you can't change the commit! .
修改上次提交的日期和时间的最简单方法
The most simple way to modify the date and time of the last commit
这是一个方便的别名,可将上次提交的提交时间和作者时间更改为
date --date
接受的时间:用法:
git cd
示例:
编辑:
这是一个更自动化的版本,它检查索引是否干净(没有未提交的更改)并重用最后的提交消息,否则会失败(万无一失):
Here is a convenient alias that changes both commit and author times of the last commit to a time accepted by
date --date
:Usage:
git cd <date_arg>
Examples:
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):
以下 bash 函数将更改当前分支上任何提交的时间。
如果您已经推送了提交或者在另一个分支中使用了提交,请小心不要使用。
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.
如果您想获取另一个提交的确切日期(假设您变基编辑了一个提交并希望它具有原始预变基版本的日期):
这会将 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):
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.
如果提交尚未推送,那么我可以使用类似的东西:
之后 git bash 打开具有已应用日期的编辑器,因此您只需通过在 VI 编辑器命令模式中键入“:wq”来保存它,然后就可以推送它
If commit not yet pushed then I can use something like that:
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
用于将最近 5 次提交的日期更新为当前日期(此方法不允许更新初始提交):
对于提交 95f5074…15074db2 后的所有提交:
对于所有提交(包括初始提交) commit):
添加
-i
为交互模式。运行 git log --format=fuller --show-signature 来验证更改。
运行 git push -f 来更新远程存储库(⚠️危险区域)
有影响。 例如:
.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):
For all commits after commit 95f5074…15074db2:
For all commits (including the initial commit):
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:
.gitconfig
, this means your key will be used for signing commits (if Git is configured to sign commits)每次都有效。
Works everytime.
将上次提交的日期设置为当前日期
将上次提交的日期设置为任意日期
将任意提交的日期设置为任意或当前日期
变基到所述提交之前并停止修改:
pick
替换为e
(编辑):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"
来源:
https://codewithhugo.com/change-the-date-of- a-git-提交/
Set the date of the last commit to the current date
Set the date of the last commit to an arbitrary date
Set the date of an arbitrary commit to an arbitrary or current date
Rebase to before said commit and stop for amendment:
git rebase <commit-hash>^ -i
pick
withe
(edit) on the line with that commit (the first one):wq
in VIM)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/
如果您想在标准 Windows 中执行接受的答案 (https://stackoverflow.com/a/454750/72809)命令行,您需要以下命令:
注意:
^
进行行拆分),但我没有成功。非常感谢 博客文章科林·斯温根。 尽管他的代码对我不起作用,但它帮助我找到了正确的解决方案。
If you want to perform the accepted answer (https://stackoverflow.com/a/454750/72809) in standard Windows command line, you need the following command:
Notes:
^
), but I didn't succeed.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.
对于使用 Powershell 的用户,
请访问 https://mnaoumov。 wordpress.com/2012/09/23/git-change-date-of-commit/
For those using Powershell
Credit to https://mnaoumov.wordpress.com/2012/09/23/git-change-date-of-commit/
编辑作者日期和最近 3 次提交的提交日期:
--exec
命令附加在 rebase 中的每一行之后,您可以使用--date= 选择作者日期...
,提交者日期将与作者日期相同。Edit the author date and the commit date of the last 3 commits:
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.除了 Matt Montag 的回答:
如果您需要将时间戳重置为当前时间 在 rebase 命令之后,
您可以使用以下选项之一,
两者都可以使用
In addition to Matt Montag's answer:
If you need to reset timestamp to current time after rebase command
you can use one of these options
Both will work
已经有很多很好的答案,但是当我想更改一天或一个月内多次提交的日期时,我找不到正确的答案。 因此,我为此创建了一个带有解释的新脚本,希望它能帮助某人:
日期将被更改:
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:
The date will be changed:
我想确保在午夜精确地更新代码的版权注释,并且我不想冒 at 或 cron。 所以,我提交了代码,然后:(
或者甚至可能将 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:
(Or perhaps even set the UTC offset to 0? Decisions… ) Now I can push!
Happy new year, everybody ????
我最近需要这个,并使我自己的脚本看起来很像 git-redate
但是我的脚本做了最小的修改,并且重写(如果您需要更新)许多提交所需的时间要少得多,因为它一次完成所有提交
change_git_history
实际上这也允许更改提交消息
解释:
脚本连接了一堆 bash if 表达式,看起来像这样
以下是修改提交日期的内容
以下是修改提交消息的内容:
我们使用
(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
Here are the ones modifiying the commit message:
And we push all the update using
(doc is here filter-branch man)
TL;DR: 匹配日期 + 重新创建 GPG 签名
(如果您知道剥离以保留原始签名的解决方法,请评论/编辑。)
我会推翻这个旧线程,因为已经引入了签名提交的功能,并且所有这些 git filter-branch 和 likes 基本上都剥离了文档中指定的签名:
但是它也会破坏漂亮的
已验证
GitHub 提交上的徽章(以及其他 Git 托管位置,如果以相同方式实现),因此这也将解决该问题。 部分。据我所知,不可能通过
git
命令破坏 (GPG) 签名,使其还包含提交日期签名日期以简单的方式,因此即使创作和提交的日期发生变化,它'仍然是当前日期,例如:假设您有一个想要从某个提交签名的存储库(我将选择根提交;如果其他人在该存储库上工作,则不推荐)。
git commit
的文档说它会提取数据也来自环境变量(如果存在),因此我们有一个地方可以放置输入。要检索数据(可以使用
git commit --date=...
设置),我们可以查看git show --format=%ad
,因此对于原始日期字符串将是:因此,我们有:
GIT_COMMITTER_DATE
来匹配日期(作者 -> 提交者)对于变基,让我们这样做:
这将用于分支的根提交
,保留使用 git commit -m "empty" --allow-empty 创建的任何空提交,并询问您要修改哪些提交。 在那里,您将所需的提交从pick
更改为edit
(对于我的情况,会将所有提交标记为edit
),然后您'将被放入一个独立的HEAD
提交中,乐趣从这里开始。(如果您没有指定
user.signingkey
,请使用--gpg-sign=
)这将遍历每个
edit
标记的提交,设置提交者的日期以匹配作者的日期,保留任何空提交,不会触及整个补丁主体,并将添加带有执行命令的日期的签名。一旦您看到
fatal: No rebase inprogress?
,请按Ctrl-C
停止循环并检查日志以确认日期匹配并且签名随处可见:如果日志中一切正常,只需发出 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: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: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 atgit show --format=%ad
, so for a raw date string that would be:So we have:
GIT_COMMITTER_DATE
to match the dates (author -> committer)For rebasing let's do this:
which will go for the root commit of a branch
<branch-name>
, preserve any empty commits created withgit commit -m "empty" --allow-empty
and ask you which commits to modify. There you change the desired commits frompick
toedit
(for my case that'd be marking all of them asedit
), then you'll be dropped into a detachedHEAD
commit and from here the fun begins.(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?
pressCtrl-C
to stop the loop and check the logs to confirm that the dates match and the signatures are present everywhere with:If everything is okay in the logs, simply issue
git push --force
and you're done. Now you should see thatVerified
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
.