How to get the latest tag name in current branch in Git?

前端 未结 24 2173
你的背包
你的背包 2020-11-22 17:04

What\'s the simplest way to get the most recent tag in Git?

git tag a HEAD
git tag b HEAD^^
git tag c HEAD^
git tag

output:



        
相关标签:
24条回答
  • 2020-11-22 17:25

    "Most recent" could have two meanings in terms of git.

    You could mean, "which tag has the creation date latest in time", and most of the answers here are for that question. In terms of your question, you would want to return tag c.

    Or you could mean "which tag is the closest in development history to some named branch", usually the branch you are on, HEAD. In your question, this would return tag a.

    These might be different of course:

    A->B->C->D->E->F (HEAD)
           \     \
            \     X->Y->Z (v0.2)
             P->Q (v0.1)
    

    Imagine the developer tag'ed Z as v0.2 on Monday, and then tag'ed Q as v0.1 on Tuesday. v0.1 is the more recent, but v0.2 is closer in development history to HEAD, in the sense that the path it is on starts at a point closer to HEAD.

    I think you usually want this second answer, closer in development history. You can find that out by using git log v0.2..HEAD etc for each tag. This gives you the number of commits on HEAD since the path ending at v0.2 diverged from the path followed by HEAD.

    Here's a Python script that does that by iterating through all the tags running this check, and then printing out the tag with fewest commits on HEAD since the tag path diverged:

    https://github.com/MacPython/terryfy/blob/master/git-closest-tag

    git describe does something slightly different, in that it tracks back from (e.g.) HEAD to find the first tag that is on a path back in the history from HEAD. In git terms, git describe looks for tags that are "reachable" from HEAD. It will therefore not find tags like v0.2 that are not on the path back from HEAD, but a path that diverged from there.

    0 讨论(0)
  • 2020-11-22 17:25
    git log --tags --no-walk --pretty="format:%d" | sed 2q | sed 's/[()]//g' | sed s/,[^,]*$// | sed  's ......  '
    

    IF YOU NEED MORE THAN ONE LAST TAG

    (git describe --tags sometimes gives wrong hashes, i dont know why, but for me --max-count 2 doesnt work)

    this is how you can get list with latest 2 tag names in reverse chronological order, works perfectly on git 1.8.4. For earlier versions of git(like 1.7.*), there is no "tag: " string in output - just delete last sed call

    If you want more than 2 latest tags - change this "sed 2q" to "sed 5q" or whatever you need

    Then you can easily parse every tag name to variable or so.

    0 讨论(0)
  • 2020-11-22 17:27

    You could take a look at git describe, which does something close to what you're asking.

    0 讨论(0)
  • 2020-11-22 17:27

    This is an old thread, but it seems a lot of people are missing the simplest, easiest, and most correct answer to OP's question: to get the latest tag for the current branch, you use git describe HEAD. Done.

    Edit: you can also supply any valid refname, even remotes; i.e., git describe origin/master will tell you the latest tag that can be reached from origin/master.

    0 讨论(0)
  • 2020-11-22 17:28

    If you want to find the last tag that was applied on a specific branch you can try the following:

    git describe --tag $(git rev-parse --verify refs/remotes/origin/"branch_name")
    
    0 讨论(0)
  • 2020-11-22 17:30

    Will output the tag of the latest tagged commit across all branches

    git describe --tags $(git rev-list --tags --max-count=1)
    
    0 讨论(0)
提交回复
热议问题