Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set master branch to latest tag

This is an example of how my git repo is right now:

v1.0    v1.1    v1.2
  |       |       |
  a   -   b   -   c
  |               |
master           HEAD

I usually commit, tag and push tags like this:

git commit -a -m "Commit msg"
git tag -a v1.3 -m "Tag msg"
git push --tags

The main problem I have is that the master branch doesn't move to the latest tag, so I'm always in a Detached HEAD state. Is there any way to fix this so the master branch will be always pointing to the latest pushed tag?

like image 949
Peter Avatar asked Dec 27 '12 11:12

Peter


People also ask

How do I get the most recent tags in git?

In order to find the latest Git tag available on your repository, you have to use the “git describe” command with the “–tags” option. This way, you will be presented with the tag that is associated with the latest commit of your current checked out branch.

How do you tag the master branch?

In order to create a new tag, you have to use the “git tag” command and specify the tag name that you want to create. As an example, let's say that you want to create a new tag on the latest commit of your master branch. To achieve that, execute the “git tag” command and specify the tagname.

How do I checkout to new tag?

In order to checkout the latest Git tag, first update your repository by fetching the remote tags available. As you can see, you retrieve multiple tags from your remote repository. Then, retrieve the latest tag available by using the “git describe” command.


1 Answers

In this particular case, I had to do the following:

  1. First set the master branch to point to the latest tag (where HEAD is pointing), because it is the most recent tag. To do so I created a new branch and merged master to it.
git branch -b exp
git merge -s ours master
git checkout master
git merge exp

Now master is the same as latest tag:

v1.0    v1.1    v1.2
  |       |       |
  a   -   b   -   c
                  |
                 HEAD
                  |
                master
  1. Once we have master back in place, we need to push both master and tags whenever we do a new commit:
git commit -a -m "Commit msg"
git tag -a v1.4 -m "Tag msg"
git push master --tags

This way we avoid being in a Detached HEAD mode and the master branch is updated.

like image 194
Peter Avatar answered Sep 22 '22 19:09

Peter