4

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?

house
  • 93
  • 1
  • 7
  • 1
    When you do a `git commit --amend`, you are creating an entirely new commit, so your question is the same as asking how to move a tag from one commit to another one. – Tim Biegeleisen Jan 20 '16 at 04:33

1 Answers1

8

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>
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250