How to get the latest tag name in current branch i

2019-01-01 14:20发布

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:

a
b
c

Should I write a script to get each tag's datetime and compare them?

19条回答
高级女魔头
2楼-- · 2019-01-01 14:31

To get the most recent tag:

git describe --tags

To get the most recent annotated tag:

git describe --abbrev=0 --tags
查看更多
人间绝色
3楼-- · 2019-01-01 14:31

How about this?

TAG=$(git describe $(git rev-list --tags --max-count=1))

Technically, won't necessarily get you the latest tag, but the latest commit which is tagged, which may or may not be the thing you're looking for.

查看更多
何处买醉
4楼-- · 2019-01-01 14:31

The following works for me in case you need last two tags (for example, in order to generate change log between current tag and the previous tag). I've tested it only in situation where the latest tag was the HEAD.

PreviousAndCurrentGitTag=`git describe --tags \`git rev-list --tags --abbrev=0 --max-count=2\` --abbrev=0`
PreviousGitTag=`echo $PreviousAndCurrentGitTag | cut -f 2 -d ' '`
CurrentGitTag=`echo $PreviousAndCurrentGitTag | cut -f 1 -d ' '`

GitLog=`git log ${PreviousGitTag}..${CurrentGitTag} --pretty=oneline | sed "s_.\{41\}\(.*\)_; \1_"`

It suits my needs, but as I'm no git wizard, I'm sure it could be further improved. I also suspect it will break in case the commit history moves forward. I'm just sharing in case it helps someone.

查看更多
浮光初槿花落
5楼-- · 2019-01-01 14:33
git tag -l ac* | tail -n1

Get the last tag with prefix "ac". For example, tag named with ac1.0.0, or ac1.0.5. Other tags named 1.0.0, 1.1.0 will be ignored.

git tag -l [0-9].* | tail -n1

Get the last tag, whose first char is 0-9. So, those tags with first char a-z will be ignored.

More info

git tag --help # Help for `git tag`

git tag -l <pattern>

List tags with names that match the given pattern (or all if no pattern is given). Running "git tag" without arguments also lists all tags. The pattern is a shell wildcard (i.e., matched using fnmatch(3)). Multiple patterns may be given; if any of them matches, the tag is shown.


tail -n <number> # display the last part of a file
tail -n1 # Display the last item 

Update

With git tag --help, about the sort argument. It will use lexicorgraphic order by default, if tag.sort property doesn't exist.

Sort order defaults to the value configured for the tag.sort variable if it exists, or lexicographic order otherwise. See git-config(1).

After google, someone said git 2.8.0 support following syntax.

git tag --sort=committerdate
查看更多
初与友歌
6楼-- · 2019-01-01 14:36

My first thought is you could use git rev-list HEAD, which lists all the revs in reverse chronological order, in combination with git tag --contains. When you find a ref where git tag --contains produces a nonempty list, you have found the most recent tag(s).

查看更多
人气声优
7楼-- · 2019-01-01 14:40

To get the most recent tag, you can do:

$ git for-each-ref refs/tags --sort=-taggerdate --format='%(refname)' --count=1

Of course, you can change the count argument or the sort field as desired. It appears that you may have meant to ask a slightly different question, but this does answer the question as I interpret it.

查看更多
登录 后发表回答