22

One thing I miss about using svn was the simple-numbering of revision numbers. I can easily see if the version deployed in the testing environment is before or after a certain commit.

With git using hashes for its commits, what is a way to tell if a commit was made before or after another commit?

7 Answers 7

19

Things, as you note, aren't so simple in Git. In particular, the definition of "before" and "after" need a little more clarification.

If you trust the people committing to your repository not to mess with timestamps, and you already know both commits are on the same branch, you can compare the timestamps of the commits and just see which one is earlier. Use the following command for each commit, and compare the result.

git log -1 --format='%ci' <commit>

With Git, you can't necessarily trust the timestamps, however, since unlike Subversion there isn't a central repository with a job of producing them. You also can't be sure that two commits are on the same branch (although that problem also exists with Subversion).

To avoid those problems, talk about whether one commit is an ancestor of another, rather than whether it comes before or after it. In the following commit graph, B and C are ancestors of A, while B is not an ancestor of C nor vice versa:

B    (master)
| C  (branch)
|/
A    (root)

To determine whether commit A is an ancestor of commit B, use the following command (based on an article at git ready):

git rev-list <commitA> | grep $(git rev-parse <commitB>)

The first part lists all the commits that are ancestors of commit A; the second part gets the full hash of commit B, then searches the ancestor list for that commit. If you see any output, commit A is an ancestor of commit B. Swap the arguments to work out if commit A is an ancestor of commit B instead.

This second form is slower, but gives you absolute certainty that one commit is an ancestor of another.

4
  • So what does the timestamp represent when I do git log -1 --format='%ci' <commit>? I'm just trying to understand a scenario when I can't trust the timestamp.
    – Glide
    Jan 9, 2013 at 21:49
  • 1
    @Glide: That timestamp represents the time the commit was made. However, because the commit is initially made on the developer's local repository, if the clock on their computer is wrong, or they use a trick like playing with GIT_COMMITTER_DATE, that timestamp could be incorrect.
    – me_and
    Jan 10, 2013 at 11:33
  • 7
    Hey, the command works but what you said about how it works is incorrect. The git rev-list command actually determines whether commit B is an ancestor of commit A. You say "the first part lists all the commits that are ancestors of commit A". Which it apparently does, from some testing I just did. Therefore if you're grepping through that list for a commit SHA, then the grep will success IFF commit B is in that ancestor list, i.e. is an ancestor of commit A -- the opposite of what you wrote.
    – eeeeaaii
    May 1, 2017 at 19:51
  • 2
    git rev-list <commitA> | grep <commitB> returns 0 (true) if commitA is after B, returns 1 (false) if commitA is before B. This is because git rev-list returns all "ancestor commits" of commitA. Apr 27, 2018 at 3:25
14

git merge-base has a flag for that. It simply returns a 0/1 exit code.

git merge-base --is-ancestor <commit> <commit>
1
  • 3
    Or do a plain merge-base, if the result is either of the two commits that's the one that came first, or it could be a third commit, that makes them cousins or siblings, or it could be nothing, in which case they're from unrelated histories.
    – jthill
    Dec 20, 2018 at 0:51
7

git rev-list --count can help. Supposing 110a187 comes 4 commits before 5d41af1, then:

 $ git rev-list --count 110a187..5d41af1
4
 $ git rev-list --count 5d41af1..110a187
0

Thus something like:

test $(git rev-list --count $a..$b) == 0 && \
       echo "$a is not an ancestor of $b"
1
  • If @Glide means parent/descendant rather than temporal, then this answer is more accurate in the case where the commits came into a branch via merges from different branches.
    – Chry Cheng
    Aug 28, 2015 at 11:32
3

Use git show on each commit to find the date of the commit. There's no way just looking at the hash to determine order, since it's nothing but a hash.

You can use custom format strings to show just what you want, check the man pages for more info.

git show --format="%ci" <commit>
2

You can get all commit info from the log which shows the hash, author, date, and comment:

git log

You can pull a specific commit's time/date from the commit log. Something like:

git log -1 --format="%cd" <commit>

The '%cd' gives you the date in the log's format, you can also do:

  • %cD: RFC2822 style
  • %cr: relative
  • %ct: UNIX timestamp
  • %ci: ISO 8601 format

From there, you could do some comparisons with some scripting if you need to automate.

1

With all the remarks mentioned by @me_and, you can also try:

git rev-list --no-walk commitA commitB

In output you will see provided commits displayed in reverse chronological order by commit time. So it's order of provided commits does not matter (in contradiction to idea suggested by @Douglas Bagnall).

1
  • Doesn't seem to work for me. If I flip the commits around the output also shows a different order. Maybe you forgot a topology flag?
    – user187676
    Jul 18, 2020 at 16:39
0

If you use tags you can do

git describe --tags

This will tell you how many commits since the last tag.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.