0

Having two commits SHA1, how do I get which went first without manually analyzing the git log?

Note that I am not trying to find which one is the ancestor of the another, I want to know which one was pushed first.

I want to get it by it's chronological order in the git history, not by the commit date. Any method is welcome but a straight forward way is preferred (I am using it directly on my terminal emulator, not on a script).


I tried using git merge-base as @wRAR suggested.

First off, there is a drawback: git merge-base --is-ancestor doesn't give me output. It exits with 0 or 1 without outputing anything to stdout. Its ok when scripting, but when manually using it directly on my terminal I have to check the exit status in a extra step afterwards (echo $?on bash, $LASTEXITCODEon PowerShell). Not a deal breaker, but generates a little hassle.

But, anyway, it didn't work.

Works in the following scenario:

> git log --oneline --graph
| * <SHA1 D> Fourth commit.
| * <SHA1 C> Third commit.
| * <SHA1 B> Second commit.
| * <SHA1 A> First commit.
> git merge-base --is-ancestor <SHA1 B> <SHA1 C>
> echo $?
0
> git merge-base --is-ancestor <SHA1 C> <SHA1 B>
> echo $?
1

But not in this one (doesn't do what I intend):

> git log --oneline --graph
*   <SHA1 D> Merged work.
|\
| * <SHA1 C> Paul's first commit.
* | <SHA1 B> John's second commit.
|/
* <SHA1 A> John's first commit.
> git merge-base --is-ancestor <SHA1 B> <SHA1 C>
> echo $?
1
> git merge-base --is-ancestor <SHA1 C> <SHA1 B>
1
talles
  • 14,356
  • 8
  • 45
  • 58

2 Answers2

1

You need to compare the commit date of both commits. It can be seen in git show (or in any other suitable command) with --format=fuller or with a custom format including the commit date field.

wRAR
  • 25,009
  • 4
  • 84
  • 97
0

The only order git knows about is the one given by the ancestor relationship. Commit times, etc depend on the (possibly going with the moon) clock of the machine where the commiting is done, could be the original commit instant of a commit that was since amended/edited several times, and could even be totally fake.

vonbrand
  • 11,412
  • 8
  • 32
  • 52