Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display latest tag in a commit with multiple tags

Tags:

git

git-tag

I have a script for a large project, ten repositories, to checkout branches(local, remote, or tags) and display eventually where each is the current branch in each repository.

I use git branch to display which branch or tag is checked out in the current repository. This works fine in most cases except where in the same commit there are multiple tags, resulting in printing a random tag of the ones in that commit.

How can I fix this? Should I implement it in a different way?

Example

for repo in ...
do
  cd repo
  git checkout $1 || git checkout $2 || git checkout $3 ....
  git branch ##(to verify what happened)
done

So lets say I run ./checkoutAll feat1 origin/feat1 tags/ALL7 but ALL7 tag is in the same commit with ALL6, git branch displays the ALL6 instead of ALL7 that I specifically asked to checkout.

EDIT: The image below is to assist the people trying to help me,

enter image description here

So, let's say I run git checkout tags/V9.00.00.ALL.04 then git branch displays a random tag (* detached from ..) of the ones that exist, 7 in this example. Is there a way to display the latest? or at least display the one I asked it to checkout (even if the ALL7 is the same with ALL9)?

like image 255
thahgr Avatar asked Sep 23 '15 16:09

thahgr


People also ask

Can a commit have multiple tags?

We occasionally have two tags on the same commit. When we use git describe for that commit, git describe always returns the first tag. My reading of the git-describe man page seems to indicate that the second tag should be returned (which makes more sense).

How do I get most recent tags?

In order to find the latest Git tag available on your repository, you have to use the “git describe” command with the “–tags” option. This way, you will be presented with the tag that is associated with the latest commit of your current checked out branch.

Can you have multiple tags in git?

To push multiple tags simultaneously pass the --tags option to git push command. When another user clones or pulls a repo they will receive the new tags.

How do I get latest Github tags?

Returns the latest tag in the current branch. To get the latest annotated tag which targets only the current commit in the current branch, use git describe --exact-match --abbrev=0 .


1 Answers

It's not clear where you want to show these tags, but let me try to help you.

First, let's find the latest commit hash of the current branch:

git rev-parse HEAD

Then, let's find all tags:

git show-ref --tags --dereference

Now that we have both, we can match them to print/show what you're asking. For example:

SHA_HEAD=$(git rev-parse HEAD)
ALL_TAGS=$(git show-ref --tags --dereference)
echo "$ALL_TAGS" | awk '/'$SHA_HEAD' refs\/tags\// { print $2 }' | tr '\n' ' '

Or in a one-liner:

git show-ref --tags --dereference | awk '/'$(git rev-parse HEAD)' refs\/tags\// { print $2 }' | tr '\n' ' '
like image 105
jweyrich Avatar answered Sep 30 '22 16:09

jweyrich