我目前正在使用一个具有多个分支的存储库。

当我创建一个标记时,该标记是否引用当时的分支?

换句话说:每当我创建一个标记时,我是否需要切换到所需的分支和该分支中的标记,以便标记在该时间点引用该分支?


当前回答

一个相关且有用的命令是

git branch --contains tag/<tag>

这将为您提供所有分支的标签上的列表

这里有更多细节…

其他回答

如果你想从一个分支创建一个标签,比如release/yourbranch等 那么你应该使用

git tag YOUR_TAG_VERSION_OR_NAME origin/release/yourbranch

创建正确的标签后,如果您希望将标签推到远程,请使用该命令

git push origin YOUR_TAG_VERSION_OR_NAME

我们可以为一些过去的提交创建一个标签:

git tag [tag_name] [reference_of_commit]

eg:

git tag v1.0 5fcdb03

如果你想标记你所在的分支,那么输入:

git tag <tag>

用:

git push origin --tags

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>