git rebase 不改变提交时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2973996/
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
git rebase without changing commit timestamps
提问by Olivier Verdier
Would it make sense to perform git rebase
while preserving the commit timestamps?
git rebase
在保留提交时间戳的同时执行是否有意义?
I believe a consequence would be that the new branch will not necessarily have commit dates chronologically. Is that theoreticallypossible at all? (e.g. using plumbing commands; just curious here)
我相信结果是新分支不一定按时间顺序有提交日期。这在理论上可能吗?(例如使用管道命令;只是在这里好奇)
If it is theoretically possible, then is it possible in practice with rebase, not to change the timestamps?
如果理论上可行,那么在实践中是否可以使用 rebase,而不是更改时间戳?
For example, assume I have the following tree:
例如,假设我有以下树:
master <jun 2010>
|
:
:
: oldbranch <feb 1984>
: /
oldcommit <jan 1984>
Now, if I rebase oldbranch
on master
, the date of the commit changes from feb 1984 to jun 2010. Is it possible to change that behaviour so that the commit timestamp is not changed? In the end I would thus obtain:
现在,如果我oldbranch
基于master
,提交日期从 1984 年 2 月更改为 2010 年 6 月。是否可以更改该行为,以便提交时间戳不更改?最后我会得到:
oldbranch <feb 1984>
/
master <jun 2010>
|
:
Would that make sense at all? Is it even allowed in git to have a history where an old commit has a more recent commit as a parent?
那会有意义吗?它甚至允许在 git 中有一个旧提交有一个最近提交作为父提交的历史记录吗?
采纳答案by Olivier Verdier
A crucial question of Von C helped me understand what is going on: when your rebase, the committer'stimestamp changes, but not the author'stimestamp, which suddenly all makes sense. So my question was actually not precise enough.
Von C 的一个关键问题帮助我理解了发生了什么:当你的 rebase 时,提交者的时间戳会改变,但作者的时间戳不会改变,这突然就变得有意义了。所以我的问题实际上不够准确。
The answer is that rebase actually doesn't change the author's timestamps (you don't need to do anything for that), which suits me perfectly.
答案是 rebase 实际上不会改变作者的时间戳(你不需要为此做任何事情),这非常适合我。
回答by VonC
Update June 2014: David Frasermentions in the commentsa solution also detailed in "Change timestamps while rebasing git branch", using the option --committer-date-is-author-date
(introduced initially in Jan. 2009 in commit 3f01ad6
2014 年 6 月更新:David Fraser在评论中提到了一个解决方案,该解决方案也在“更改时间戳,同时 rebase git 分支”中详细--committer-date-is-author-date
介绍,使用该选项(最初于 2009 年 1 月在提交 3f01ad6 中引入)
Note that the
--committer-date-is-author-date
option seems to leave the author timestamp, and set the committer timestamp to be the same as the original author timestamp, which is what the OP Olivier Verdierwanted.I found the last commit with the correct date and did:
请注意,该
--committer-date-is-author-date
选项似乎保留了作者时间戳,并将提交者时间戳设置为与原始作者时间戳相同,这正是OP Olivier Verdier想要的。我找到了具有正确日期的最后一次提交并做了:
git rebase --committer-date-is-author-date SHA
See git am
:
见git am
:
--committer-date-is-author-date
By default the command records the date from the e-mail message as the commit author date, and uses the time of commit creation as the committer date.
This allows the user to lie about the committer date by using the same value as the author date.
默认情况下,该命令将来自电子邮件的日期记录为提交作者日期,并使用提交创建时间作为提交者日期。
这允许用户通过使用与作者日期相同的值来谎报提交者日期。
(Original answer, June 2012)
(原答案,2012 年 6 月)
You could try, for a non-interactiverebase
您可以尝试进行非交互式rebase
git rebase --ignore-date
(from this SO answer)
(来自这个SO 答案)
This is passed to git am
, which mentions:
这被传递给git am
,其中提到:
--ignore-date
By default the command records the date from the e-mail message as the commit author date, and uses the time of commit creation as the committer date.
This allows the user to lie about the author date by using the same value as the committer date.
默认情况下,该命令将来自电子邮件的日期记录为提交作者日期,并使用提交创建时间作为提交者日期。
这允许用户通过使用与提交者日期相同的值来谎报作者日期。
For git rebase
, this option is "Incompatible with the --interactive option."
对于git rebase
,此选项是“与 --interactive 选项不兼容”。
Since you can change at will the timestamp of old commit date(with git filter-branch
), I suppose you can organize your Git history with whatever commit date order you want/need, even set it to the future!.
由于您可以随意更改旧提交日期的时间戳(使用git filter-branch
),我想您可以使用您想要/需要的任何提交日期顺序组织您的 Git 历史记录,甚至将其设置为未来!.
As Oliviermentions in his question, the author dateis never changed by a rebase;
From the Pro Git Book:
正如Olivier在他的问题中提到的那样,作者日期永远不会因变基而改变;
来自Pro Git Book:
- The author is the person who originally wrote the work,
- whereas the committer is the person who last applied the work.
So, if you send in a patch to a project and one of the core members applies the patch, both of you get credit.
- 作者是最初撰写作品的人,
- 而提交者是最后应用工作的人。
因此,如果您向项目发送补丁并且其中一个核心成员应用了该补丁,那么你们俩都会获得信用。
To be extra clear, in this instance, as Olivier comments:
更明确地说,在这种情况下,正如奥利维尔评论的那样:
the
--ignore-date
does the opposite of what I was trying to achieve!
Namely, it erases the author's timestamp and replace them with the commits timestamps!
So the right answer to my question is:
Do not do anything, sincegit rebase
does actually not change authors' timestamps by default.
这
--ignore-date
与我试图实现的目标相反!
也就是说,它会删除作者的时间戳并将其替换为提交时间戳!
所以我的问题的正确答案是:
不要做任何事情,因为git rebase
默认情况下实际上不会更改作者的时间戳。
回答by Andy
If you've already screwed up the commit dates (perhaps with a rebase) and want to reset them to their corresponding author dates, you can run:
如果您已经搞砸了提交日期(可能是 rebase)并希望将它们重置为相应的作者日期,您可以运行:
git filter-branch --env-filter 'GIT_COMMITTER_DATE=$GIT_AUTHOR_DATE; export GIT_COMMITTER_DATE'
git filter-branch --env-filter 'GIT_COMMITTER_DATE=$GIT_AUTHOR_DATE; export GIT_COMMITTER_DATE'
回答by weynhamz
By default, git rebase will set the committer's timestamp to the time when the new commit is created, but keep the author's timestamp intact. Most of time, this is the desired behavior, but at some scenarios, we dot not wish to change the commiter's timestamp either. How can we accomplish that? Well, here is the trick I usually do.
默认情况下,git rebase 会将提交者的时间戳设置为创建新提交的时间,但保持作者的时间戳不变。大多数情况下,这是期望的行为,但在某些情况下,我们也不希望更改提交者的时间戳。我们怎样才能做到这一点?嗯,这是我通常做的把戏。
First, make sure each of the commits you are about to rebase has a unique commit message and author timestamp (This is where is trick needs improvements, currently it suits my needs though).
首先,确保您将要变基的每个提交都有唯一的提交消息和作者时间戳(这是技巧需要改进的地方,不过目前它适合我的需求)。
Before the rebase, record the committer's timestamp, author's timestamp and commit message of all the commits which will be rebased to a file.
在 rebase 之前,记录提交者的时间戳、作者的时间戳和所有将被 rebase 到文件的提交的提交消息。
#NOTE: BASE is the commit where your rebase begins
git log --pretty='%ct %at %s' BASE..HEAD > hashlog
Then, let the actual rebase take place.
然后,让实际的变基发生。
Finally, we replace the current committer's timestamp with the one recorded in the file if the commit message is the same by using git filter-branch
.
最后,如果提交消息相同,我们将使用文件中记录的时间戳替换当前提交者的时间戳git filter-branch
。
git filter-branch --env-filter '__date=$(__log=$(git log -1 --pretty="%at %s" $GIT_COMMIT); grep -m 1 "$__log" ../../hashlog | cut -d" " -f1); test -n "$__date" && export GIT_COMMITTER_DATE=$__date || cat'
If something goes wrong, just checkout git reflog
or all the refs/original/
refs.
如果出现问题,只需结帐git reflog
或所有refs/original/
参考。
Furthormore, you can do the similar thing to the author's timestamp.
此外,您可以对作者的时间戳执行类似的操作。
For example, if the author's timestamp of some commits are out of order, and without rearrange these commits, we just want the author's timestamp to show in order, then the following commands will help.
例如,如果某些提交的作者时间戳乱序,并且没有重新排列这些提交,我们只是希望作者的时间戳按顺序显示,那么以下命令将有所帮助。
git log --pretty='%at %s' COMMIT1..COMMIT2 > hashlog
join -1 1 -2 1 <(cat hashlog | cut -f 1 | sort -nr | awk '{ print NR" " }') <(cat hashlog | awk '{ print NR" "##代码## }') | cut -d" " -f2,4- > hashlog_
mv hashlog_ hashlog
git filter-branch --env-filter '__date=$(__log=$(git log -1 --pretty="%s" $GIT_COMMIT); grep -m 1 "$__log" ../../hashlog | cut -d" " -f1); test -n "$__date" && export GIT_AUTHOR_DATE=$__date || cat'