Dev,  Free Software,  Linux

Git: push a new commit before a tag

Last day, I faced a case where I had to backport a bug in a software which was released by a git tag.

As far I know, there is no easy way to simply push a patch before a tag, so here is how I proceeded:

# Checkout the upstream
git checkout master
git fetch --all
git pull
# List the tags
git tag

Let’s say we want to add a commit before 1.0.0:

# Create a branch from the tag
git checkout -b backport_1.0.0 1.0.0
# Apply your backport
touch fix
git add fix
git commit -m "Fix a bug"
git push -u origin backport_1.0.0

Recreate the tag:

git checkout master
# Delete the tag
git tag -d 1.0.0
git push origin :refs/tags/1.0.0
# Create the tag from the branch
git tag -a 1.0.0 backport_1.0.0
git push --tags origin master

That’s it! If you checkout the new tag, you should see your “fix” patchset as the last patch before the tag 1.0.0.

Software Engineeer at Red Hat, Private Pilot, French guy hiding somewhere in Canada.