josaphatv
josaphatv

Reputation: 643

How to get commit from git given unix timestamp of commit?

Because of reasons, the "version" in my application is a timestamp (generated with git show --format=%ct). So the bug report says that vApp-<timestamp> is broken.

How would I go about reversing the operation and getting the commit hash from the timestamp using git?

I can run git log --date=unix and then rely on less to search for the timestamp, but I was hoping there was something more "built-in" to git.

This is distinct from the alleged duplicate because I don't want "all commits for a specific day", I want a single commit given the timestamp of that commit.

Upvotes: 3

Views: 945

Answers (1)

TTT
TTT

Reputation: 28869

Suppose your Unix timestamp is 1635020939, then this should do it:

git log --date=unix --before=1635020939 --after=1635020939 --pretty=format:"%h"

Note that Git only stores timestamps to 1-second resolution, so you are not guaranteed to get only 1 commit ID. If multiple commits have the identical committer date timestamp, they all will be returned.

Upvotes: 6

Related Questions