Skip to main content

createTag()

Creates a lightweight Git tag.

Syntax

function createTag(
  tagName: string,
  opts?: CreateTagOptions
): Promise<string>

Parameters

tagName
string
required
The name of the tag to create.Examples: "v1.0.0", "hotfix-1"
opts
CreateTagOptions
Configuration options.

Returns

result
Promise<string>
A promise that resolves with the command output.

Examples

Create a basic tag

await createTag("v1.0.0")
// Same as: git tag v1.0.0

Create with promise handling

createTag("hotfix-1")
  .then(output => console.log(output))
  .catch(error => console.error(error))

Force replace an existing tag

await createTag("v1.0.0", { force: true })
// Same as: git tag -f v1.0.0

createAnnotatedTag()

Creates an annotated Git tag with a message. Annotated tags are stored as full objects in Git and include the tagger name, email, date, and a message.

Syntax

function createAnnotatedTag(
  tagName: string,
  message: string,
  opts?: CreateTagOptions
): Promise<string>

Parameters

tagName
string
required
The name of the tag to create.Examples: "v1.0.0", "release-2024"
message
string
required
The annotation message for the tag.Examples: "Initial stable release", "Production release for 2024"
opts
CreateTagOptions
Configuration options.

Returns

result
Promise<string>
A promise that resolves with the command output.

Examples

Create an annotated tag

await createAnnotatedTag("v1.0.0", "Initial stable release")
// Same as: git tag -m "Initial stable release" v1.0.0

Create with promise handling

createAnnotatedTag("release-2024", "Production release for 2024")
  .then(output => console.log(output))
  .catch(error => console.error(error))

Force replace with new message

await createAnnotatedTag(
  "v1.0.0",
  "Re-tagged after hotfix",
  { force: true }
)
// Same as: git tag -f -m "Re-tagged after hotfix" v1.0.0

deleteTag()

Deletes a local Git tag.

Syntax

function deleteTag(tagName: string): Promise<string>

Parameters

tagName
string
required
The name of the tag to delete.Examples: "v1.0.0", "release-2024"

Returns

result
Promise<string>
A promise that resolves with the command output.

Examples

Delete a tag

await deleteTag("v1.0.0")
// Deletes the local tag `v1.0.0`
// Same as: git tag --delete v1.0.0

Delete with promise handling

deleteTag("release-2024")
  .then(output => console.log(output))
  .catch(error => console.error(error))

getTags()

Retrieves all Git tags from the current repository.

Syntax

function getTags(
  prefixes?: string[],
  to?: string
): Promise<string[]>

Parameters

prefixes
string[]
default:"[]"
Optional list of prefixes to remove or replace from the beginning of each tag.
to
string
default:"''"
Replacement value for matched prefixes.

Returns

result
Promise<string[]>
A promise that resolves to an array of tag names.

Examples

Get all tags

const tags = await getTags()
console.log(tags)
// ['v0.1.0', 'v0.2.0', 'v0.3.0']

Strip and replace prefixes

const tags = await getTags(['v'], 'version-')
console.log(tags)
// ['version-0.1.0', 'version-0.2.0']

hasTag()

Checks whether a given Git tag exists.

Syntax

function hasTag(tagName: string): Promise<boolean>

Parameters

tagName
string
required
The tag name to check.

Returns

result
Promise<boolean>
A promise that resolves to true if the tag exists, otherwise false.

Examples

Check if a tag exists

const exists = await hasTag("v0.3.0")
console.log(exists) // true

const missing = await hasTag("v9.9.9")
console.log(missing) // false

Conditional tag creation

if (!await hasTag("v1.0.0")) {
  await createTag("v1.0.0")
  console.log("Tag created")
} else {
  console.log("Tag already exists")
}

Notes

  • Works for lightweight tags
  • Works for annotated tags
  • Works for signed and unsigned tags

isValidTagName()

Determines whether a string is a valid and safe Git tag name.

Syntax

function isValidTagName(name: string): boolean

Parameters

name
string
required
The tag name to validate.

Returns

result
boolean
Returns true if the name is a valid Git tag name, otherwise false.

Examples

Valid tag names

isValidTagName("v1.0.0")     // true
isValidTagName("release-1")  // true
isValidTagName("feature/v2") // true

Invalid tag names

isValidTagName("v 1.0.0")    // false (contains space)
isValidTagName("../v1")      // false (parent directory reference)
isValidTagName("@")          // false (reserved name)
isValidTagName("tag.lock")   // false (ends with .lock)
isValidTagName("feature//x") // false (consecutive slashes)

Validate before creating

const tags = [
  "v1.0.0",
  "release-2025",
  "feature/v2",
  "@",
  "../v1",
  "tag.lock",
  "v 1.0.0",
  "/start",
  "end/",
  "feature//x",
]

for (const tag of tags) {
  console.log(`${tag.padEnd(15)} →`, isValidTagName(tag))
}

// Output:
// v1.0.0         → true
// release-2025   → true
// feature/v2     → true
// @              → false
// ../v1          → false
// tag.lock       → false
// v 1.0.0        → false
// /start         → false
// end/           → false
// feature//x     → false

Invalid Cases

This function follows Git reference naming rules and prevents tag names that would be rejected by Git or cause ambiguous behavior:
  • Empty strings
  • Reserved name @
  • Names containing the sequence @{
  • Names ending with .lock or .
  • Names starting or ending with /
  • Consecutive slashes (//)
  • Parent or current directory references (.., /., or segments starting with .)
  • Whitespace or forbidden characters (~ ^ : ? * [ ] \)

Build docs developers (and LLMs) love