Foad Rezek
Foad Rezek

Reputation: 722

git push --tag keeps commits

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 ?

Upvotes: 1

Views: 1425

Answers (3)

Matthieu Moy
Matthieu Moy

Reputation: 16517

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.

Upvotes: 1

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476554

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.

Upvotes: 1

firelynx
firelynx

Reputation: 32214

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

Upvotes: 0

Related Questions