Reputation: 3648
Is there any way to get a list of refs (including tags, branches, and remotes) that point to a particular commit in git?
Upvotes: 16
Views: 4553
Reputation: 3871
git for-each-ref --format='%(refname)' \
--exclude='refs/remotes/*/HEAD' --points-at=<commit> \
'refs/heads/**' 'refs/remotes/**' 'refs/tags/**'
The --format
overrides the default three-column output so that you only get the refname for each line.
Upvotes: 2
Reputation: 4292
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+ branchesuniq
only unique lines${1p}
if last line and first line, print2q 1
else quit with exit code 1q
if it doesn't work.Upvotes: 0
Reputation: 47013
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/' #"
Upvotes: 5
Reputation: 3648
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.
Upvotes: 15