首页 > 解决方案 > 是否使用 git push --tags 指定 refspec 的区别

问题描述

我偶然发现了这个相当晦涩的问题,使用两个命令git push --tagsgit push --tags origin master. 在以下情况下,它们的行为不同:

初始情况

我正在尝试将新创建的标签git tag test(如图:

Remote [master] (one commit ahead):
A ----- B ----- C ---- D
Local  [master] (one commit behind):
A ----- B ----- C 
            (tag:test)

问题

git push --tags正在按预期工作:

Total 0 (delta 0), reused 0 (delta 0)
To https://remote.gitrepo.com/path/to/project
 * [new tag]         test14 -> test14

git push --tags origin master被拒绝和错误:

Total 0 (delta 0), reused 0 (delta 0)
To https://remote.gitrepo.com/path/to/project
  * [new tag]         test15 -> test15
  ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://remote.gitrepo.com/path/to/project'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

问题

为什么要git push --tags origin master尝试将某些东西从标签中推开?我在问为什么命令试图将分支推送到远程而不是为什么它被拒绝

标签: gitgit-push

解决方案


我在问为什么命令试图将分支推送到远程而不是为什么它被拒绝,

因为 git push 将推送新的提交和(使用--tags)新标签。

  • C 已经被推了,所以没有什么可以推到那里的
  • 新标签被推送。

在第二种情况下,您试图将远程主分支(位于 D)重置为 C(除了推送标签)。


推荐阅读