Skip to main content

git tags

Delete a Local and Remote Git Tag

Delete a Local Tag

To delete a tag locally, use:

git tag -d <tagname>

Delete a Remote Tag

After deleting the tag locally, you can also remove it from the remote repository:

git push origin --delete <tagname>

Example

Suppose you want to delete a tag named v1.0.0.

Delete locally:

git tag -d v1.0.0

Delete remotely:

git push origin --delete v1.0.0

Alternative: Delete Remote Tag Without Deleting Locally

To remove a tag only from the remote repository without affecting the local tag, use:

git push origin :refs/tags/<tagname>

This removes the tag from the remote repository without touching the local tag.


Create a Signed Git Tag

To create a signed tag, use the following command:

git tag -s <tagname> -m "<message>"

Steps:

  1. Create a signed tag: Replace <tagname> with the desired tag name (e.g., v1.0.0) and <message> with a description.

    git tag -s v1.0.0 -m "Initial release"
  2. Select a GPG key (if multiple keys exist): Specify the key explicitly if you have multiple GPG keys.

    GIT_COMMITTER_SIGNING_KEY=<key_id> git tag -s v1.0.0 -m "Initial release"
  3. Verify the signature:

    git tag -v <tagname>
  4. Push the tag (optional): Push the signed tag to the remote repository.

    git push origin <tagname>

Prerequisites:

  • Install and configure a GPG key:
    git config --global user.signingkey <key_id>
  • Ensure the GPG agent is running, and your key is correctly imported.

Synchronize Local and Remote Tags

To synchronize tags between your local and remote repositories and remove locally obsolete tags:

git fetch --prune origin "+refs/tags/*:refs/tags/*"

Explanation:

  • git fetch: Retrieves data from the remote repository.
  • --prune: Removes references (e.g., tags) that no longer exist on the remote.
  • "+refs/tags/*:refs/tags/*": Synchronizes all tags, including force updates.

This ensures your local tags match the remote repository exactly, including removing deleted tags.


Start Fresh with a Single Commit (v1.0.0)

To consolidate all previous commits into one and start clean with a v1.0.0, follow these steps:

  1. Ensure a clean working directory:

    git status

    Commit or stash any changes if needed.

  2. Start an interactive rebase:

    git rebase -i --root

    The --root flag allows you to modify the very first commit in the history.

  3. Combine commits: In the editor, you’ll see a list of all previous commits. Use squash (or s) for all commits after the first:

    pick abcdef First commit
    squash bcdefg Added feature A
    squash cdefgh Fixed bug B

    Save and close the editor.

  4. Set a new commit message: After combining commits, set a new message like:

    v1.0.0: Initial release

    Save and close the editor.

  5. Create a tag for v1.0.0:

    git tag v1.0.0
  6. Push changes (optional): If the repository is on a remote server, force-push the changes:

    git push --force
    git push --tags

Your repository now has a single commit with a clean history and a v1.0.0 tag.