git, OYB software

Rebasing in git without losing tags

Rewriting commits, squashing them, eliminating them, forgeting blobs or just rebasing from master to make future merges less painful. Git comes equiped with all we need to rewrite history.

But! doing so will have the nasty side effect of changing our commit hashes, and our tags will be now referencing commits that are out of tree. As we want to make changes further and further away in the branch history, manually adjusting the tags to the new hashes can become really tedious.

Enter git rebasetags



Get the script from github and make it executable.

To do it in one step, paste the following on your terminal

If you don’t like installing to /usr/local/bin  using sudo,  just copy git-rebasetags  wherever you like. It will work as long as the file is in the $PATH with execute permissions.


In case we are doing interactive rebasing, we will be presented with a bash shell where we can make any manual changes and finish (or abort) the rebase operation. Whenever we exit that shell, our tags will be reassigned to the new hashes.

Matching is performed based on the commit line. This means that, if we change it, we will still have to do manual adjustments, and the same goes whenever we have different commits with the same comment ( for example ‘Update‘ ).

Keep in mind that we are still changing history, so forced pushes, merges, conflicts and such niceties will happen.
Remember to keep a checked out copy of the repo before trying this, and use with care.

Extra: using git-filter-branch

If you only want to do an action that can be automated with git-filter-branch, then you can use the –tag-name-filter option to restore tags to the each commit after the operation



Author: nachoparker

Humbly sharing things that I find useful [ github dockerhub ]

3 Comments on “Rebasing in git without losing tags

  1. Hi. This script looks great. I had some trouble with it on Windows 7 (I know…). Seems to have trouble importing pty in python. I made some tweaks, but it ends up not running that interactive rebase in the background and checking for its completion, just runs synchronously in one thread, which messes up if there are conflicts, etc. I’ll see if I can do this on a later version of windows or another OS.

  2. One more question, I’m guessing this script was intended for lightweight tags, not annotated tags, correct? Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *