힘내 로그 날짜 형식을 변경하는 방법
Git 내에서 마지막 커밋을 표시하려고하지만 특수 형식의 날짜가 필요합니다.
로그 형식 %ad
이 --date
형식과 관련 이 있다는 것을 알고 있지만 --date
찾을 수 있는 유일한 형식은 "짧음"입니다. 나는 다른 사람들을 알고 싶어하며 다음과 같은 사용자 정의 항목을 만들 수 있는지 여부를 알고 싶습니다.
git -n 1 --date=**YYMMDDHHmm** --pretty=format:"Last committed item in this release was by %%an, %%aD, message: %%s(%%h)[%%d]"
다른 사람은 (에서 git help log
) :
--date=(relative|local|default|iso|rfc|short|raw)
Only takes effect for dates shown in human-readable format,
such as when using "--pretty". log.date config variable
sets a default value for log command’s --date option.
--date=relative shows dates relative to the current time, e.g. "2 hours ago".
--date=local shows timestamps in user’s local timezone.
--date=iso (or --date=iso8601) shows timestamps in ISO 8601 format.
--date=rfc (or --date=rfc2822) shows timestamps in RFC 2822 format,
often found in E-mail messages.
--date=short shows only date but not time, in YYYY-MM-DD format.
--date=raw shows the date in the internal raw git format %s %z format.
--date=default shows timestamps in the original timezone
(either committer’s or author’s).
사용자 정의 형식을 만드는 기본 방법은 없지만 쉘 마법을 수행 할 수 있습니다.
timestamp=`git log -n1 --format="%at"`
my_date=`perl -e "print scalar localtime ($timestamp)"`
git log -n1 --pretty=format:"Blah-blah $my_date"
첫 번째 단계는 밀리 초 타임 스탬프입니다. 그러나 원하는 타임 스탬프 형식을 지정하기 위해 두 번째 줄을 변경할 수 있습니다. 이 예제는 --date=local
패딩 일과 비슷한 것을 제공합니다 .
매번 입력하지 않고 영구적 인 효과를 원한다면
git config log.date iso
또는이 계정의 모든 자식 사용에 영향을 미치기 위해
git config --global log.date iso
이외에도 --date=(relative|local|default|iso|iso-strict|rfc|short|raw)
다른 사람들이 언급했듯이 사용자 정의 로그 날짜 형식을
--date=format:'%Y-%m-%d %H:%M:%S'
이것은 다음과 같은 것을 출력합니다 2016-01-13 11:32:13
.
참고 : 아래에 연결된 커밋을 살펴보면 적어도 Git v2.6.0-rc0이것이 작동 해야한다고 생각합니다 .
전체 명령에서 다음과 같습니다.
git config --global alias.lg "log --graph --decorate
-30 --all --date-order --date=format:'%Y-%m-%d %H:%M:%S'
--pretty=format:'%C(cyan)%h%Creset %C(black bold)%ad%Creset%C(auto)%d %s'"
나는 어디서나 문서에서 이것을 찾을 수 없었습니다.
이것에 대한 문서를 검색 할 때 형식이 직접 제공되었음을 나타내는 Git 자체에 대한 커밋을 발견 했습니다strftime
. 찾은 자리 표시자를 찾고 strftime
( 여기 또는 여기 ) 나열된 자리 표시 자와 일치합니다.
자리 표시 자에는 다음이 포함됩니다.
%a Abbreviated weekday name
%A Full weekday name
%b Abbreviated month name
%B Full month name
%c Date and time representation appropriate for locale
%d Day of month as decimal number (01 – 31)
%H Hour in 24-hour format (00 – 23)
%I Hour in 12-hour format (01 – 12)
%j Day of year as decimal number (001 – 366)
%m Month as decimal number (01 – 12)
%M Minute as decimal number (00 – 59)
%p Current locale's A.M./P.M. indicator for 12-hour clock
%S Second as decimal number (00 – 59)
%U Week of year as decimal number, with Sunday as first day of week (00 – 53)
%w Weekday as decimal number (0 – 6; Sunday is 0)
%W Week of year as decimal number, with Monday as first day of week (00 – 53)
%x Date representation for current locale
%X Time representation for current locale
%y Year without century, as decimal number (00 – 99)
%Y Year with century, as decimal number
%z, %Z Either the time-zone name or time zone abbreviation, depending on registry settings
%% Percent sign
In a full command it would be something like
git config --global alias.lg "log --graph --decorate -30 --all --date-order --date=format:'%Y-%m-%d %H:%M:%S' --pretty=format:'%C(cyan)%h%Creset %C(black bold)%ad%Creset%C(auto)%d %s'"
After a long time looking for a way to get git log
output the date in the format YYYY-MM-DD
in a way that would work in less
, I came up with the following format:
%ad%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08
along with the switch --date=iso
.
This will print the date in ISO format (a long one), and then print 14 times the backspace character (0x08), which, in my terminal, effectively removes everything after the YYYY-MM-DD part. For example:
git log --date=iso --pretty=format:'%ad%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%aN %s'
This gives something like:
2013-05-24 bruno This is the message of the latest commit.
2013-05-22 bruno This is an older commit.
...
What I did was create an alias named l
with some tweaks on the format above. It shows the commit graph to the left, then the commit's hash, followed by the date, the shortnames, the refnames and the subject. The alias is as follows (in ~/.gitconfig):
[alias]
l = log --date-order --date=iso --graph --full-history --all --pretty=format:'%x08%x09%C(red)%h %C(cyan)%ad%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08 %C(bold blue)%aN%C(reset)%C(bold yellow)%d %C(reset)%s'
You can use the field truncation option to avoid quite so many %x08
characters. For example:
git log --pretty='format:%h %s%n\t%<(12,trunc)%ci%x08%x08, %an <%ae>'
is equivalent to:
git log --pretty='format:%h %s%n\t%ci%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08, %an <%ae>'
And quite a bit easier on the eyes.
Better still, for this particular example, using %cd
will honor the --date=<format>
, so if you want YYYY-MM-DD
, you can do this and avoid %<
and %x08
entirely:
git log --date=short --pretty='format:%h %s%n\t%cd, %an <%ae>'
I just noticed this was a bit circular with respect to the original post but I'll leave it in case others arrived here with the same search parameters I did.
I needed the same thing and found the following working for me:
git log -n 1 --pretty='format:%cd' --date=format:'%Y-%m-%d %H:%M:%S'
The --date=format
formats the date output where the --pretty
tells what to print.
Be aware of the "date=iso
" format: it isn't exactly ISO 8601.
See commit "466fb67" from Beat Bolli (bbolli
), for Git 2.2.0 (November 2014)
pretty: provide a strict ISO 8601 date format
Git's "ISO" date format does not really conform to the ISO 8601 standard due to small differences, and it cannot be parsed by ISO 8601-only parsers, e.g. those of XML toolchains.
The output from "
--date=iso
" deviates from ISO 8601 in these ways:
- a space instead of the
T
date/time delimiter- a space between time and time zone
- no colon between hours and minutes of the time zone
Add a strict ISO 8601 date format for displaying committer and author dates.
Use the '%aI
' and '%cI
' format specifiers and add '--date=iso-strict
' or '--date=iso8601-strict
' date format names.
See this thread for discussion.
date -d @$(git log -n1 --format="%at") +%Y%m%d%H%M
Note that this will convert to your local timezone, in case that matters for your use case.
Git 2.7 (Q4 2015) will introduce -local
as an instruction.
It means that, in addition to:
--date=(relative|local|default|iso|iso-strict|rfc|short|raw)
you will also have:
--date=(default-local|iso-local|iso-strict-local|rfc-local|short-local)
The -local
suffix cannot be used with raw
or relative
. Reference.
You now can ask for any date format using the local timezone. See
- commit 99264e9,
- commit db7bae2,
- commit dc6d782,
- commit f3c1ba5,
- commit f95cecf,
- commit 4b1c5e1,
- commit 8f50d26,
- commit 78a8441,
- commit 2df4e29 (03 Sep 2015) by John Keeping (
johnkeeping
).
See commit add00ba, commit 547ed71 (03 Sep 2015) by Jeff King (peff
).
(Merged by Junio C Hamano -- gitster
-- in commit 7b09c45, 05 Oct 2015)
In particular, the last from above (commit add00ba) mentions:
date
: make "local
" orthogonal to date format:Most of our "
--date
" modes are about the format of the date: which items we show and in what order.
But "--date=local
" is a bit of an oddball. It means "show the date in the normal format, but using the local timezone".
The timezone we use is orthogonal to the actual format, and there is no reason we could not have "localized iso8601", etc.This patch adds a "
local
" boolean field to "struct date_mode
", and drops theDATE_LOCAL
element from thedate_mode_type
enum (it's now justDATE_NORMAL
pluslocal=1
).
The new feature is accessible to users by adding "-local
" to any date mode (e.g., "iso-local
"), and we retain "local
" as an alias for "default-local
" for backwards compatibility.
The format option %ai
was what I wanted:
%ai
: author date, ISO 8601-like format
--format="%ai"
git log -n1 --format="Last committed item in this release was by %an, `git log -n1 --format=%at | awk '{print strftime("%y%m%d%H%M",$1)}'`, message: %s (%h) [%d]"
I need the date in a special format.
With Git 2.21 (Q1 2019), a new date format "--date=human
" that morphs its output depending on how far the time is from the current time has been introduced.
"--date=auto
" can be used to use this new format when the output is going to the pager or to the terminal and otherwise the default format.
See commit 110a6a1, commit b841d4f (29 Jan 2019), and commit 038a878, commit 2fd7c22 (21 Jan 2019) by Stephen P. Smith (``).
See commit acdd377 (18 Jan 2019) by Linus Torvalds (torvalds
).
(Merged by Junio C Hamano -- gitster
-- in commit ecbe1be, 07 Feb 2019)
Add 'human' date format documentation
Display date and time information in a format similar to how people write dates in other contexts.
If the year isn't specified then, the reader infers the date is given is in the current year.By not displaying the redundant information, the reader concentrates on the information that is different.
The patch reports relative dates based on information inferred from the date on the machine running thegit
command at the time the command is executed.While the format is more useful to humans by dropping inferred information, there is nothing that makes it actually human.
If the 'relative
' date format wasn't already implemented, then using 'relative
' would have been appropriate.Add
human
date format tests.When using
human
several fields are suppressed depending on the time difference between the reference date and the local computer date.
- In cases where the difference is less than a year, the year field is suppressed.
- If the time is less than a day; the month and year is suppressed.
check_date_format_human 18000 "5 hours ago" # 5 hours ago
check_date_format_human 432000 "Tue Aug 25 19:20" # 5 days ago
check_date_format_human 1728000 "Mon Aug 10 19:20" # 3 weeks ago
check_date_format_human 13000000 "Thu Apr 2 08:13" # 5 months ago
check_date_format_human 31449600 "Aug 31 2008" # 12 months ago
check_date_format_human 37500000 "Jun 22 2008" # 1 year, 2 months ago
check_date_format_human 55188000 "Dec 1 2007" # 1 year, 9 months ago
check_date_format_human 630000000 "Sep 13 1989" # 20 years ago
## Replace the proposed '
auto
' mode with 'auto:
'In addition to adding the '
human
' format, the patch added theauto
keyword which could be used in the config file as an alternate way to specify the human format. Removing 'auto' cleans up the 'human
' format interface.Added the ability to specify mode '
foo
' if the pager is being used by usingauto:foo
syntax.
Therefore, 'auto:human
' date mode defaults tohuman
if we're using the pager.
So you can do:git config --add log.date auto:human
and your "
git log
" commands will show the human-legible format unless you're scripting things.
Git 2.24 (Q4 2019) simplified the code.
See commit 47b27c9, commit 29f4332 (12 Sep 2019) by Stephen P. Smith (``).
(Merged by Junio C Hamano -- gitster
-- in commit 36d2fca, 07 Oct 2019)
Quit passing 'now' to date code
Commit b841d4f (Add
human
format to test-tool, 2019-01-28, Git v2.21.0-rc0) added aget_time()
function which allows$GIT_TEST_DATE_NOW
in the environment to override the current time.
So we no longer need to interpret that variable incmd__date()
.Therefore, we can stop passing the "
now
" parameter down through the date functions, since nobody uses them.
Note that we do need to make sure all of the previous callers that took a "now
" parameter are correctly usingget_time()
.
Use Bash and the date command to convert from an ISO-like format to the one you want. I wanted an org-mode date format (and list item), so I did this:
echo + [$(date -d "$(git log --pretty=format:%ai -1)" +"%Y-%m-%d %a %H:%M")] \
$(git log --pretty=format:"%h %s" --abbrev=12 -1)
And the result is for example:
+ [2015-09-13 Sun 22:44] 2b0ad02e6cec Merge pull request #72 from 3b/bug-1474631
참고URL : https://stackoverflow.com/questions/7853332/how-to-change-git-log-date-formats
'development' 카테고리의 다른 글
설치된 보석 목록? (0) | 2020.07.02 |
---|---|
파이썬에서 반복 건너 뛰기 (0) | 2020.07.02 |
Mac에 pip3를 설치하는 방법은 무엇입니까? (0) | 2020.07.02 |
버튼 누름시 가상 키보드 닫기 (0) | 2020.07.02 |
롤리팝 : 색상을 투명하게 설정하여 statusBar 뒤에 그리기 (0) | 2020.07.02 |