Question

Is there any way to get a list of refs (including tags, branches, and remotes) that point to a particular commit in git?

Was it helpful?

Solution

git show-ref | grep $(git rev-parse HEAD) shows all refs that point to HEAD, the currently checked out commit.

git show-ref shows all refs in your git repo.

git show-ref | grep "SHA goes here" shows all refs that point to the SHA of a commit.

OTHER TIPS

Human-readable format

For the last commit (ie, HEAD):

git log -n1 --oneline --decorate

Or to specify a particular commit:

git log -n1 --oneline --decorate fd88

gives:

fd88175 (HEAD -> master, tag: head, origin/master) Add diff-highlight and icdiff

To only get the tags/refs/remotes, pass this through sed:

$ git log -n1 --oneline --decorate | sed 's/.*(\(.*\)).*/\1/'

HEAD -> master, tag: head, origin/master

For bonus points, add an alias for this:

decorations = "!git log -n1 --oneline --decorate $1 | sed 's/.*(\\(.*\\)).*/\\1/' #"

Using only pipes. Outputs the identical SHA of the two branches if they are the same, fails with no output and exit code 1 otherwise.

git rev-parse A B |\
 uniq |\
 sed -n -e '${1p}' -e '2q 1'
  • rev-parse the 2+ branches
  • uniq only unique lines
  • ${1p} if last line and first line, print
  • 2q 1 else quit with exit code 1
    may be GNU specific. Try q if it doesn't work.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top