Determine Which Git Commit is Older

If you’ve ever had to debug a live issue, you may have had to diagnose specific commits to determine if they could have been the cause of the issue. You might have a suspect commit, but how can you easily tell if that commit is included in the release of the app at the time of the issue?

Pretty simple with a few lines of Bash code! We can use the git merge-base --is-ancestor command to determine which of the two provided commits are older.

/usr/local/bin/later
#!/bin/bash

# If two args are provided, compare them directly, otherwise allow typing
# multiple hashes to compare against the base.
if [[ -n "$2" ]]; then
	git merge-base --is-ancestor "$1" "$2" && echo "$1 is older" || echo "$2 is older"
else
	while IFS= read -r hash; do
		git merge-base --is-ancestor "$1" "$hash" && echo "$1 is older" || echo "$hash is older"
	done
fi

Usage is pretty simple:

later b0f4ea89 dcde59ca