245

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.

Denis Howe
  • 2,092
  • 1
  • 23
  • 25
Jeremy Woodland
  • 3,444
  • 5
  • 17
  • 25

6 Answers6

387

You mean this?

git checkout destination_branch
git merge tag_name
twalberg
  • 59,951
  • 11
  • 89
  • 84
  • 18
    Same here so I git fetch --tags origin then I could: git merge tagname – Will Hancock Mar 25 '15 at 16:41
  • 1
    Is there a way to merge all tags at once? – ComFreek Aug 18 '19 at 12:22
  • 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 Jan 20 '20 at 12:52
  • @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: https://stackoverflow.com/questions/8044583/how-can-i-move-a-tag-on-a-git-branch-to-a-different-commit – Josiah Jun 16 '20 at 15:35
  • 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 If you missed pushing tags in the past, push all using: git push origin --tags To fetch/pull tags: git fetch[pull] --tag – jsa Nov 01 '21 at 22:35
  • This worked for me. But only the problem is, git does not show any inform after tag merge. I expected it will be showing the changes in chageslist. It is not showing. I am using Android studio with in git integrated. Any advice to see the change list ?? – Raghu Mudem Jan 31 '22 at 15:32
  • This should be like the first answer (I mean in top lol). Thanks a lot! – Rodrigo Bittencourt Mar 28 '22 at 14:12
110

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.

DeadManSpirit
  • 2,036
  • 2
  • 20
  • 27
  • 6
    I had to do `git remote add upstream git@github.com/org/repo` followed by `git fetch --tags upstream` to make it work. – MarkHu Jan 18 '17 at 23:54
7

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

e-ruiz
  • 101
  • 1
  • 4
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
paiego
  • 3,619
  • 34
  • 43
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

ForTheWin
  • 617
  • 1
  • 8
  • 15
1

With modern versions merge will autodetect the tags as follows

git checkout <my-branch>
git merge tags/<my-tag>
Pato Sandaña
  • 519
  • 5
  • 14