简体   繁体   English

我如何压缩 git 中提交日期不是过去的提交?

[英]How do I squash commits in git with a commit date that is not in the past?

So, I have a very simple use case;所以,我有一个非常简单的用例; I want to squash all the commits that need to go back into master from my 'todays working' branch.我想压缩所有需要从我的“今天工作”分支返回到 master 的提交。

Until now I've been using git rebase -i for this, but it doesn't work quite right;到目前为止,我一直在为此使用git rebase -i ,但效果不佳; the timestamp on the final commit isn't right.最终提交的时间戳不正确。

Here's an example of doing this:这是一个这样做的例子:

[work1] git checkout master
Switched to branch 'master'

[work1] git rebase today
First, rewinding head to replay your work on top of it...
Fast-forwarded master to today.

[work1] git log --pretty=format:"%h%x09%an%x09%ad%x09%s"
5ad95ff Doug    Wed Nov 7 10:12:42 2012 +0800   Updated TODO again
abb891c Doug    Wed Nov 7 10:12:24 2012 +0800   Added more work
c5fd35c Doug    Wed Nov 7 10:11:50 2012 +0800   Added more work
a98facd Doug    Wed Nov 7 10:11:22 2012 +0800   Add work
b4465be Doug    Tue Nov 6 21:38:53 2012 -0800   Updated TODO
403cea9 Doug    Fri Jan 2 21:38:53 2009 -0800   Added todo

Right, now I want to squash these last four commits into one commit.是的,现在我想将最后四次提交压缩为一次提交。

[work2] date
Wed  7 Nov 2012 10:39:39 WST

[work2] git rebase -i b4465be

pick a98facd Add work
squash c5fd35c Added more work
squash abb891c Added more work
squash 5ad95ff Updated TODO to reflect todays work

And the result:结果:

[work2] git log
commit 3f7f1d2eb4ef23c73dce95f718152c7d5683a926
Author: Doug <doug@null.com>
Date:   Wed Nov 7 10:11:22 2012 +0800

    Add work
    Added more work
    Added more work
    Updated TODO to reflect todays work

commit b4465bee5b278214704edcfef3f6e222b5b52964
Author: Doug <doug@null.com>
Date:   Tue Nov 6 21:38:53 2012 -0800

    Updated TODO

No!不! That's not what I wanted.不是我想要的。 The timestamp of the resulting commit is the timestamp of the commit we squashed into;结果提交的时间戳是我们压缩的提交的时间戳; what I wanted was the new commit date to be the current time.我想要的是新的提交日期是当前时间。

Just to show exactly what I'm talking about:只是为了准确说明我在说什么:

[work2] date
Wed  7 Nov 2012 10:39:39 WST

Author: Doug <doug@null.com>
Date:   Wed Nov 7 10:11:22 2012 +0800

I want the resulting commit to be date by the merge time, ie.我希望生成的提交在合并时间之前是日期,即。 now, not the time of the commit.现在,不是提交的时间。

As far as I'm aware you can only squash down into a previous commit, not upwards into a new commit, but is there some way of doing this?据我所知,您只能压缩到之前的提交中,而不能向上压缩到新的提交中,但是有什么方法可以做到这一点吗?

The correct solution seems to be正确的解决方案似乎是

  1. create a new commit with the merge message and the correct commit date time,使用合并消息和正确的提交日期时间创建一个新的提交,
  2. ??? ??? <--- somehow squash previous commits into this one. <--- 以某种方式将以前的提交压缩到这个中。

How do I do that?我怎么做?

Hack: You can use黑客:你可以使用

git commit --amend --reset-author

The git commit man page says that this "also renews the author timestamp". git commit 手册页说这“也更新了作者时间戳”。 You don't have to make any changes to the commit (I tried it locally), and it will update the timestamp to the current time.您不必对提交进行任何更改(我在本地尝试过),它会将时间戳更新为当前时间。 Definitely kind of an abuse, but it seems to work.绝对是一种滥用,但它似乎有效。

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

Or, if you do not want to edit the commit message:或者,如果您不想编辑提交消息:

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

By default, amend will update the Committer date to the current time, but leave the Author commit date untouched.默认情况下, amend会将提交者日期更新为当前时间,但保持作者提交日期不变。
--date="now" will also set the Author commit date to the current time. --date="now"还将作者提交日期设置为当前时间。

Why does it matter if Author and Committer date are different?为什么作者和提交者日期不同很重要?
When doing a git log , by default the Author date is shown ( git log --format=fuller shows the Committer date).执行git log时,默认显示作者日期( git log --format=fuller显示提交者日期)。
What may be unexpected, however, is if you use since / until it uses the Committer date, not the Author date.然而,可能出乎意料的是,如果您使用since / until它使用Committer日期,而不是 Author 日期。
This discrepancy can be a bit confusing, or lead to unexpected results.这种差异可能会让人感到困惑,或导致意想不到的结果。
eg: git log --since="yesterday"例如: git log --since="yesterday"

--date - 日期
You can commit or amend, setting the Author date to any date:您可以提交或修改,将作者日期设置为任何日期:

git commit --date="Wed Apr 15 13:00 2037 -0700"
git commit --amend --date="Wed Apr 15 13:00 2037 -0700"

But, you have to use fixed dates, in a format such as ISO 8601 , or Internet Message RFC 2822 Format .但是,您必须使用固定日期,格式如ISO 8601或 Internet Message RFC 2822 Format YYYY.MM.DD, MM/DD/YYYY, DD.MM.YYYY will all work, but I believe a time must also be included. YYYY.MM.DD、MM/DD/YYYY、DD.MM.YYYY 都可以,但我相信还必须包括时间。

source has additional options: https://alexpeattie.com/blog/working-with-dates-in-git来源还有其他选项: https ://alexpeattie.com/blog/working-with-dates-in-git

This SO post shows a method to set Commit and Author dates, independently, to specific dates, and on multiple commits.这篇SO 帖子展示了一种将提交和作者日期独立设置为特定日期和多次提交的方法。 Though it is recommended to not change Commit dates on anything that other people use.尽管建议不要更改其他人使用的任何内容的提交日期。

Instead of git rebase -i b4465be , copy the recent log into the clipboard and do:而不是git rebase -i b4465be ,将最近的日志复制到剪贴板并执行:

git reset --soft b4465be
git commit

paste and edit the change logs, save & exit from the commit message editor.粘贴并编辑更改日志,保存并退出提交消息编辑器。

How about not relying on squash and just diff an entire feature branch: http://dymitruk.com/blog/2012/02/05/branch-per-feature/不依赖壁球而只区分整个功能分支怎么样:http: //dymitruk.com/blog/2012/02/05/branch-per-feature/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM