-1

Excepting pathological cases, every Git commit points to one commit (typical use) or two commits (when merging). Given the hash of a commit, how do I see what commit(s) it points to?

I'm looking for a formula. I know I can visually parse the output of

$ git log --graph --pretty=oneline --abbrev-commit

but I would prefer something scriptable, something that gives

f(hash in) -> hash(es) out

directly. The information is right there in the commit file, it shouldn't be so hard to pull out. I can't find anything from web searches, though, including Stack Overflow.

Borea Deitz
  • 379
  • 4
  • 11
  • There are commits that have no _parents_. They are _root_ commits and they are not like scary. Every commit history starts with at least one commit like that. Then, to your point, you can ask git show for it: `git show --quiet --pretty=%p `.... or you can instead parse `parent` lines using `git cat-file -p `. – eftshift0 Aug 03 '23 at 20:40
  • `git rev-parse hash_in~` <- note the tilda, it means ["the parent commit"](https://git-scm.com/docs/gitrevisions#Documentation/gitrevisions.txt-emltrevgtltngtemegemHEADmaster3em). – phd Aug 03 '23 at 20:50
  • 1
    @phd That only shows one parent for commits with multiple parents. – user229044 Aug 03 '23 at 20:56
  • 2
    @user229044 Yep, you're right. For all parents: [`git rev-parse hash_in^@`](https://git-scm.com/docs/gitrevisions#_other_rev_parent_shorthand_notations). – phd Aug 03 '23 at 21:04
  • @phd `git rev-parse hash_in^@` is perfect. It gives all answers and is easy to read into an array. – Borea Deitz Aug 04 '23 at 20:29

0 Answers0