Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

command line arguments - What is the format for --date parameter of git commit

I need to overwrite the date of the commit of git, all the documentation points to --date parameter, but then leaves one without a clue to the appropriate format. I've tried every permutation i can think of, and i'm getting "fatal: invalid date format:" error for each and every one.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Git 2.6+ (Q3 2015) add a new option.

See commit e4f031e (30 Jun 2015), and commit aa1462c, commit a5481a6, commit b7c1e11 (25 Jun 2015) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit d939af1, 03 Aug 2015)

introduce "format" date-mode

This feeds the format directly to strftime.
Besides being a little more flexible, the main advantage is that your system strftime may know more about your locale's preferred format (e.g., how to spell the days of the week).

--date=format:... feeds the format ... to your system strftime.
Use --date=format:%c to show the date in your system locale's preferred format.
See the strftime manual for a complete list of format placeholders.

Davide Cavestro proposes in the comments the example:

git commit -m "Test" --date=format:relative:5.hours.ago 

Original answer (mid 2014)

The --date option (introduced in commit 02b47cd in Dec. 2009, for git1.7.0) uses the same format than for GIT_AUTHOR_DATE, with date formats tested in commit 96b2d4f:

There you can see the various format accepted:

  • rfc2822: Mon, 3 Jul 2006 17:18:43 +0200
  • iso8601: 2006-07-03 17:18:43 +0200
  • local: Mon Jul 3 15:18:43 2006
  • short: 2006-07-03 (not in 1.9.1, works in 2.3.0)
  • relative: see commit 34dc6e7:

    5.seconds.ago, 
    2.years.3.months.ago, 
    '6am yesterday'
    
  • raw: see commit 7dff9b3 (git 1.6.2, March 2009)
    internal raw git format - seconds since epoch plus timezone
    (put another way: 'date +"%s %z"' format)

  • default: Mon Jul 3 17:18:43 2006 +0200

ADTC asks and answers in the comments:

Does it accept 2006-07-03 15:18:43 for local?

Yes it does work and it takes the local time zone automatically.
With that format I don't need to bother which day of the week it is (Sun, Mon, etc).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...