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?

1
  • 1
    A commit can be part of many branches. Commented Jun 27, 2019 at 21:50

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

So create a tag, for releases, that makes sense since most branches either don't mean much or get deleted
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).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.