git describe Command
git describe The command is used to generate a readable string that describes the current commit based on Git's tag system.
git describe The command is commonly used to generate version numbers, help identify specific commits, and can be used when building, releasing, or tracking a specific version.
Basic Syntax
git describe [] []
: Specify the commit to describe. Defaults to the current commit.: Options for customizing the output format or behavior.
** Common options and usage:**
| Option | Description | Usage Examples |
|---|---|---|
--tags |
Use all tags (including lightweight tags) to generate the description. By default,git describe only annotated tags are considered. |
git describe --tags |
--abbrev= |
Set the minimum length of the commit hash to display to characters. |
git describe --abbrev=8 |
--long |
Force a long-format description, including the commit hash and the number of commits since the tag. | git describe --long |
--exact-match |
Only return the tag name when the current commit exactly matches the tag. | git describe --exact-match |
--dirty |
If the working directory has changes (uncommitted changes), add the -dirty suffix to the description. |
git describe --dirty |
--match= |
Only use tags matching the specified pattern for the description. | git describe --match "v1.*" |
--candidates= |
Limit the number of tags considered for the description; the default is 10. | git describe --candidates=5 |
--always |
Display the commit hash even if no tag is found. | git describe --always |
Common usage
** 1. Describe the current commit**
Generate a description string based on the nearest tag:
git describe
Example output:
v1.2-15-g2414721
Here the output indicates that the current commit is 15 commits away from the nearest tag v1.2, and the current commit's hash is 2414721.
** 2. Describe using all tags**
Include all tags (lightweight and annotated tags) to generate the description:
git describe --tags
3. Display a long-format description
Display a long-format description, including the hash and the number of commits since the tag:
git describe --long
4. Only return the tag name when the commit exactly matches the tag
If the current commit exactly matches a tag, only return the tag name:
git describe --exact-match
5. Add the -dirty suffix
If the working directory has uncommitted changes, add the -dirty suffix to the description:
git describe --dirty
6. Only use tags matching a pattern
Use tags matching the specified pattern for the description:
git describe --match "v1.*"
7. Display the commit hash even if no tag is found
Display the current commit's hash, even when there is no tag:
git describe --always
Git Basic Operations