1

To get the commit of the latest tag, I'm doing

$(git rev-parse $(git describe --tags --abbrev=0))

But if there is no tag at all, it will throw the error fatal: No names found, cannot describe anything. So in case, there is no tag at all, I would like to get the first commit at all. How do I have to do it?

$(git rev-parse $(git describe --tags --abbrev=0 || <?-- get the very first commit -->))
user3142695
  • 15,844
  • 47
  • 176
  • 332
  • 1
    Are you looking for https://stackoverflow.com/questions/1006775/how-to-reference-the-initial-commit ? – Joe Aug 13 '21 at 12:38
  • 1
    I would preemptively tag the first commit; that tag would either be ignored (if there is already a later tag) or prevent `git rev-parse` failing. – chepner Aug 13 '21 at 14:12
  • @chepner Oh, that's a good point. Easy, but didn't see this option... – user3142695 Aug 13 '21 at 18:28

1 Answers1

0

One way to get the first commit in the repository is to use git rev-list and ask for the list of commits in reverse topological order:

git rev-list --topo-order --reverse --all | head -1

This will return the SHA-1 hash of the first commit in the entire history graph, regardless of when it was created.

Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154