1

When pushing to the master, I read that it is recommended to use:

git tag -a <tag name> -m <message> ; git push --tags

I added a new tag, and pushed the changes to the master, now I modified another file and want to push it to the master with the same tag.

I do: git push --tags -f, I get "Everything up-to-date" message. but when I do: git status I still see that: "Your branch is ahead of 'origin/master' by 2 commits"

why it is so ? what I have to do to add the new changes to the tag ?

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Foad Rezek
  • 694
  • 2
  • 10
  • 22
  • ‘*push it to the master with the same tag*’ What?! – Biffen May 18 '15 at 11:53
  • 2
    possible duplicate of [Push git commits & tags simultaneously](http://stackoverflow.com/questions/3745135/push-git-commits-tags-simultaneously) – Hybris95 May 18 '15 at 11:53

3 Answers3

1

You need to push with

git push --all

and if there are new tags, with:

git push --all; git push --tag

Not git push --tags. --tags will push all references of tags, not commits on the master branch. I don't know where you have read this advice, but tagging constantly is not really encouraged. Normally you tag when you have a new release of your software. In other words a major milestone. Not for an ordinary commit.

Furthermore I would strongly discourage using the -f (also known as --force) flag (fully) automatically. If the repositories are out of sync, it is better not to force your commits over the shared repository. If there are warnings, you must try to resolve them, not overrule these warnings immediately.

General advice is to learn to use a tool instead of following a few steps without realising what is going under the hood.

EDIT:

You probably received an error when you added the tag a second time:

fatal: tag 'foo' already exists

(with foo the name of the tag). As the statement says, you can't simply tag with the same name twice. git push --tags commits content up to that tag. Since you assigned the tag with a previous commit, you will push up to that commit, not the latest. You can do a few things:

  • Use a different tag (recommended) and push with git push --tags;
  • Reuse the tag and force tagging git tag -f -a <tag name> -m <message>. In that case the old tag is removed. And git push --tags will work. The problem is that usually tags are used to specify a release. Users might say: aha, the latest release is release-2.3, I don't have to update the software whereas the new release-2.3 is different from the old one. Annotating with release-2.3-fix0 might make them aware you fixed something.
  • Use git push --all to push the commit up to the heads of all branches.

Background

You can see your commit graph like:

A -- B -- C -- D
         /
      <tag>

If you call git push --tag, it will synchronize A, B and C because that's the last commit under the supervision of <tag>. If you retag, the tag will be assigned to D instead.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • this is what I am doing, tagging for every release, this time I had a bug fix and wanted to include it in the same tag ... maybe I am missing how tags really work, I understand that they are snapshot of the master at specific time ( release time ... ). I though that push --tags pushes the changes and tag them with the available tag. so what does really push --tag does if I did git push --all and all my changed are pushed ? – Foad Rezek May 18 '15 at 12:04
  • @FoadRezek: but if you tag for the second time, you will get a fatal error, because the tag already exists. A tag is a reference to a commit (like google.com is a reference 216.58.210.46). If you push with `--tags` it will push **up to that tag**. But since your tag is placed a few commits behind (the new commit is not retagged), you push thus only up to the commits behind the head. – Willem Van Onsem May 18 '15 at 12:08
  • @FoadRezek: see uopdated anser. – Willem Van Onsem May 18 '15 at 12:14
  • The `--all` flag is completely unrelated from the issue. If you want to push all your branches, then use `--all`, but if you want the default behavior of `push` (configured by `push.default`), then don't use `--all` and let `git push` do its job. – Matthieu Moy May 18 '15 at 15:49
  • @MatthieuMoy: please read the edit. At that point it was unclear that the OP was working with tags... – Willem Van Onsem May 18 '15 at 15:51
  • I did read the edit, and I still think there's no relation between `--all` and the problem. You can just drop the `--force` even in your edit. – Matthieu Moy May 18 '15 at 16:00
1

When you run a git push, several things happen:

  1. Some commits are sent to the remote repository
  2. The remote branch (e.g. master on the remote machine) is updated to point to the latest commit
  3. The remote-tracking branch (e.g. origin/master on the local machine) is updated to reflect the change on the remote branch

By specifying --tags, you modify the behavior to update only the tag reference (plus some other references you may have specified on the command-line, but you didn't). If you tagged the latest commit, then this commits and all its ancestors were pushed, but you didn't go through steps 2 and 3 above. As a consequence

  • Users looking at your remote repository will see the tag, but will see the branch some commits behind
  • Since git status uses the remote-tracking branch to determine whether you're ahead of the remote, it will tell you that you are ahead of the remote.

The simplest solution is to run two pushes:

  • git push to push your branches as usual (as specified by push.default, i.e. push the current branch to the configured upstream if it has the same name with recent Git)
  • After creating a tag, git push --tags to push your tags (will take almost no time since the commits are already pushed, Git just needs to create the tag on the remote repository)

If you want to do both at the same time, then see the answers to the question Push git commits & tags simultaneously.

Note that there's no reason to create a tag for each of your pushes. Usually, people create tags only for releases.

Community
  • 1
  • 1
Matthieu Moy
  • 15,151
  • 5
  • 38
  • 65
0

git push --tag only pushes the tag, not the commits.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
firelynx
  • 30,616
  • 9
  • 91
  • 101