Search code examples
gitversioningtagging

How to add only one commit to a git tag


I have created a git tag, but then I made some other commits and one of those needs to be in the tag, the others do not.

For example:

git commit 123
git commit 456
git commit 789
git commit 1011
git commit 1213

I made a tag at point 1011, but now I want to add commit 123 to that and not any of the ones in between. Any way to do that?


Solution

  • I think you have your terminology mixed up.

    • A tag is a string label attached to a single commit. It doesn't "contain" commits, it's just another way to name the commit that was tagged.
    • A branch is a ordered set of commits, when you use git commit you'll add the commit to the current branch.

    It's not clear if you just meant branch instead of tag, or if you're asking why the output of e.g. git log mytag is showing all commits since up until 1011.

    git log mytag will log the commit tagged with mytag and all its parents.

    If you meant branch and the branch you created does not yet contain commit 123 in it's history, you can git cherry-pick it.

    Writing this, I realize you might also have meant how to add commit 123 to set of commits listed by git log mytag. To do that you need to create a new branch at that point, git checkout -b mybranch mytag, then cherry pick commit 123, then move the tag to the new commit, using git tag -f. Then you can go back to your previous branch (e.g. git checkout master). Your previous branch will then have a different history, the branches have diverged.