Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

Various answers/comments already given about why to not do things this way, but here's how you fix this particular scenario:

git checkout -b tmpbranch       # creates a branch called tmpbranch at HEAD
git checkout master             # switch back to master branch
git merge --ff-only tmpbranch   # fast-forward merge master to tmpbranch, fail if not possible
git branch -d tmpbranch         # delete tmpbranch, it's not needed anymore

Then, going forward, don't check out a tag, except for this way:

git checkout -b somebranch refs/tags/tagname    # creates a new branch starting at tag

That way, you will not be in detached HEAD state, and any new commits will be added starting at the tag in question, which seems to be what you want... After making a commit you can git tag newtag to create additional tags at the right points.

A branch doesn't reference a tag.
A tag references a fixed commit.

So as long as you git checkout master, you are not in a detached HEAD mode.
You can then commit and tag: the tag will be created on the LATEST of the current branch.

If you were in a detached HEAD mode, see "Git: How can I reconcile detached HEAD with master/origin?" for various way of reconciling a detached commit with a branch.

Tags are used for creating stable releases. To create a tag for using with the Git Drupal Repository, first, ensure that you're following the tag naming convention if you're using this tag for making a release. From inside the directory of the project, an example is:

git tag 7.x-1.0

Once the tag is created, you need to push the tag up to the master repository. By itself, push doesn't send the tags up, you also need to tell it to include the tags in the push by appending the --tags flag:

git push --tags

If you don't want to push all your tags, you can also be specific: Example:

git push origin tag 7.x-1.0

To check and confirm remote tags, the command is : -

git tag -l

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top