How to update the git tag automatically when I do a commit --amend

前端 未结 1 1695
粉色の甜心
粉色の甜心 2021-01-14 09:50

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 origin

相关标签:
1条回答
  • 2021-01-14 10:36

    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>
    
    0 讨论(0)
提交回复
热议问题