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:
-
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" -
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" -
Verify the signature:
git tag -v <tagname> -
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:
-
Ensure a clean working directory:
git statusCommit or stash any changes if needed.
-
Start an interactive rebase:
git rebase -i --rootThe
--rootflag allows you to modify the very first commit in the history. -
Combine commits: In the editor, you’ll see a list of all previous commits. Use
squash(ors) for all commits after the first:pick abcdef First commit
squash bcdefg Added feature A
squash cdefgh Fixed bug BSave and close the editor.
-
Set a new commit message: After combining commits, set a new message like:
v1.0.0: Initial releaseSave and close the editor.
-
Create a tag for v1.0.0:
git tag v1.0.0 -
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.