Git 标签只适用于当前分支吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14613540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do Git tags only apply to the current branch?
提问by Gerardo
I'm currently working with a repository that has multiple branches.
我目前正在使用具有多个分支的存储库。
When I create a tag, does that tag refer to the then-current branch?
当我创建一个标签时,该标签是否引用了当时的分支?
In other words: Whenever I create a tag, do I need to switch to the desired branch and tag inside that branch so that the tag refers to that branch at that point in time?
换句话说:每当我创建一个标签时,我是否需要切换到所需的分支并在该分支内进行标记,以便该标记在那个时间点引用该分支?
采纳答案by Kalle Pokki
If you create a tag by e.g.
如果您通过例如创建标签
git tag v1.0
the tag will refer to the most recent commit of the branch you are currently on. You can change branch and create a tag there.
该标签将引用您当前所在分支的最新提交。您可以更改分支并在那里创建标签。
You can also just refer to the other branch while tagging,
您也可以在标记时引用另一个分支,
git tag v1.0 name_of_other_branch
which will create the tag to the most recent commit of the other branch.
这将创建标签到另一个分支的最新提交。
Or you can just put the tag anywhere, no matter which branch, by directly referencing to the SHA1 of some commit
或者你可以将标签放在任何地方,无论哪个分支,通过直接引用某个提交的 SHA1
git tag v1.0 <sha1>
回答by mklement0
CharlesB's answerand helmbert's answerare both helpful, but it took me a while to understand them. Here's another way of putting it:
CharlesB 的回答和helmbert 的回答都有帮助,但我花了一段时间才理解它们。这是另一种表达方式:
- A tagis a pointer to a commit, and commits exist independently of branches.
- It is important to understand that tags have no directrelationship 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
)
- (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.:
- It is important to understand that tags have no directrelationship with branches- they only ever identify a commit.
- 甲标签是一个指针,指向一个提交,并提交独立存在的分支。
- 重要的是要了解标签与分支没有直接关系——它们只标识一次提交。
- 该提交可以从任意数量的分支指向——即,它可以是任意数量分支历史的一部分——包括一个分支。
- 因此,运行
git show <tag>
以查看标记的详细信息不包含对任何分支的引用,仅包含标记指向的提交的 ID。- (提交的ID(又名对象名或SHA-1的ID)的40个字符的字符串构成的十六进制中超过的内容散列提交数字;例如:。
6f6b5997506d48fc6267b0b60c3f0261b6afe7a2
)
- (提交的ID(又名对象名或SHA-1的ID)的40个字符的字符串构成的十六进制中超过的内容散列提交数字;例如:。
- 重要的是要了解标签与分支没有直接关系——它们只标识一次提交。
- Branches come into play only indirectly:
- At the time of creatinga tag, by implying the committhat 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.)
- Not specifying a target for a tag defaults to the current branch's most recent commit (a.k.a. HEAD); e.g.:
- 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.
- At the time of creatinga tag, by implying the committhat the tag will point to:
- 分支仅间接发挥作用:
- 在创建标签时,通过暗示标签将指向的提交:
- 不为标签指定目标默认为当前分支的最新提交(又名 HEAD);例如:
git tag v0.1.0 # tags HEAD of *current* branch
- 指定分支名称作为标记目标默认为该分支的最近提交;例如:
git tag v0.1.0 develop # tags HEAD of 'develop' branch
- (正如其他人所指出的,您还可以明确指定一个提交 ID 作为标签的目标。)
- 不为标签指定目标默认为当前分支的最新提交(又名 HEAD);例如:
- 当使用
git describe
来描述当前分支:git describe [--tags]
根据此分支历史记录中最近的 [可能是轻量级] 标记以来的提交来描述当前分支。- 因此,由 引用的标签
git describe
可能不会整体反映最近创建的标签。
- 在创建标签时,通过暗示标签将指向的提交:
回答by CharlesB
Tags and branch are completely unrelated, since tags refer to a specific commit, and branch is a moving reference to the last commit of a history. Branches go, tags stay.
标签和分支完全无关,因为标签是指特定的提交,而分支是对历史最后一次提交的移动引用。分支去,标签留下。
So when you tag a commit, git doesn't care which commit or branch is checked out, if you provide him the SHA1 of what you want to tag.
因此,当您标记提交时,git 不关心检出哪个提交或分支,如果您向他提供您想要标记的 SHA1。
I can even tag by refering to a branch (it will then tag the tip of the branch), and later say that the branch's tip is elsewhere (with git reset --hard
for example), or delete the branch. The tag I created however won't move.
我什至可以通过引用一个分支来标记(然后它会标记分支的尖端),然后说分支的尖端在别处(git reset --hard
例如),或者删除该分支。但是我创建的标签不会移动。
回答by helmbert
When calling just git tag <TAGNAME>
without any additional parameters, Git will create a new tag from your current HEAD (i.e. the HEAD of your current branch). When adding additional commits into this branch, the branch HEAD will keep up with those new commits, while the tag always refers to the same commit.
当git tag <TAGNAME>
不带任何附加参数调用时,Git 将从您当前的 HEAD(即您当前分支的 HEAD)创建一个新标签。当向这个分支添加额外的提交时,分支 HEAD 将跟上那些新的提交,而标签总是引用同一个提交。
When calling git tag <TAGNAME> <COMMIT>
you can even specify which commit to use for creating the tag.
调用时,git tag <TAGNAME> <COMMIT>
您甚至可以指定用于创建标签的提交。
Regardless, a tag is still simply a "pointer" to a certain commit (not a branch).
无论如何,标签仍然只是指向某个提交(而不是分支)的“指针”。
回答by Sana Hameed
We can create a tag for some past commit:
我们可以为一些过去的提交创建一个标签:
git tag [tag_name] [reference_of_commit]
eg:
例如:
git tag v1.0 5fcdb03
回答by Mutatos
If you want to tag the branch you are in, then type:
如果要标记您所在的分支,请输入:
git tag <tag>
and push the branch with:
并使用以下命令推送分支:
git push origin --tags
回答by BharathRao
If you want to create a tag from a branch which is something like release/yourbranch
etc
Then you should use something like
如果你想从一个类似release/yourbranch
etc的分支创建一个标签,那么你应该使用类似的东西
git tag YOUR_TAG_VERSION_OR_NAME origin/release/yourbranch
After creating proper tag if you wish to push the tag to remote then use the command
创建适当的标签后,如果您希望将标签推送到远程然后使用命令
git push origin YOUR_TAG_VERSION_OR_NAME