Git tags are commonly used to mark important points in a repository history, such as releases or version milestones. Being able to retrieve the most recent tag can be helpful when generating version numbers, preparing releases, or automating build pipelines. This tutorial explains how to get latest tag name in Git.
1. Current branch
To get the most recent tag reachable from the current branch, execute the following command:
git describe --tags --abbrev=0
This command finds the closest tag in the commit history of the current branch and prints only the tag name.
2. Across all branches
To retrieve the latest tag in the entire repository, regardless of which branch contains it, run:
git describe --tags $(git rev-list --tags --max-count=1)
This command first finds the commit that has the most recent tag and then outputs the tag name associated with that commit. Unlike the previous command, this approach searches through all tagged commits in the repository, making it suitable when the goal is to identify the newest tag globally rather than within the current branch.
Leave a Comment
Cancel reply