提交时如何自动更新 git 标签 --amend

hou*_*use 3 git

如果我git commit --amend对标记的提交进行操作,标记将消失。我需要删除原始标签并重新添加。有没有办法将原始标签移动到新提交?

Von*_*onC 5

您不能直接将新提交 ( --amend)的创建和标记(仍引用原始提交)联系起来。

您需要移动标签(保留其旧消息)并删除/替换远程标签。
Juan Antonio Tubío有一组有趣的别名来促进这个序列:

# 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 #"
Run Code Online (Sandbox Code Playgroud)

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

git tagm <yourTag> <sha>
Run Code Online (Sandbox Code Playgroud)