269

I'm trying to find the syntax for merging a tagged commit onto another branch. I guess it's straightforward but my feeble search attempts aren't finding it.

6 Answers 6

416

You mean this?

git checkout destination_branch git merge tag_name 
Sign up to request clarification or add additional context in comments.

7 Comments

Same here so I git fetch --tags origin then I could: git merge tagname
Is there a way to merge all tags at once?
Is it possible the other way? Merge a branch to the tag? I tried "git checkout tag_name" and "git merge branch". But ended up checking out the branch instead of merging.
@learner a Tag identifies a specific commit. You can't merge into a specific commit so you'd need to move the tag to the commit you want. This would address the how on that: stackoverflow.com/questions/8044583/…
Ensure that tags are pushed after they are created so that they are available at the origin (remote)'s branch, and therefore others can get them: git push origin <tagname> If you missed pushing tags in the past, push all using: git push origin --tags To fetch/pull tags: git fetch[pull] --tag
|
116

Remember before you merge you need to update the tag, it's quite different from branches (git pull origin tag_name won't update your local tags). Thus, you need the following command:

git fetch --tags origin 

Then you can perform git merge tag_name to merge the tag onto a branch.

1 Comment

I had to do git remote add upstream [email protected]/org/repo followed by git fetch --tags upstream to make it work.
8

Just complementing the answer.

Merging the last tag on a branch:

git checkout my-branch git merge $(git describe --tags $(git rev-list --tags --max-count=1)) 

Inspired by https://gist.github.com/rponte/fdc0724dd984088606b0

Comments

4

With modern versions merge will autodetect the tags as follows

git checkout <my-branch> git merge tags/<my-tag> 

Comments

1

This is the only comprehensive and reliable way I've found to do this.

Assume you want to merge "tag_1.0" into "mybranch".

 $git checkout tag_1.0 (will create a headless branch) $git branch -D tagbranch (make sure this branch doesn't already exist locally) $git checkout -b tagbranch $git merge -s ours mybranch $git commit -am "updated mybranch with tag_1.0" $git checkout mybranch $git merge tagbranch 

Comments

1

I'm late to the game here, but another approach could be:

1) create a branch from the tag ($ git checkout -b [new branch name] [tag name])

2) create a pull-request to merge with your new branch into the destination branch

2 Comments

This will just create unnecessary branches.
it depends @Anuja 'cause you can delete the source branch after the merge via tool or via command line

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.