Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find if a git SHA points to a branch HEAD?

Tags:

git

To avoid unnecessarily detached HEADs when checking out a certain git SHA with a (python) script, I'd like to check out a branch instead, if that SHA happens to be the current HEAD of a branch.

Ideally, I want to feed git an SHA, and it returns a branch name if the SHA is on a branch's current tip, or error out if not.

git describe --all --exact-match <SHA> very nearly is what I need, only that it is primarily aimed at tags, and so if a branch and a tag point at my SHA (which often happens in our release branches, for example), only the tag is given. This is not useful, because checking out the tag leads to a detached HEAD (even if a branch points at the same SHA).

Note, I don't want to do git branch --contains - I don't need to know which branches contain my commit.

If there's no command like git describe, just for branches, I know I can cross-check my SHA against branch SHAs via git show-ref. That's not the most elegant solution, though.

I could also do git name-rev --name-only <hash>, but I'd have to manually check the output for ~ characters, which feels unelegant if there's a git command somewhere to do the same thing.

like image 207
Christoph Avatar asked Sep 30 '13 17:09

Christoph


People also ask

How do I find my branch hash?

Retrieving the hash If you want to turn references (branches and tags) into hash, you can use git show-ref and git for-each-ref commands. Using the git reflog command is also used if you want to have the history on the head of your branches.

Where is my head pointing git?

In Git, a head is a ref that points to the tip (latest commit) of a branch. You can view your repository's heads in the path . git/refs/heads/ . In this path you will find one file for each branch, and the content in each file will be the commit ID of the tip (most recent commit) of that branch.

Are commits tied to a branch?

As a result, commits don't 'belong to' or 'come from' a branch, they are just nodes in your graph.


1 Answers

With the upcoming git 2.7 (Q4 2015) you will get a more complete version of git for-each-ref, which now support the --points-at

git for-each-ref --points-at <branch_name>

With the doc:

--points-at <object>:

Only list refs which points at the given object.

That will allow to check if a commit is part of that list or not.


See commit 4a71109, commit ee2bd06, commit f266c91, commit 9d306b5, commit 7c32834, commit 35257aa, commit 5afcb90, ..., commit b2172fd (07 Jul 2015), and commit af83baf (09 Jul 2015) by Karthik Nayak (KarthikNayak).
(Merged by Junio C Hamano -- gitster -- in commit 9958dd8, 05 Oct 2015)

Some features from "git tag -l" and "git branch -l" have been made available to "git for-each-ref" so that eventually the unified implementation can be shared across all three, in a follow-up series or two.

* kn/for-each-tag-branch:
  for-each-ref: add '--contains' option
  ref-filter: implement '--contains' option
  parse-options.h: add macros for '--contains' option
  parse-option: rename parse_opt_with_commit()
  for-each-ref: add '--merged' and '--no-merged' options
  ref-filter: implement '--merged' and '--no-merged' options
  ref-filter: add parse_opt_merge_filter()
  for-each-ref: add '--points-at' option
  ref-filter: implement '--points-at' option  
like image 194
VonC Avatar answered Oct 19 '22 22:10

VonC