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

前端 未结 24 2176
你的背包
你的背包 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:30

    What is wrong with all suggestions (except Matthew Brett explanation, up to date of this answer post)?

    Just run any command supplied by other on jQuery Git history when you at different point of history and check result with visual tagging history representation (I did that is why you see this post):

    $ git log --graph --all --decorate --oneline --simplify-by-decoration
    

    Todays many project perform releases (and so tagging) in separate branch from mainline.

    There are strong reason for this. Just look to any well established JS/CSS projects. For user conventions they carry binary/minified release files in DVCS. Naturally as project maintainer you don't want to garbage your mainline diff history with useless binary blobs and perform commit of build artifacts out of mainline.

    Because Git uses DAG and not linear history - it is hard to define distance metric so we can say - oh that rev is most nearest to my HEAD!

    I start my own journey in (look inside, I didn't copy fancy proof images to this long post):

    What is nearest tag in the past with respect to branching in Git?

    Currently I have 4 reasonable definition of distance between tag and revision with decreasing of usefulness:

    • length of shortest path from HEAD to merge base with tag
    • date of merge base between HEAD and tag
    • number of revs that reachable from HEAD but not reachable from tag
    • date of tag regardless merge base

    I don't know how to calculate length of shortest path.

    Script that sort tags according to date of merge base between HEAD and tag:

    $ git tag \
         | while read t; do \
             b=`git merge-base HEAD $t`; \
             echo `git log -n 1 $b --format=%ai` $t; \
           done | sort
    

    It usable on most of projects.

    Script that sort tags according to number of revs that reachable from HEAD but not reachable from tag:

    $ git tag \
        | while read t; do echo `git rev-list --count $t..HEAD` $t; done \
        | sort -n
    

    If your project history have strange dates on commits (because of rebases or another history rewriting or some moron forget to replace BIOS battery or other magics that you do on history) use above script.

    For last option (date of tag regardless merge base) to get list of tags sorted by date use:

    $ git log --tags --simplify-by-decoration --pretty="format:%ci %d" | sort -r
    

    To get known current revision date use:

    $ git log --max-count=1
    

    Note that git describe --tags have usage on its own cases but not for finding human expected nearest tag in project history.

    NOTE You can use above recipes on any revision, just replace HEAD with what you want!

提交回复
热议问题