我在主分支上创建了一个叫做v0.1
的标签,就像这样:
git tag -a v0.1
但后来我发现仍然有一些更改需要合并到主分支以发布0.1版本,因此我做了这个。但现在我的v0.1
标签(用便利贴笔记的类比来说)被固定在了错误的提交上。我想让它固定在主分支上最新的提交上,但它却被固定在次新的提交上。
我该如何将它移动到主分支上最新的提交?
-f
选项来git tag
:-f
--force
Replace an existing tag with the given name (instead of failing)
你可能想要将-f
与-a
一起使用,以强制创建一个带注释的标签,而不是非注释的标签。
在推送之前,请删除任何远程标签
git push origin :refs/tags/<tagname>
将标签替换为引用最新的提交
git tag -fa <tagname>
将标签推送到远程仓库
git push origin --tags
总之,如果你的远程仓库叫做origin
,并且你正在master
分支上工作:
git tag -d <tagname> # delete the old tag locally
git push origin :refs/tags/<tagname> # delete the old tag remotely
git tag <tagname> <commitId> # make a new tag locally
git push origin <tagname> # push the new local tag to the remote
描述:
您还可以将第4行更改为git push origin --tags
,以将所有本地标签更改/更新推送到远程仓库。
以上答案基于@eedeep的问题内容,以及Stuart Golodetz、Greg Hewgill和@ben-hocking的答案,以及他们的回答下面的评论和@NateS在我的答案下原始的评论。
1.0.0
),它将会报错,显示 error: src refspec 1.0.0 matches more than one
。您可以通过使用 git push origin tags/1.0.0
来明确指定要推送一个标签。 - Joseph Marikle1.0.0
),它会报错,显示 error: src refspec 1.0.0 matches more than one
。你可以通过使用 git push origin tags/1.0.0
来明确指定推送标签。 - undefined更准确地说,您需要强制添加标签,然后使用选项--tags和-f进行推送:
git tag -f -a <tagname>
git push -f --tags
-f
来推送标签,这个回答完善了被接受的回答。 - Muhammad Yasirroni使用 git tag -d <tagname>
命令删除该标签,然后在正确的提交上重新创建。
当使用Git时,我会避免几件事情:
不使用内部知识,例如refs/tags。我尽可能只使用Git文档中记录的命令,并避免使用需要了解.git目录内部内容的东西。(也就是说,我将Git视为Git用户而不是Git开发人员。)
在没有必要的情况下使用force。
做过头。(推送分支和/或大量标签,以便在我想要的位置获取一个标签。)
因此,这是我的非暴力解决方案,用于在不了解Git内部工作原理的情况下更改标记,包括本地和远程标记。
在软件修复最终存在问题并需要更新/重新发布时,我使用它。
git tag -d fix123 # delete the old local tag
git push github :fix123 # delete the old remote tag (use for each affected remote)
git tag fix123 790a621265 # create a new local tag
git push github fix123 # push new tag to remote (use for each affected remote)
github
是示例的远程名称,fix123
是示例的标签名称,790a621265
是示例的提交。
`git tag -a fix123 790a621265 # 创建一个新的本地注释标签`
- Johan Bergens我会在这里提供符合我的需求的另一种形式的该命令。
有一个标签v0.0.1.2
,我想要移动它。
$ git tag -f v0.0.1.2 63eff6a
Updated tag 'v0.0.1.2' (was 8078562)
然后:
$ git push --tags --force
将一个标签移动到不同提交的别名。
在您的示例中,要移动哈希为 e2ea1639 的提交,请执行以下操作:git tagm v0.1 e2ea1639
。
对于已推送的标签,请使用 git tagmp v0.1 e2ea1639
。
这两个别名都会保留原始日期和消息。如果您使用 git tag -d
,则会丢失原始消息。
请将它们保存在您的.gitconfig
文件中。
# Return date of tag. (To use in another alias)
tag-date = "!git show $1 | awk '{ if ($1 == \"Date:\") { print substr($0, index($0,$3)) }}' | tail -2 | head -1 #"
# Show tag message
tag-message = "!git show $1 | awk -v capture=0 '{ if(capture) message=message\"\\n\"$0}; BEGIN {message=\"\"}; { if ($1 == \"Date:\" && length(message)==0 ) {capture=1}; if ($1 == \"commit\" ) {capture=0} }; END { print message }' | sed '$ d' | cat -s #"
### Move tag. Use: git tagm <tagname> <newcommit>
tagm = "!GIT_TAG_MESSAGE=$(git tag-message $1) && GIT_COMMITTER_DATE=$(git tag-date $1) && git tag-message $1 && git tag -d $1 && git tag -a $1 $2 -m \"$GIT_TAG_MESSAGE\" #"
### Move pushed tag. Use: git tagmp <tagname> <newcommit>
tagmp = "!git tagm $1 $2 && git push --delete origin $1 && git push origin $1 #"
本地删除:
git tag -d v0.1
在远程上移除:
git push origin --delete v0.1
然后在本地重新添加并将 v0.1 推送到最近的提交:
git tag -a v0.1
git push origin --tags
另一种方式:
将标签移动到远程仓库。(如果需要,用其他标签替换 HEAD。)
$ git push --force origin HEAD:refs/tags/v0.0.1.2
获取更改并将其取回。
$ git fetch --tags
git push origin :refs/tags/<tagname>
执行此命令后,GitHub将删除您的标签,使您的发布变为草稿状态。这意味着您可以重新创建发布并选择提交。您的文件和消息将被保存。
git push -f origin <标签名>
。 - rc_lukegit push origin master --tags
将推送标记和“master”分支。如果您只想推送标记,请使用git push origin --tags
。 - c32hedge