使用 -f
选项来 git tag
:
-f
--force
Replace an existing tag with the given name (instead of failing)
您可能希望将 -f
与 -a
结合使用来强制创建带注释的标记,而不是未注释的标记。
例子
在推送之前删除任何远程上的标签 git push origin :refs/tags/
更准确地说,您必须强制添加标签,然后使用选项 --tags 和 -f 推送:
git tag -f -a <tagname>
git push -f --tags
-f
来推送标签来完成接受的答案。
总结一下,如果您的遥控器被称为 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
描述:
第 1 行删除本地环境中的标记。
第 2 行删除远程环境中的标签。
第 3 行将标签添加到不同的提交
第 4 行将更改推送到远程
您还可以将第 4 行更改为 git push origin --tags
,以将所有本地标签更改/更新推送到远程存储库。
以上答案基于@eedeep 的问题内容,以及Stuart Golodetz、Greg Hewgill 和@ben-hocking 的答案,以及他们答案下方的评论,以及我的答案下方@NateS 的原始评论。
使用 git tag -d <tagname>
删除它,然后在正确的提交上重新创建它。
在使用 Git 时,我会尽量避免一些事情。
使用内部知识,例如参考/标签。我尝试仅使用记录在案的 Git 命令,并避免使用需要了解 .git 目录内部内容的东西。 (也就是说,我将 Git 视为 Git 用户,而不是 Git 开发人员。)不需要时使用武力。过分的事情。 (推动一个分支和/或许多标签,以获得我想要的一个标签。)
因此,这是我在本地和远程更改标签的非暴力解决方案,无需了解 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 # create a new local, annotaged tag
我将在此留下适合我需要的另一种命令形式。
我想移动一个标签 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 #"
另一种方式:
在远程仓库中移动标签。(如果需要,将 HEAD 替换为任何其他标签。)
$ git push --force origin HEAD:refs/tags/v0.0.1.2
取回更改。
$ git fetch --tags
如果您使用 github 并希望更改提交以进行发布(例如,您发现在创建发布后不提交)。您可以使用
git push origin :refs/tags/<tagname>
在此命令 github 删除您的标签后,您的发布将成为草稿。这意味着您可以重新创建发布并选择提交。您的文件和消息将被保存。
如果要移动带注释的标记,仅更改目标提交但保留注释消息和其他元数据,请使用:
moveTag() {
local tagName=$1
# Support passing branch/tag names (not just full commit hashes)
local newTarget=$(git rev-parse $2^{commit})
git cat-file -p refs/tags/$tagName |
sed "1 s/^object .*$/object $newTarget/g" |
git hash-object -w --stdin -t tag |
xargs -I {} git update-ref refs/tags/$tagName {}
}
用法:moveTag
上述函数是通过引用 teerapap/git-move-annotated-tag.sh 开发的。
git tag -f -a my_tag
已经保留了先前消息的消息(使用 git 版本 2.11.0)。
不定期副业成功案例分享
git push -f origin <tagname>
git push origin master --tags
将推送标签,和master
分支(如果您已在本地提交任何更改)。如果您只想推送标签,只需使用git push origin --tags
。