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