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

17
我尝试将最新提交的提交者日期设为其作者日期。通常使用命令 git rebase --committer-date-is-author-date HEAD~1 可以实现。但不幸的是,只有一个提交,这意味着我必须使用--root而不是HEAD~1,但出于某种原因,git rebase --committer-date-is-author-date --root未能将提交者日期设置为作者日期。我该怎么办?

1
请尝试在Git 2.19(2018年第三季度)中重新使用git rebase --committer-date-is-author-date --root命令,现在应该可以正常工作。请参见下面的答案 - VonC
4个回答

18

简述;给我一个可用的命令行

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


4
但这并没有给提交打上签名。如何修改以添加 GPG 签名,就像运行 git commit --amend --no-edit -n -S 时一样? - gMale
要重新签署它们,您可以运行 git filter-branch -f --commit-filter 'git commit-tree -S "$@";' -- --all 来再次重新签署它们。 - kiler129

11

坏消息

不幸的是,git rebase --root使用交互式rebase代码(因为非交互式代码无法“重放”根提交),而--committer-date-is-author-date实际上是传递给git am的标志,它实现了简单的非交互式情况。

好消息

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

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

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,这正是您想要的。


3
FYI: --amend --no-edit --date="..."听起来像一个常用的用例,通常可以缩写为-C sha,其中SHA指向我们想要获取作者日期、作者电子邮件和提交消息的提交ID。不幸的是,-C没有涉及'committer'部分,因此该部分仍然需要使用环境变量。 - quetzalcoatl

7

但是由于某些原因,git rebase --committer-date-is-author-date --root 没有将提交者日期设置为作者日期。

实际上,从 Git 23.19(2018年第三季度)开始,它可能会正确设置。

git rebase -i 创建的 "author-script" 文件在我们开始将命令远离 shell 脚本时出现了问题,现在正在修复。

请看 提交 5522bba, 提交 67f16e3, 提交 0f16c09, 提交 ca3e182 (2018年7月31日) 由 Eric Sunshine (sunshineco) 提交。
(由Junio C Hamano -- gitster --合并于提交 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.


1
git rebase --committer-date-is-author-date --root 命令将会输出以下错误信息:fatal: cannot combine am options with either interactive or merge options (git version 2.21.0.windows.1)。但这是可以预料到的:https://git-scm.com/docs/git-rebase#_incompatible_options - 40detectives
@40detectives 奇怪:只有在与 --onto 结合使用时,它才与 --root 不兼容。 - VonC

0

git rebase --committer-date-is-author-date 在较新的 git 版本中可以正常工作。 请注意,在 Ubuntu 上,使用 apt 安装的版本无法正常工作,您需要按照此 https://itsfoss.com/install-git-ubuntu/ 安装更新版本。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接