简体   繁体   English

如何使用过去的日期创建git commit?

[英]How to create a git commit with date in the past?

I wonder how would you create a commit in the past? 我不知道您过去如何创建提交?

I've searched for it, and as far as I understand one can use git commit --date"" 我已经搜索了它,据我所知,可以使用git commit --date""

I would really appreciate if someone could clarify this and give an example of the date format. 如果有人可以澄清这一点并举例说明日期格式,我将不胜感激。

For example --date="Wed Feb 16 14:00 2011 +0100" 例如--date="Wed Feb 16 14:00 2011 +0100"

From kernel.org: 从kernel.org:

DATE FORMATS 日期格式

The GIT_AUTHOR_DATE, GIT_COMMITTER_DATE environment variables and the --date option support the following date formats: GIT_AUTHOR_DATE,GIT_COMMITTER_DATE环境变量和--date选项支持以下日期格式:

Git internal format It is , where is the number of seconds since the UNIX epoch. Git内部格式它是,是UNIX时代以来的秒数。 is a positive or negative offset from UTC. 是UTC的正或负偏移量。 For example CET (which is 2 hours ahead UTC) is +0200. 例如,CET(比UTC提前2小时)为+0200。

RFC 2822 The standard email format as described by RFC 2822, for example Thu, 07 Apr 2005 22:13:13 +0200. RFC 2822由RFC 2822描述的标准电子邮件格式,例如2005年4月7日星期四22:13:13 +0200。

ISO 8601 Time and date specified by the ISO 8601 standard, for example 2005-04-07T22:13:13. ISO 8601 ISO 8601标准指定的时间和日期,例如2005-04-07T22:13:13。 The parser accepts a space instead of the T character as well. 解析器也接受空格而不是T字符。

source: http://www.kernel.org/pub/software/scm/git/docs/git-commit.html 来源: http : //www.kernel.org/pub/software/scm/git/docs/git-commit.html

Note that git 2.0.X/2.1 (Q3 2014) will accept more date format. 请注意,git 2.0.X / 2.1(2014年第三季度)将接受更多日期格式。

That means git commit --date=2.days.ago will work! 这意味着git commit --date=2.days.ago将起作用! Easy to set a commit in the past. 轻松设置过去的提交。

See commit 14ac286 by Jeff King ( peff ) 参见Jeff King 提交的14ac286 peff

commit : accept more date formats for " --date " commit :为“ --date ”接受更多日期格式

Right now we pass off the string found by " --date " straight to the fmt_ident function, which will use our strict parse_date to normalize it. 现在,我们将“ --date ”找到的字符串直接传递给fmt_ident函数,它将使用我们严格的parse_date进行规范化。 However, this means obvious things like " --date=now " or " --date=2.days.ago " will not work. 但是,这意味着诸如“ --date=now ”或“ --date=2.days.ago ”之类的显而易见的事情将不起作用。

Instead, let's fallback to the approxidate function to handle this for us. 取而代之的是让我们退回到近似函数来为我们处理这个问题。 Note that we must try parse_date ourselves first, even though approxidate will try strict parsing itself. 请注意,即使近似值将尝试对其进行严格解析,我们也必须先尝试parse_date The reason is that approxidate throws away any timezone information it sees from the strict parsing, and we want to preserve it. 原因是近似值会丢弃它从严格解析中看到的任何时区信息,因此我们希望保留它。 So asking for: 所以要求:

git commit --date="@1234567890 -0700"

continues to set the date in -0700, regardless of what the local timezone is. 无论本地时区是什么,继续将日期设置为-0700。

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

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