gitgithubgit-tag

How to create a tag after PR got merged but before merge commit?


I have a long running PR which was merged to main branch. However, I needed to create a tag for the main branch before the PR got merged for any future hot fixes!

Now that PR got merged (with merge strategy), how do I reach a point back in the history of main branch where I can create a tag which includes latest of the main branch excluding all the commits from that specific PR? Note that, the PR itself was updated with changes from main couple of times.

Is it even possible?

The question was closed marking as duplicate. The other question is about tagging against a particular commit, but the two questions are unrelated. I am already aware of how to tag against a commit; I need to know exactly which commit should I be using (and how to find that) after PR got merged so that I can tag against the main branch just before the PR merge. The PR merge has merged up the history.


Solution

  • The command git tag allows you to define a tag for any commit in the history, also commits prior to the HEAD commit of a branch. This is the general syntax for the command:

    git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] [-e] <tagname> [<commit> | <object>]
    

    In your case, if you're on the main branch and need to create a tag for the commit prior the merge, you can use:

    git tag -a my_tag HEAD^
    

    Update to answer the edited question

    The answer I gave previously still answers your question. However, it can work only under the condition that a merge commit has been produced. Here I'll deepen my answer and further into its details.

    Supposing that you've handled your PR on GitHub, or similar services, the merge has most likely produced a merge commit. In fact, these platforms tend to introduce a merge commit, even when the merge can be resolved with a fast-forward degenerate merge. This means that the tip of the main branch has two parents: main's former tip and the tip of the other branch (for simplicity, I'm assuming that no further changes have been applied after the merge on both main and the other branch).

    A merge commit has at least two parents, depending on the merge strategy and, therefore, on how many branches have concurred to the merge. So, based on the official documentation in the paragraph Ancestry References of Git Tools - Revision Selection, you could use the caret symbol to refer to main's former tip.

    You can also specify a number after the ^ to identify which parent you want; [...]. This syntax is useful only for merge commits, which have more than one parent — the first parent of a merge commit is from the branch you were on when you merged (frequently master), while the second parent of a merge commit is from the branch that was merged (say, topic).

    Therefore, my original post does answer your question and allows you to tag main's previous HEAD commit.

    git tag -a my_tag HEAD^
    

    Instead, if for some reason the PR wasn't handled on your hosting service, but locally with a git merge (although I highly doubt it, but let's say that's case), Git always saves the previous HEAD commit of a branch in ORIG_HEAD when performing certain operation, like merge or reset. Therefore, in this unlikely second scenario, you could tag main's former tip with:

    git tag -a my_tag ORIG_HEAD