Display last tag in commit with multiple tags

I have a script for a large project, ten repositories, for checking branches (local, remote or tags) and displaying ultimately where each of them is the current branch in each repository.

I use git branch to display which branch or tag is unloaded in the current repository. This works fine in most cases, unless there are several tags in the same commit, which results in printing a random tag from the ones in this commit.

How can i fix this? Should I implement it differently?

Example

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

So let's say I ran ./checkoutAll feat1 origin/feat1 tags/ALL7 but the ALL7 tag is in the same commit with ALL6 , git branch displays ALL6 instead of ALL7 , which I specifically asked to check.

EDIT: the image below will help people who are trying to help me,

enter image description here

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

+5
source share
3 answers

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

First, find the last commit hash of the current branch:

 git rev-parse HEAD 

Then find all the tags:

 git show-ref --tags --dereference 

Now that we have both, we can match them to print / show what you ask. For instance:

 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 one layer:

 git show-ref --tags --dereference | awk '/'$(git rev-parse HEAD)' refs\/tags\// { print $2 }' | tr '\n' ' ' 
+3
source

When you check the tag, you will usually get a “headless” statement, and git branch will say:

 $ git branch * (detached from TAG) 

If you want to know in which branch this tag is “on”, you can check if your tagged ref is the ancestor of any branches known to your repo. Use

 $ git branch --contains TAG # (or HEAD) 

Usually you get several candidates, since any number of branches could be created after the tag has been placed. You decide which one is a "branch".

If you want to see all tags indicating your current ref number, use:

 $ git tag --points-at TAG # (or HEAD) 

To see all symbolic links, use

 $ git log --oneline --decorate -1 
+2
source

No, there is no way: git does not always have the idea that you are on a branch; if you check a single commit, for example. by checking the tag, you are not on a branch, and therefore git cannot tell you which branch you are in.

As far as I know, the git branch should always print your current branch name if you are on one.

+1
source

Source: https://habr.com/ru/post/1232117/


All Articles