我如何在git分支上移动一个标记,以实现不同的提交?

时间:2022-11-24 23:51:36

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

我在主分支上创建了一个名为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.1 tag 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.

但是后来我意识到我还需要合并到master 0.1版本中,所以我做了。但是现在我的v0.1标记被错误的提交(调用post-it note类比)卡住了。我希望它被卡在最近提交的关于master的提交上,但是它却被卡在了最近提交给master的第二次提交上。

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

我如何将它转移到最近一次提交的master上?

8 个解决方案

#1


804  

Use the -f option to git tag:

使用-f选项来标记git:

-f
--force

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

You probably want to use -f in conjunction with -a to 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
    

#2


200  

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

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

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

#3


92  

To sum up if your remote is called origin and you're working on master branch:

如果你的遥控器叫origin,你正在做master branch:

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.
  • 第1行删除本地env中的标记。
  • Line 2 removes the tag in remote env.
  • 第2行删除远程env中的标记。
  • Line 3 adds the tag to different commit
  • 第3行向不同的提交添加标记
  • Line 4 pushes the change to the remote
  • 第4行将更改推到远程

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

您还可以交换第4行到git推源——标记将所有的更改与本地更改的标记一起推。

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的回答,在他们的回答下面评论,在我的回答下面评论。

#4


79  

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

使用git标签-d 删除它,然后在正确的提交上重新创建它。

#5


5  

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 -d you lost your original message.

两个别名都保留原始日期和消息。如果您使用git标签-d,您将丢失原始消息。

Save them on your .gitconfig file

将它们保存在.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 #"

#6


4  

I'll leave here just another form of this command that suited my needs.
There was a tag v0.0.1.2 that 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

#7


1  

One other way:

另一个方法:

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

在远程repo中移动标记。(如有需要,可更换头部。)

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

Fetch changes back.

获取更改回来。

$ git fetch --tags

#8


0  

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

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

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 {}
}

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

用法:moveTag < tag-to-move > <目标>

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

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

#1


804  

Use the -f option to git tag:

使用-f选项来标记git:

-f
--force

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

You probably want to use -f in conjunction with -a to 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
    

#2


200  

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

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

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

#3


92  

To sum up if your remote is called origin and you're working on master branch:

如果你的遥控器叫origin,你正在做master branch:

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.
  • 第1行删除本地env中的标记。
  • Line 2 removes the tag in remote env.
  • 第2行删除远程env中的标记。
  • Line 3 adds the tag to different commit
  • 第3行向不同的提交添加标记
  • Line 4 pushes the change to the remote
  • 第4行将更改推到远程

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

您还可以交换第4行到git推源——标记将所有的更改与本地更改的标记一起推。

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的回答,在他们的回答下面评论,在我的回答下面评论。

#4


79  

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

使用git标签-d 删除它,然后在正确的提交上重新创建它。

#5


5  

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 -d you lost your original message.

两个别名都保留原始日期和消息。如果您使用git标签-d,您将丢失原始消息。

Save them on your .gitconfig file

将它们保存在.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 #"

#6


4  

I'll leave here just another form of this command that suited my needs.
There was a tag v0.0.1.2 that 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

#7


1  

One other way:

另一个方法:

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

在远程repo中移动标记。(如有需要,可更换头部。)

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

Fetch changes back.

获取更改回来。

$ git fetch --tags

#8


0  

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

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

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 {}
}

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

用法:moveTag < tag-to-move > <目标>

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

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