如何使用不在过去的提交日期来压缩git中的提交? [英] How do I squash commits in git with a commit date that is not in the past?

查看:93
本文介绍了如何使用不在过去的提交日期来压缩git中的提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个非常简单的用例;我想压缩所有需要从我的今日工作"分支重新回到master的提交.

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.

直到现在,我一直在使用git rebase -i,但是它不能正常工作.最终提交的时间戳不正确.

Until now I've been using git rebase -i for this, but it doesn't work quite right; 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

结果:

[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.

仅显示我在说什么:

[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?

正确的解决方案似乎是

  1. 使用合并消息和正确的提交日期时间创建新的提交
  2. ??? < ---以某种方式将先前的提交压缩到此.

我该怎么做?

推荐答案

技巧:您可以使用

git commit --amend --reset-author

git commit手册页说,这也更新了作者的时间戳".您不必对提交进行任何更改(我在本地尝试过),它将把时间戳更新为当前时间.绝对是一种滥用,但这似乎奏效.

The git commit man page says that this "also renews the author timestamp". 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中的提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆