什么是git标签,如何创建标签&如何签出git远程标签 [英] What is git tag, How to create tags & How to checkout git remote tag(s)

查看:110
本文介绍了什么是git标签,如何创建标签&如何签出git远程标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我签出远程git标签时,请使用以下命令:

when I checkout remote git tag use command like this:

git checkout -b local_branch_name origin/remote_tag_name

我遇到这样的错误:

error: pathspec `origin/remote_tag_name` did not match any file(s) known to git.

使用git tag命令时可以找到remote_tag_name.

I can find remote_tag_name when I use git tag command.

推荐答案

让我们开始解释git中的标记是什么

标签用于标记和标记历史记录中的特定 commit .
通常用于标记发布点(例如v1.0等).

A tag is used to label and mark a specific commit in the history.
It is usually used to mark release points (eg. v1.0, etc.).

尽管标签可能看起来与分支相似,但标签不会更改.它直接指向指向历史记录中的特定提交,除非明确更新,否则不会更改.

Although a tag may appear similar to a branch, a tag, however, does not change. It points directly to a specific commit in the history and will not change unless explicitly updated.

如果标签不在您的存储库中,您将无法检出标签,因此,首先,您必须将标签fetch到本地存储库中.

You will not be able to checkout the tags if it's not locally in your repository so first, you have to fetch the tags to your local repository.

首先,通过执行以下操作确保标记在本地存在

# --all will fetch all the remotes.
# --tags will fetch all tags as well
$ git fetch --all --tags --prune

然后通过运行

$ git checkout tags/<tag_name> -b <branch_name>

使用tags/前缀代替origin.

在此示例中,您有2个标签的版本为1.0& 1.1版,您可以使用以下任意一种方法将其检出:

In this sample you have 2 tags version 1.0 & version 1.1 you can check them out with any of the following:

$ git checkout A  ...
$ git checkout version 1.0  ...
$ git checkout tags/version 1.0  ...

由于标记只是指向给定提交的指针,因此上述所有操作都将执行相同的操作.

All of the above will do the same since the tag is only a pointer to a given commit.


来源: https://backlog.com/git-tutorial/img/post /stepup/capture_stepup4_1_1.png


origin: https://backlog.com/git-tutorial/img/post/stepup/capture_stepup4_1_1.png

# list all tags
$ git tag

# list all tags with given pattern ex: v-
$ git tag --list 'v-*'


如何创建标签?

有两种创建标签的方法:


How to create tags?

There are 2 ways to create a tag:

# lightweight tag 
$ git tag 

# annotated tag
$ git tag -a

两者之间的区别在于,创建带注释的标签时,您可以像添加git commit一样添加元数据:
姓名,电子邮件,日期,评论&签名

The difference between the 2 is that when creating an annotated tag you can add metadata as you have in a git commit:
name, e-mail, date, comment & signature

# delete any (local) given tag
$ git tag -d <tag name>

# Delete a tag from the server with push tags
$ git push --delete origin <tag name>

如何克隆特定标签?

为了获取给定标签的内容,可以使用checkout命令.如上所述,标记与其他任何提交一样,因此我们可以使用checkout而不是使用SHA-1,而只需将其替换为 tag_name

How to clone a specific tag?

In order to grab the content of a given tag, you can use the checkout command. As explained above tags are like any other commits so we can use checkout and instead of using the SHA-1 simply replacing it with the tag_name

选项1:

# Update the local git repo with the latest tags from all remotes
$ git fetch --all

# checkout the specific tag
$ git checkout tags/<tag> -b <branch>

选项2:

由于git通过将--branch添加到clone命令中来支持 shallow clone ,因此我们可以使用标签名称代替分支名称. Git知道如何翻译"翻译.给定SHA-1的相关提交

Since git supports shallow clone by adding the --branch to the clone command we can use the tag name instead of the branch name. Git knows how to "translate" the given SHA-1 to the relevant commit

# Clone a specific tag name using git clone 
$ git clone <url> --branch=<tag_name>

git clone --branch =

--branch还可以获取标签并在结果存储库中的提交时分离HEAD.

--branch can also take tags and detaches the HEAD at that commit in the resulting repository.


如何推送标签?

git push --tags

要推送所有标签,请执行以下操作:


How to push tags?

git push --tags

To push all tags:

# Push all tags
$ git push --tags 

使用refs/tags而不是仅指定<tagname>.

为什么?

Using the refs/tags instead of just specifying the <tagname>.

Why?

  • 建议使用refs/tags,因为有时标签可以与分支具有相同的名称,而简单的git push会推送分支而不是标签
  • It's recommended to use refs/tags since sometimes tags can have the same name as your branches and simple git push will push the branch instead of the tag

要推送带注释的标签和当前历史记录链标签,请使用:

To push annotated tags and current history chain tags use:

### git push --follow-tags

###git push --follow-tags

此标志--follow-tags会同时推送提交仅标记:

  • 带注释的标签(因此您可以跳过本地/临时构建标签)
  • 当前分支(位于历史记录中)的可到达标签(祖先)

在Git 2.4中,您可以使用配置进行设置

From Git 2.4 you can set it using configuration

$ git config --global push.followTags true


备忘单:


Cheatsheet:

这篇关于什么是git标签,如何创建标签&amp;如何签出git远程标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆