Search code examples
gitsedgrepcut

sed/grep tag of a commit in git


I need to grep/sed tags that are associated for a commit in a git repository.

I have tried with this command:

git ls-remote -t <remote-project> | grep -E '<commit-sha>' | sed -e 's,.* -e refs/tags/,,' -e 's/\^{}//'

But what i get here is :

<commit-sha1> refs/tags/<tag1>
<commit-sha1> refs/tags/<tag2>

I want to fetch just tag1 and tag2 so that I can then seperate them and store it in variables for further usage.

I have also tried:

git ls-remote -t <remote-project> | grep -E '<commit-id>' | sed -e 's,.* -e refs/tags/,,' -e 's/\^{}//' | cut -f 2

Output:

refs/tags/<tag1>
refs/tags/<tag2>

Still not what I was looking for and i dont think it is the compact way of doing it.

Expected output for one commit:

tag1
tag2

Edit:

git ls-remote -t ssh://somerepo.git 

This command gives me list of commits with associated tag information.

Output:

b63862c999a160add6b7617b3deb40a399aaa0e7    refs/tags/tag1
b63862c999a160add6b7617b3deb40a399aaa0e7    refs/tags/tag2^{}
1a761add5189d037e4b713a000aa650c740f624e    refs/tags/tag3
a8723b64a0d70f7d92ad65d3535ea13a9d6cfc2b    refs/tags/tag4^{}

Solution

  • Try this command

    git ls-remote -t ssh://somerepo.git | sed  -n '/b63862c999a160add6b7617b3deb40a399aaa0e7/{s/.*\///p}' 
    

    or

    git ls-remote -t ssh://somerepo.git | sed  -rn '/b63862c999a160add6b7617b3deb40a399aaa0e7/{s/.*\/([^^]+).*/\1/p}'
    

    First Command Output:

    tag1
    tag2^{}
    

    Second Command Output:

    tag1
    tag2