我目前正在使用一个具有多个分支的存储库。
当我创建一个标记时,该标记是否引用当时的分支?
换句话说:每当我创建一个标记时,我是否需要切换到所需的分支和该分支中的标记,以便标记在该时间点引用该分支?
我目前正在使用一个具有多个分支的存储库。
当我创建一个标记时,该标记是否引用当时的分支?
换句话说:每当我创建一个标记时,我是否需要切换到所需的分支和该分支中的标记,以便标记在该时间点引用该分支?
当前回答
如果你想从一个分支创建一个标签,比如release/yourbranch等 那么你应该使用
git tag YOUR_TAG_VERSION_OR_NAME origin/release/yourbranch
创建正确的标签后,如果您希望将标签推到远程,请使用该命令
git push origin YOUR_TAG_VERSION_OR_NAME
其他回答
CharlesB的回答和helmbert的回答都很有用,但我花了一些时间来理解它们。 这是另一种表达方式:
A tag is a pointer to a commit, and commits exist independently of branches. It is important to understand that tags have no direct relationship with branches - they only ever identify a commit. That commit can be pointed to from any number of branches - i.e., it can be part of the history of any number of branches - including none. Therefore, running git show <tag> to see a tag's details contains no reference to any branches, only the ID of the commit that the tag points to. (Commit IDs (a.k.a. object names or SHA-1 IDs) are 40-character strings composed of hex. digits that are hashes over the contents of a commit; e.g.: 6f6b5997506d48fc6267b0b60c3f0261b6afe7a2)
Branches come into play only indirectly: At the time of creating a tag, by implying the commit that the tag will point to: Not specifying a target for a tag defaults to the current branch's most recent commit (a.k.a. HEAD); e.g.: git tag v0.1.0 # tags HEAD of *current* branch Specifying a branch name as the tag target defaults to that branch's most recent commit; e.g.: git tag v0.1.0 develop # tags HEAD of 'develop' branch (As others have noted, you can also specify a commit ID explicitly as the tag's target.) When using git describe to describe the current branch: git describe [--tags] describes the current branch in terms of the commits since the most recent [possibly lightweight] tag in this branch's history. Thus, the tag referenced by git describe may NOT reflect the most recently created tag overall.
如果你通过例如。
git tag v1.0
标记将引用您当前所在分支的最近一次提交。你可以改变分支并在那里创建一个标签。
你也可以在标记时引用另一个分支,
git tag v1.0 name_of_other_branch
这将为另一个分支的最近提交创建标记。
或者,通过直接引用某个提交的SHA1,您可以将标记放在任何地方,无论哪个分支
git tag v1.0 <sha1>
当只调用git tag <TAGNAME>而没有任何附加参数时,git将从你当前的HEAD(即你当前分支的HEAD)创建一个新标签。当向这个分支添加额外的提交时,分支HEAD将跟上这些新的提交,而标记总是引用相同的提交。
当调用git标签<TAGNAME> <COMMIT>时,你甚至可以指定使用哪个提交来创建标签。
无论如何,标记仍然只是指向某个提交(而不是分支)的“指针”。
标记和分支是完全不相关的,因为标记引用的是特定的提交,而分支是对历史记录的最后一次提交的移动引用。树枝走了,标签留了。
所以当你标记一个提交时,git并不关心哪个提交或分支被签出,只要你向他提供你想要标记的内容的SHA1。
我甚至可以通过引用一个分支来标记(然后它会标记分支的尖端),然后说分支的尖端在其他地方(git重置——例如很难),或者删除分支。但是我创建的标签不会移动。
我们可以为一些过去的提交创建一个标签:
git tag [tag_name] [reference_of_commit]
eg:
git tag v1.0 5fcdb03