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

git tag -a v0.1

但后来我意识到,在0.1版的master中还需要合并一些更改,所以我就这么做了。但是现在我的v0.1标签被粘在了错误的提交上(引用便利贴的类比)。我希望它卡在主节点上的最近提交上,但它却卡在主节点上的第二个最近提交上。

我如何移动它到最近提交的主?


当前回答

将一个标记移动到不同的提交。

在您的示例中,要移动提交散列e2ea1639 do: git tagm v0.1 e2ea1639。

对于推送标记,使用git tagmp v0.1 e2ea1639。

两个别名保持您原始的日期和消息。如果您使用git标记-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 #"

其他回答

将一个标记移动到不同的提交。

在您的示例中,要移动提交散列e2ea1639 do: git tagm v0.1 e2ea1639。

对于推送标记,使用git tagmp v0.1 e2ea1639。

两个别名保持您原始的日期和消息。如果您使用git标记-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 #"

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

$ git tag -f v0.0.1.2 63eff6a

Updated tag 'v0.0.1.2' (was 8078562)

然后:

$ git push --tags --force

如果期望的最终结果是更新远程上已经存在的标签的内容:

Git checkout <tag_name> 提交您的更改 Git标签-f <tag_name> Git push -f origin <tag_name>

更准确地说,你必须强制添加标签,然后用option——tags和-f push:

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

本地删除:

git tag -d v0.1  

远程移除:

git push origin --delete v0.1

然后在本地重新添加,并将v0.1推送到最近的提交:

git tag -a v0.1
git push origin --tags