Search code examples
gitgit-taggit-amend

git commit amendment without losing the tags


I have the following script that runs on post-commit hook of Git:

#!/bin/sh
# by Martin Seeler, and
# by Jorge Javier Araya Navarro

# destination of the final changelog file
OUTPUT_FILE=CHANGELOG.md

# generate the changelog
if ! type gitchangelog > /dev/null; then
    echo "ERROR: Please install gitchangelog"
    exit 1
fi

gitchangelog > $OUTPUT_FILE

# prevent recursion!
# since a 'commit --amend' will trigger the post-commit script again
# we have to check if the changelog file has changed or not
res=$(git status --porcelain | grep $OUTPUT_FILE | wc -l)
if [ "$res" -gt 0 ]; then
  git add $OUTPUT_FILE
  git commit --amend --no-edit
  echo "Populated Changelog in $OUTPUT_FILE"
fi

This is convenient for generating a CHANGELOG file using gitchangelog without creating a new commit to register the change on that file.

However, if I had a tag on HEAD, after commit --amend is ran the tag is "lost", thus I have to delete it locally and remotely and recreate it which is annoying.

I'm trying to figure a way for the script to move the tags after the amend, but at this point I not sure what I'm doing. Should I list first the tags on HEAD? May I move the tags after the amend? Will Git know what I mean anyway?


Solution

  • You can look at git tagm alias I described here in order to move your tag after a git commit --amend.

    That would automate the sequence of:

    • finding the tag,
    • moving (ie recreating it)
    • deleting the old one.