git rebase --committer-date-is-author-date --root 不起作用

git rebase --committer-date-is-author-date --root does not work

我尝试将最新提交的提交者日期设置为其作者日期。通常这适用于 git rebase --committer-date-is-author-date HEAD~1。不幸的是只有一个提交,这意味着我必须使用 --root 而不是 HEAD~1 但是 git rebase --committer-date-is-author-date --root 出于某种原因没有将提交者日期设置为作者日期。我能做什么?

坏消息

不幸的是git rebase --root使用了交互式rebase代码(因为非交互式代码不能"replay"根提交),--committer-date-is-author-date实际上是一个标志传递给git am,它实现了简单的非交互案例。

好消息

git rebase 所做的,在基本层面上,是 copy 一些提交(通常在复制过程中进行某种更改),然后指向最终此类复制提交的分支名称。如果您只想在复制时更改一个提交,则可以使用 git commit --amend 而不是 git rebase1 如果在复制中只有一个提交整个存储库,只有一个提交需要在复制时更改,因此这种情况适用。

而不是 --committer-date-is-author-date,您需要使用 GIT_COMMITTER_DATE 变量将提交时间戳设置为某个任意值。您还可以使用 --author and/or --date 来覆盖作者姓名 and/or 时间戳。因此:

t='2017-09-01 12:34:56'
GIT_COMMITTER_DATE="$t" git commit --amend --date="$t"

会将两个时间戳设置为 2017 年 9 月 1 日 12:34:56。 (我在这里使用了一个 shell 变量 t 来避免输入相同的时间戳两次。)

(如果您不想编辑提交消息,请添加 --no-edit。请记住,新提交将使用当前索引中的任何内容!如果您在提取 HEAD 提交后更改了索引,你可能想先将 HEAD 提交复制到一个临时索引,然后使用它。)


1这假定您要进行的更改是,例如,提交消息文本或日期或作者等,而不是提交的父 ID。根提交的定义是没有父 ID,git commit --amend 将继续没有父 ID,这正是您在这种情况下想要的。

But git rebase --committer-date-is-author-date --root does not set the committer date to the author date for some reason.

实际上,它可能设置正确,从 Git 23.19 (Q3 2018)

当我们开始将命令从 shell 脚本中移开时,“git rebase -i”创建的“author-script”文件损坏了,该脚本现在正在修复。

参见 commit 5522bba, commit 67f16e3, commit 0f16c09, commit ca3e182 (31 Jul 2018) by Eric Sunshine (sunshineco)
(由 Junio C Hamano -- gitster -- in commit 1bc505b 合并,2018 年 8 月 17 日)

sequencer: fix "rebase -i --root" corrupting author header timestamp

When "git rebase -i --root" creates a new root commit, it corrupts the "author" header's timestamp by prepending a "@":

author A U Thor <author@example.com> @1112912773 -0700

The commit parser is very strict about the format of the "author" header, and does not allow a "@" in that position.

The "@" comes from GIT_AUTHOR_DATE in "rebase-merge/author-script", signifying a Unix epoch-based timestamp, however, read_author_ident() incorrectly allows it to slip into the commit's "author" header, thus corrupting it.

One possible fix would be simply to filter out the "@" when constructing the "author" header timestamp, however, a more correct fix is to parse the GIT_AUTHOR_DATE date (via parse_date()) and format the parsed result into the "author" header.
Since "rebase-merge/author-script" may be edited by the user, this approach has the extra benefit of catching other potential timestamp corruption due to hand-editing.

We can do better than calling parse_date() ourselves and constructing the "author" header manually, however, by instead taking advantage of fmt_ident() which does this work for us.

TL;DR 给我一个有效的命令行

git filter-branch --env-filter 'export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"'