git tag简介

原文: http://gitbook.liuhui998.com/3_7.html
http://web.mit.edu/~mkgray/project/silk/root/afs/sipb/project/git/git-doc/git-tag.html 
一、轻量级标签
我们可以用  git tag 不带任何参数创建一个标签(tag)指定某个提交(commit):
git tag   stable-1   1b2e1d63ff
这样,我们可以用 stable-1  作为提交(commit) " 1b2e1d63ff " 的代称(refer)。
前面这样创建的是一个“轻量级标签",这种分支通常是从来不移动的。
如果你想为一个标签(tag)添加注释,或是为它添加一个签名(sign it cryptographically), 那么我们就需要创建一个 ”标签对象".
二、标签对象
如果有 " -a ", " -s " 或是 " -u  " 中间的一个命令参数被指定,那么就会创建 一个标签对象,并且需要一个标签消息(tag message). 如果没有" -m  " 或是 " -F  " 这些参数,那么就会启动一个编辑器来让用户输入标签消息(tag message).
当这样的一条命令执行后,一个新的对象被添加到Git对象库中,并且 标签引用 就指向了 一个 标签对象 ,而不是指向一个提交(commit). 这样做的好处就是:你可以为一个标签 打处签名(sign), 方便你以后来查验这是不是一个正确的提交(commit).
下面是一个创建标签对象的例子:
git tag   -a   stable-1   1b2e1d63ff
标签对象可以指向任何对象,但是在通常情况下是一个提交(commit). (在Linux内核代 码中,第一个标签对象是指向一个树对象(tree),而不是指向一个提交(commit)).
三、签名的标签
如果你配有GPG key,那么你就很容易创建签名的标签.首先你要在你的 .git/config 或 ~.gitconfig里配好key.
下面是示例:
[user]
    signingkey =
你也可以用命令行来配置:
$ git config (--global) user.signingkey
现在你可以直接用"-s" 参数来创“签名的标签”。
$ git tag -s stable-1 1b2e1d63ff
如果没有在配置文件中配GPG key,你可以用"-u“ 参数直接指定。
$  git tag   -u   >  stable-1   1b2e1d63ff
四、语法详解

语法

git tag [-a | -s | -u ] [-f] [-m | -F ] [ | ] git tag -d git tag [-n[]] -l [--contains ] [--points-at ] […] git tag -v

DESCRIPTION

Add a tag reference in .git/refs/tags/, unless -d/-l/-v is given to delete, list or verify tags.

Unless -f is given, the tag to be created must not yet exist in the .git/refs/tags/ directory.

If one of -a-s, or -u  is passed, the command creates a tag object, and requires a tag message. Unless -m  or -F  is given, an editor is started for the user to type in the tag message.

If -m  or -F  is given and -a-s, and -u  are absent, -a is implied.

Otherwise just a tag reference for the SHA1 object name of the commit object is created (i.e. a lightweight tag).

A GnuPG signed tag object will be created when -s or -u  is used. When -u  is not used, the committer identity for the current user is used to find the GnuPG key for signing. The configuration variable gpg.program is used to specify custom GnuPG binary.

OPTIONS

-a
--annotate

Make an unsigned, annotated tag object

-s
--sign

Make a GPG-signed tag, using the default e-mail address’s key.

-u
--local-user=

Make a GPG-signed tag, using the given key.

-f
--force

Replace an existing tag with the given name (instead of failing)

-d
--delete

Delete existing tags with the given names.

-v
--verify

Verify the gpg signature of the given tag names.

-n

specifies how many lines from the annotation, if any, are printed when using -l. The default is not to print any annotation lines. If no number is given to -n, only the first line is printed. If the tag is not annotated, the commit message is displayed instead.

-l
--list

List tags with names that match the given pattern (or all if no pattern is given). Running "git tag" without arguments also lists all tags. The pattern is a shell wildcard (i.e., matched using fnmatch(3)). Multiple patterns may be given; if any of them matches, the tag is shown.

--contains

Only list tags which contain the specified commit.

--points-at

Only list tags of the given object.

-m
--message=

Use the given tag message (instead of prompting). If multiple -m options are given, their values are concatenated as separate paragraphs. Implies -a if none of -a-s, or -u  is given.

-F
--file=

Take the tag message from the given file. Use - to read the message from the standard input. Implies -a if none of -a-s, or -u  is given.

--cleanup=

This option sets how the tag message is cleaned up. The  can be one of verbatimwhitespace and strip. The strip mode is default. Theverbatim mode does not change message at all, whitespace removes just leading/trailing whitespace lines and strip removes both whitespace and commentary.

The name of the tag to create, delete, or describe. The new tag name must pass all checks defined by git-check-ref-format(1). Some of these checks may restrict the characters allowed in a tag name.

CONFIGURATION

By default, git tag in sign-with-default mode (-s) will use your committer identity (of the form "Your Name ") to find a key. If you want to use a different default key, you can specify it in the repository configuration as follows:

[user]
    signingkey = 

DISCUSSION

On Re-tagging

What should you do when you tag a wrong commit and you would want to re-tag?

If you never pushed anything out, just re-tag it. Use "-f" to replace the old one. And you’re done.

But if you have pushed things out (or others could just read your repository directly), then others will have already seen the old tag. In that case you can do one of two things:

  1. The sane thing. Just admit you screwed up, and use a different name. Others have already seen one tag-name, and if you keep the same name, you may be in the situation that two people both have "version X", but they actually have different "X"'s. So just call it "X.1" and be done with it.

  2. The insane thing. You really want to call the new version "X" too, even though others have already seen the old one. So just use git tag -fagain, as if you hadn’t already published the old one.

However, Git does not (and it should not) change tags behind users back. So if somebody already got the old tag, doing a git pull on your tree shouldn’t just make them overwrite the old one.

If somebody got a release tag from you, you cannot just change the tag for them by updating your own one. This is a big security issue, in that people MUST be able to trust their tag-names. If you really want to do the insane thing, you need to just fess up to it, and tell people that you messed up. You can do that by making a very public announcement saying:

 
     

Ok, I messed up, and I pushed out an earlier version tagged as X. I

then fixed something, and retagged the *fixed* tree as X again.

If you got the wrong tag, and want the new one, please delete

the old one and fetch the new one by doing:

        git tag -d X

        git fetch origin tag X

to get my updated tag.

You can test which tag you have by doing

        git rev-parse X

which should return 0123456789abcdef.. if you have the new version.

Sorry for the inconvenience.

Does this seem a bit complicated? It should be. There is no way that it would be correct to just "fix" it automatically. People need to know that their tags might have been changed.

On Automatic following

If you are following somebody else’s tree, you are most likely using remote-tracking branches (refs/heads/origin in traditional layout, orrefs/remotes/origin/master in the separate-remote layout). You usually want the tags from the other end.

On the other hand, if you are fetching because you would want a one-shot merge from somebody else, you typically do not want to get tags from there. This happens more often for people near the toplevel but not limited to them. Mere mortals when pulling from each other do not necessarily want to automatically get private anchor point tags from the other person.

Often, "please pull" messages on the mailing list just provide two pieces of information: a repo URL and a branch name; this is designed to be easily cut&pasted at the end of a git fetch command line:

 
     

Linus, please pull from

        git://git..../proj.git master

to get the following updates...

becomes:

$ git pull git://git..../proj.git master

In such a case, you do not want to automatically follow the other person’s tags.

One important aspect of git is its distributed nature, which largely means there is no inherent "upstream" or "downstream" in the system. On the face of it, the above example might seem to indicate that the tag namespace is owned by the upper echelon of people and that tags only flow downwards, but that is not the case. It only shows that the usage pattern determines who are interested in whose tags.

A one-shot pull is a sign that a commit history is now crossing the boundary between one circle of people (e.g. "people who are primarily interested in the networking part of the kernel") who may have their own set of tags (e.g. "this is the third release candidate from the networking group to be proposed for general consumption with 2.6.21 release") to another circle of people (e.g. "people who integrate various subsystem improvements"). The latter are usually not interested in the detailed tags used internally in the former group (that is what "internal" means). That is why it is desirable not to follow tags automatically in this case.

It may well be that among networking people, they may want to exchange the tags internal to their group, but in that workflow they are most likely tracking each other’s progress by having remote-tracking branches. Again, the heuristic to automatically follow such tags is a good thing.

On Backdating Tags

If you have imported some changes from another VCS and would like to add tags for major releases of your work, it is useful to be able to specify the date to embed inside of the tag object; such data in the tag object affects, for example, the ordering of tags in the gitweb interface.

To set the date used in future tag objects, set the environment variable GIT_COMMITTER_DATE (see the later discussion of possible values; the most common form is "YYYY-MM-DD HH:MM").

For example:

$ GIT_COMMITTER_DATE="2006-10-02 10:31" git tag -s v1.0.1

DATE FORMATS

The GIT_AUTHOR_DATE, GIT_COMMITTER_DATE environment variables support the following date formats:

Git internal format

It is  , where  is the number of seconds since the UNIX epoch.  is a positive or negative offset from UTC. For example CET (which is 2 hours ahead UTC) is +0200.

RFC 2822

The standard email format as described by RFC 2822, for example Thu, 07 Apr 2005 22:13:13 +0200.

ISO 8601

Time and date specified by the ISO 8601 standard, for example 2005-04-07T22:13:13. The parser accepts a space instead of the T character as well.

Note
In addition, the date part is accepted in the following formats: YYYY.MM.DDMM/DD/YYYY and DD.MM.YYYY.
五、标签相关操作举例
标签可以针对某一时间点的版本做标记,常用于版本发布。
其常用操作如下:
1、列出标签
$ Git tag # 在控制台打印出当前仓库的所有标签
$ git tag -l ‘v0.1.*’ # 搜索符合模式的标签
2、打标签
git标签分为两种类型:轻量标签和附注标签。轻量标签是指向提交对象的引用,附注标签则是仓库中的一个独立对象。建议使用附注标签。
# 创建轻量标签
$ git tag v0.1.2-light
# 创建附注标签
$ git tag -a v0.1.2 -m “0.1.2版本”
创建轻量标签不需要传递参数,直接指定标签名称即可。
创建附注标签时,参数a即annotated的缩写,指定标签类型,后附标签名。参数m指定标签说明,说明信息会保存在标签对象中。
3、切换到标签
与切换分支命令相同,用git checkout [tagname]
4、查看标签信息
用git show命令可以查看标签的版本信息:
$ git show v0.1.2
5、删除标签
误打或需要修改标签时,需要先在本地将标签删除,推送到服务器,再打新标签。
$ git tag -d v0.1.2 # 删除标签
参数d即delete的缩写,意为删除其后指定的标签。
git push origin :refs/tags/ v0.1.2 #推送到服务器


6、给指定的commit打标签
打标签不必要在head之上,也可在之前的版本上打,这需要你知道某个提交对象的校验和(通过git log获取)。
# 补打标签
$ git tag -a v0.1.1 9fbc3d0
7、标签发布
通常的git push不会将标签对象提交到git服务器,我们需要进行显式的操作:
$ git push origin v0.1.2 # 将v0.1.2标签提交到git服务器
$ git push origin --tags # 将本地所有标签一次性提交到git服务器


 


注意:如果想看之前某个标签状态下的文件,可以这样操作


1.git tag   查看当前分支下的标签


2.git  checkout v0.21   此时会指向打v0.21标签时的代码状态,(但现在处于一个空的分支上)


3. cat  test.txt   查看某个文件
结束

你可能感兴趣的:(版本控制)