2

I assume that a git commit hash (5743a31610d38064af35573b91e3bbe39d808b9b) will always map to 0 or 1 git branches? Is there a reliable way to trace what the branch name was when the git commit was created? From what I know a git branch is a pointer to a commit, given a commit can we reliably point to a branch (if the branch still exists)?

Context: I am creating some devops deployment change-logs so we can keep track of what we changed - I am wondering if it's worthwhile to name the git branches that were released, or just put the git commit hash in there, and we can reverse lookup the branch name if we really want to?

Alexander Mills
  • 1
  • 80
  • 344
  • 642

1 Answers1

5

I assume that a git commit hash (5743a31610d38064af35573b91e3bbe39d808b9b) will always map to 0 or 1 git branches?

This is a bad assumption. In a typical repository, there's one commit—the root commit—that's on every branch.

The opposite direction holds though: from a name (branch or tag name, or really, any reference that identifies a commit), you can find the (single) commit named by that name. To do so, use git rev-parse name^{commit}.

A commit hash uniquely identifies that one particular commit, and you can find all branch or tag names that allow you to reach that commit by walking the graph from the one commit that the given branch or tag identifies:

git branch --contains <hash>
git tag --contains <hash>

Context: I am creating some devops deployment change-logs so we can keep track of what we changed - I am wondering if it's worthwhile to name the git branches that were released, or just put the git commit hash in there, and we can reverse lookup the branch name if we really want to?

The usual approach here is to create a tag name to identify a particular commit that was handed to a particular user. Since a tag names one single object (usually a commit), and you have full control over the spelling of the tag, this tends to work particularly well.

torek
  • 330,127
  • 43
  • 437
  • 552
  • So create a tag, for releases, that makes sense since most branches either don't mean much or get deleted – Alexander Mills Jun 27 '19 at 21:57
  • Also: _Is there a reliable way to trace what the branch name was when the git commit was created?_ ah, no. Unless you include the branch name that was checked out as part of the comment for a revision (and that assumes you always work on a branch... which is not correct either). – eftshift0 Jun 28 '19 at 02:13