如何将git分支上的标记移动到其他提交?

时间:2011-11-08 00:32:07

标签: git git-tag

我在主分支上创建了一个名为v0.1的标记,如下所示:

git tag -a v0.1

但后来我意识到我需要将一些更改合并到master for release 0.1中,所以我做到了。但是现在我的v0.1标签被卡住了(用于调用便利贴的类比)错误的提交。我希望它停留在master上的最新提交中,但它会停留在master上最近的第二次提交中。

如何将其移至master上的最新提交?

10 个答案:

答案 0 :(得分:998)

使用git tag-f选项:

-f
--force

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

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

实施例

  1. 在按

    之前删除任何遥控器上的标签
    git push origin :refs/tags/<tagname>
    
  2. 替换标记以引用最近的提交

    git tag -fa <tagname>
    
  3. 将标签推送到远程原点

    git push origin master --tags
    

答案 1 :(得分:236)

更确切地说,你必须强制添加标签,然后用选项--tags和-f推送:

git tag -f -a <tagname>
git push -f --tags

答案 2 :(得分:129)

总结一下您的遥控器是否被调用origin并且您正在使用master分支:

git tag -d <tagname>
git push origin :refs/tags/<tagname>
git tag <tagname> <commitId>
git push origin <tagname>
  • 第1行删除本地环境中的标记
  • 第2行删除远程环境中的标记。
  • 第3行将标记添加到不同的提交
  • 第4行将更改推送到远程

您还可以将第4行交换为git push origin --tags,以便使用本地更改中的代码推送所有更改。

基于@ stuart-golodetz,@ greg-hewgill,@ hedeep,@ ben-hocking回答,评论低于他们的答案,NateS评论低于我的答案。

答案 3 :(得分:84)

使用git tag -d <tagname>删除它,然后在正确的提交中重新创建它。

答案 4 :(得分:16)

在使用Git时,我尽量避免一些事情。

  1. 使用内部知识,例如参考/标签。我尝试仅使用记录在案的Git命令,并避免使用需要了解.git目录内部内容的内容。 (也就是说,我将Git视为Git用户,而不是Git开发人员。)

  2. 在不需要时避免使用武力。

因此,这是我在不了解Git内部知识的情况下在本地和远程更改标签的非暴力解决方案。

当软件修复最终有问题并且需要更新/重新发布时,我会使用它。

git tag -d fix123                      # delete the old local tag
git push github :fix123                # delete the old remote tag (use for each remote)
git tag fix123 790a621265              # create a new local tag
git push github fix123                 # push new tag to remote    (use for each remote)

github是一个示例远程名称,fix123是一个示例标记名称,790a621265是一个示例提交。

答案 5 :(得分:12)

我将离开这里,只是符合我需要的另一种形式的命令 我想移动标签v0.0.1.2

$ git tag -f v0.0.1.2 63eff6a

Updated tag 'v0.0.1.2' (was 8078562)

然后:

$ git push --tags --force

答案 6 :(得分:8)

将一个标记移动到另一个提交的别名。

在您的示例中,要使用散列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 #"

答案 7 :(得分:6)

另一种方式:

在远程仓库中移动标签。(如果需要,将HEAD替换为任何其他。)

$ git push --force origin HEAD:refs/tags/v0.0.1.2

取回更改。

$ git fetch --tags

答案 8 :(得分:1)

如果要移动带注释的标记,只更改目标提交但保留注释消息和其他元数据使用:

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&lt; tag-to-move&gt; &lt;目标&GT;

上述功能是通过引用teerapap/git-move-annotated-tag.sh开发的。

答案 9 :(得分:1)

如果你使用 github 并且想要更改 commit for release(例如你发现创建 release 后不要 commit smth)。你可以使用

git push origin :refs/tags/<tagname>

执行此命令后 github 删除您的标签,您的发布将成为草稿。这意味着您可以重新创建发布并选择提交。您的文件和消息将被保存。

相关问题