Set master branch to latest tag

Peter picture Peter · Dec 27, 2012 · Viewed 12.2k times · Source

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?

Answer

Peter picture Peter · Dec 29, 2012

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 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

2) Once we have master back on 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 avoind being in a Detached HEAD mode and master branch is updated.