如何在执行commit -amend时自动更新git标签 [英] How to update the git tag automatically when I do a commit --amend

查看:189
本文介绍了如何在执行commit -amend时自动更新git标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将 git commit --amend 添加到标记提交中,标记将会消失。我需要删除原始标签并再次添加。有没有办法将原始标签移动到新的提交?

你不能直接绑定创建一个新的提交( - - 和一个标签(仍然引用原始提交)。



您需要移动标记(保留其旧消息)并删除/替换远程标记。

Juan AntonioTubío有一个一组有趣的别名来促进这个顺序:

 #标签的返回日期。 (用于别名)
tag-date =!git show $ 1 | awk'{if($ 1 == \Date:\){print substr($ 0,index($ 0,$ 3) )}}'| tail -2 | head -1#

#显示标签消息
tag-message =!git show $ 1 | awk -v capture = 0'{if capture)message = message \\\\\
\$ 0}; BEGIN {message = \\}; {if($ 1 == \Date:\&& length (message)== 0){capture = 1}; if($ 1 == \commit\){capture = 0}}; END {print message}'| sed'$ d'| cat -s#

###移动标签。使用:git tagm<标记名称> < 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标记-a $ 1 $ 2 -m \$ GIT_TAG_MESSAGE\#

###移动推送标签。使用:git tagmp<标记名> < newcommit>
tagmp =!git tagm $ 1 $ 2&& git push --delete origin $ 1&& git push origin $ 1#

一旦您修改了提交(使用新的SHA1),您应该输入:

  git tagm< yourTag> < SHA> 


If I do git commit --amend to a tagged commit, the tag will disappeared. I need to delete the original tag and add it again. Is there any way to move the original tag to the new commit?

解决方案

You cannot directly tie the creation of a new commit (--amend) and a tag (which still references the original commit).

You would need to move the tag (keeping its old message) and delete/replace the tag on remote.
Juan Antonio Tubío has an interesting set of alias to facilitate that sequence:

# 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 #"

Once you have amended your commit (with a new SHA1 ), you would type:

git tagm <yourTag> <sha>

这篇关于如何在执行commit -amend时自动更新git标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆