用 Git 替换远程标记

我在我的“起源”存储库上有一些标签。然后我意识到我需要在其中一个标记上添加一些更改,并将它们推回到我的存储库中。 是否有一种方法可以将现有标记一次性推送到存储库,或者应该在此之前删除该标记?

54484 次浏览

我不确定我是否理解你的问题,但听起来似乎删除标签,推动您的变化,然后重新添加标签是最简单的..。

虽然您可以删除标记并将更改推送到远程回购,但这不应该是实践。

git tag -d tag1
git push origin :refs/tags/tag1

因此,如果您需要将 git 分支上的标记(例如: “ V0.5”)移动到另一个提交(例如: 师父) ,可能是一个较新的提交,那么您可以使用 -f选项到 git tag:

-f
--force


Replace an existing tag with the given name (instead of failing)

您可能希望结合使用 -f-a来强制创建带注释的标记,而不是非带注释的标记。

例子

  1. 在你按之前删除任何遥控器上的标签

    git push origin :refs/tags/<tagname>
    

    或者举个例子:

    $ git push origin master :refs/tags/v0.5
    To git@github.com:org_name/repo_name.git
    - [deleted]         v0.5
    
  2. Replace the tag to reference the most recent commit (using -f will save as the git tag -d <tagname> local tag deletion step).

    git tag -fa <tagname>
    

    或者举个例子:

    $ git tag -fa "v0.5" -m "version 0.5"
    Updated tag 'v0.5' (was f55c93f)
    
  3. Push the tag to the remote origin

    git push origin --tags
    

    或者举个例子:

    $ git push origin master --tags
    Counting objects: 1, done.
    Writing objects: 100% (1/1), 196 bytes | 0 bytes/s, done.
    Total 1 (delta 0), reused 0 (delta 0)
    To git@github.com:org_name/repo_name.git
    * [new tag]         v0.5 -> v0.5
    

假设 newtag是新的标记,而 oldtag是旧的标记:

# Create new tag that points to the same of old tag
git tag newtag oldtag


# Remove oldtag
git tag -d oldtag


# Remove oldtag in remote machine
git push --delete origin oldtag


# Propapate newtag to remote machine
git push --tags

更简单的方法来替换标签,也是在远程:

git tag -f mytagname
git push -f --tags