如何将 git 分支上的标签移动到不同的提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8044583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:15:33  来源:igfitidea点击:

How can I move a tag on a git branch to a different commit?

gitgit-tag

提问by eedeep

I created a tag on the master branch called v0.1like this:

我在 master 分支上创建了一个标签,v0.1如下所示:

git tag -a v0.1

But then I realized there were still some changes I needed to merge into master for release 0.1, so I did that. But now my v0.1tag is stuck on (to invoke the post-it note analogy) the wrong commit. I want it to be stuck on the most recent commit on master, but instead it is stuck on the second most recent commit on master.

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

How can I move it to the most recent commit on master?

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

回答by Greg Hewgill

Use the -foption to git tag:

使用该-f选项git tag

-f
--force

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

You probably want to use -fin conjunction with -ato force-create an annotated tag instead of a non-annotated one.

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

Example

例子

  1. Delete the tag on any remote before you push

    git push origin :refs/tags/<tagname>
    
  2. Replace the tag to reference the most recent commit

    git tag -fa <tagname>
    
  3. Push the tag to the remote origin

    git push origin master --tags
    
  1. 在推送之前删除任何遥控器上的标签

    git push origin :refs/tags/<tagname>
    
  2. 替换标签以引用最近的提交

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

    git push origin master --tags
    

回答by Daniel

More precisely, you have to force the addition of the tag, then push with option --tags and -f:

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

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

回答by Vive

To sum up if your remote is called originand you're working on masterbranch:

总结一下,如果您的遥控器被调用origin并且您正在master分支上工作:

git tag -d <tagname>
git push origin :refs/tags/<tagname>
git tag <tagname> <commitId>
git push origin <tagname>
  • Line 1 removes the tag in local env.
  • Line 2 removes the tag in remote env.
  • Line 3 adds the tag to different commit
  • Line 4 pushes the change to the remote
  • 第 1 行删除本地环境中的标签。
  • 第 2 行删除远程环境中的标签。
  • 第 3 行将标签添加到不同的提交
  • 第 4 行将更改推送到远程

You can also exchange line 4 to git push origin --tagsto push all the changes with tags from your local changes.

您还可以将第 4 行git push origin --tags更改为使用本地更改中的标签推送所有更改。

Basing on @stuart-golodetz, @greg-hewgill, @eedeep, @ben-hocking answers, comments below their answers and NateS comments below my answer.

基于@stuart-golodetz、@greg-hewgill、@eedeep、@ben-hocking 的答案,他们的答案下方的评论和我的答案下方的 NateS 评论。

回答by Stuart Golodetz

Delete it with git tag -d <tagname>and then recreate it on the correct commit.

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

回答by Ivan

I try to avoid a few things when using Git.

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

  1. Using knowledge of the internals, e.g. refs/tags. I try to use solely the documented Git commands and avoid using things which require knowledge of the internal contents of the .git directory. (That is to say, I treat Git as a Git user and not a Git developer.)

  2. The use of force when not required.

  3. Overdoing things. (Pushing a branch and/or lots of tags, to get one tag where I want it.)

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

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

  3. 做事过分。(推动一个分支和/或许多标签,以在我想要的地方获得一个标签。)

So here is my non-violent solution for changing a tag, both locally and remotely, without knowledge of the Git internals.

所以这是我在不了解 Git 内部原理的情况下在本地和远程更改标签的非暴力解决方案。

I use it when a software fix ultimately has a problem and needs to be updated/re-released.

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

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)

githubis a sample remote name, fix123is a sample tag name, and 790a621265a sample commit.

github是示例远程名称、fix123示例标记名称和790a621265示例提交。

回答by Nakilon

I'll leave here just another form of this command that suited my needs.
There was a tag v0.0.1.2that I wanted to move.

我将在这里只留下适合我需要的此命令的另一种形式。
有一个v0.0.1.2我想移动的标签。

$ git tag -f v0.0.1.2 63eff6a

Updated tag 'v0.0.1.2' (was 8078562)

And then:

进而:

$ git push --tags --force

回答by Алексей Югов

One other way:

另一种方式:

Move tag in remote repo.(Replace HEAD with any other if needed.)

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

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

Fetch changes back.

取回更改。

$ git fetch --tags

回答by Juan Antonio Tubío

Alias to move one tag to a different commit.

将一个标签移动到不同提交的别名。

In your sample, to move commit with hash e2ea1639 do: git tagm v0.1 e2ea1639.

在您的样品,以招犯了哈希e2ea1639做:git tagm v0.1 e2ea1639

For pushed tags, use git tagmp v0.1 e2ea1639.

对于推送的标签,请使用git tagmp v0.1 e2ea1639.

Both alias keeps you original date and message. If you use git tag -dyou lost your original message.

这两个别名都可以让您保留原始日期和消息。如果您使用,git tag -d您将丢失原始消息。

Save them on your .gitconfigfile

将它们保存在您的.gitconfig文件中

# Return date of tag. (To use in another alias)
tag-date = "!git show  | awk '{ if ( == \"Date:\") { print substr(
moveTag() {
  local tagName=
  # Support passing branch/tag names (not just full commit hashes)
  local newTarget=$(git rev-parse ^{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 {}
}
, index(##代码##,)) }}' | tail -2 | head -1 #" # Show tag message tag-message = "!git show | awk -v capture=0 '{ if(capture) message=message\"\n\"##代码##}; BEGIN {message=\"\"}; { if ( == \"Date:\" && length(message)==0 ) {capture=1}; if ( == \"commit\" ) {capture=0} }; END { print message }' | sed '$ d' | cat -s #" ### Move tag. Use: git tagm <tagname> <newcommit> tagm = "!GIT_TAG_MESSAGE=$(git tag-message ) && GIT_COMMITTER_DATE=$(git tag-date ) && git tag-message && git tag -d && git tag -a -m \"$GIT_TAG_MESSAGE\" #" ### Move pushed tag. Use: git tagmp <tagname> <newcommit> tagmp = "!git tagm && git push --delete origin && git push origin #"

回答by vossad01

If you want to move an annotated tag, changing only the targeted commit but preserving the annotation message and other metadata use:

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

##代码##

usage: moveTag <tag-to-move> <target>

用法: moveTag <tag-to-move> <target>

The above function was developed by referencing teerapap/git-move-annotated-tag.sh.

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