inquiring minds
inquiring minds

Reputation: 1915

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?

Upvotes: 3

Views: 7185

Answers (3)

JCotton
JCotton

Reputation: 11770

Starting from your tag, you'll want to cherrypick the single commit you want to include. Then, you'll move your tag to point at the new state.

git checkout my_tag
git cherry-pick commit_123
git tag -f my_tag

It may help to think of tags as branches. Just like a branch, a tag refers to a particular point along a history of commits. So what you want to do is effectively create a new branch with a different commit history, then tag that new state. Checking out your current tag brings you back to the original point, cherrypicking pulls in the single commit you want to add, then retagging saves that new branch state. You'll need the -f option to "move" the tag otherwise you'll get a tag 'my_tag' already exists error.

Upvotes: 12

Per Johansson
Per Johansson

Reputation: 6887

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.

Upvotes: 10

markw
markw

Reputation: 630

If all of the changes are still local you should be able to squash the 1213 commit with 1011 and retag the squashed commit.

Upvotes: 0

Related Questions