Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git describe show only latest tag and additional commits

Is there any syntax for git describe to display only the latest tag and additional commits?

So that you get

4.0.7 for being at the commit tagged 4.0.7
4.0.7-12 for having 12 commits since tag 4.0.7

git describe --tags is pretty close with 4.0.7-12-g09181 but i haven't found a way to get rid of the hash being appended.

git describe --tags --abbrev=2

still displays 4.0.7-12-g0918

git describe --tags --abbrev=0

displays 4.0.7 only.

like image 210
mgherkins Avatar asked May 19 '16 07:05

mgherkins


People also ask

How do I get most recent tags?

In order to find the latest Git tag available on your repository, you have to use the “git describe” command with the “–tags” option. This way, you will be presented with the tag that is associated with the latest commit of your current checked out branch.

How do I get the latest tag from github?

get-latest-tag-on-git.sh # The command finds the most recent tag that is reachable from a commit. # If the tag points to the commit, then only the tag is shown. # and the abbreviated object name of the most recent commit.

What does git describe -- tags do?

The `git describe` command finds the most recent tag that is reachable from a commit. If the tag is on the current commit, then only the tag is shown. Otherwise, it shows the tag name appended with the number of additional commits and the abbreviated SHA of the current commit.

How do I see git tags?

Listing the available tags in Git is straightforward. Just type git tag (with optional -l or --list ). You can also search for tags that match a particular pattern. The command finds the most recent tag that is reachable from a commit.


2 Answers

There is no option in the describe command to do what you want. You could pipe the output to a shell script that removes the hash.

git describe --tags | sed 's/\(.*\)-.*/\1/'

see https://stackoverflow.com/a/32084572/1468708

thx !

like image 87
mgherkins Avatar answered Oct 05 '22 08:10

mgherkins


I ran into a similar problem where I wanted to generate a string like: "tag-commits" but optionally followed with the -dirty and/or -broken suffix.

1.0-3    
1.0-3-dirty
1.0-3-dirty-broken

(Dirty simply indicates that you have uncommitted changes).

The accepted answer would however remove the -dirty (or when used -broken) tag at the end and leave the hash in the output.

To fix this I wrote the following command:

git describe --tags --dirty | sed 's/-g[a-z0-9]\{7\}//'

This works because the hash always starts with a "g" followed by 7 characters.

like image 30
Wouter Vandevelde Avatar answered Oct 05 '22 08:10

Wouter Vandevelde