How to return only the last commit date?

Asked

Viewed 38 times

4

I know if I use the remote git log I can see commit ids in my repository and if I use git rev-parse HEAD, I can see the name of the last commit. But I needed to return only the date information of my last commit.

Example:

commit 49fb9be4b5e655dd8104e79eb5dc824cc7c782d1 
Author: WallaceMaxters 
Date:   Tue Nov 17 14:44:03 2020 -0300

    alteração 3

commit 8a94c729e8095060afb3d58402b113852b9429ff 
Author: WallaceMaxters 
Date:   Tue Nov 10 16:31:45 2020 -0300

    alteração 2

commit e1b958709236f81d7e11d5bb38ab46b4a16b9b62
Author: WallaceMaxters 
Date:   Tue Oct 27 16:11:30 2020 -0300

    alteração 1

In the above case, I would like to return only the date Tue Nov 17 14:44:03 2020 -0300 through some of the commands of the git.

Is it possible to do this? I only need the date information for the last commit.

1 answer

5


You can use the option --format, using the placeholder %cd, thus:

git log --format=%cd

That returns only the dates for the latter commits. To return the date of the last commit, just use the range -1. Thus:

git log --format=%cd -1

The option --format (of command git log) works based on what the Git documentation refers to as Pretty-formats. See the reference for other formatting cases.

In that case, the placeholder %cd, according to the documentation:

%cd
committer date (format respects --date= option)

Note that, according to the documentation of the placeholder %cd points, the option --date is respected. This way, you can change the "default" of the date. See:

$ git log  --format=%cd -1
Mon Oct 26 12:14:45 2020 -0300

$ git log --date=iso --format=%cd -1
2020-10-26 12:14:45 -0300

See the documentation of --date to learn more.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.