git 笔记记录(九) git里程碑

来源:互联网 发布:网络国际象棋游戏 编辑:程序博客网 时间:2024/05/16 00:51

里程碑即Tag,是人为的对提交的命名.使用任何数字版本号无论长短,都没有使用一个直观的表意的字符串来得方便.

1.显示里程碑 git tag命令.

bluceshang@bluceshang:~/gittemp/hello-world.git$ git tagjx/v1.0jx/v1.0-i18njx/v1.1jx/v1.2jx/v1.3jx/v2.0jx/v2.1jx/v2.2jx/v2.3
不带任何参数执行git tag即可显示当前版本库的里程碑列表.,里程碑创建的时候可以包含一个说明,在显示里程碑的时候同时显示说明,使用-n<num>参数,显示最多<num>行里程碑的说明

bluceshang@bluceshang:~/gittemp/hello-world.git$ git tag -n1jx/v1.0         Version 1.0jx/v1.0-i18n    i18n support for v1.0jx/v1.1         Version 1.1jx/v1.2         Version 1.2: allow spaces in username.jx/v1.3         Version 1.3: Hello world speaks in Chinese now.jx/v2.0         Version 2.0jx/v2.1         Version 2.1: fixed typo.jx/v2.2         Version 2.2: allow spaces in username.jx/v2.3         Version 2.3: Hello world speaks in Chinese now.
而且还可以使用通配符对输出进行过滤,只显示名称和通配符相符的里程碑.

bluceshang@bluceshang:~/gittemp/hello-world.git$ git tag -l jx/v2*jx/v2.0jx/v2.1jx/v2.2jx/v2.3
2.命令git log

   在查看日志时使用参数--decorate可以看到提交对应的里程碑及其他引用

bluceshang@bluceshang:~/gittemp/hello-world.git$ git log --oneline --decorated901dd8 (HEAD, master) Merge pull request #1 from gotgithub/patch-196fc4d4 (refs/pull/1/head) Bugfix: build target when version.h changed.3e6070e (tag: jx/v1.0) Show version.75346b3 Hello world initialized.
3.命令git describe

   git  describe将提交显示为一个易记的名称.这个易记的名称来自于建立在该提交上的里程碑,若该提交没有里程碑则使用该提交历史版本上的里程碑并加上可题解的寻址信息.

bluceshang@bluceshang:~/gittemp/hello-world.git$ git describejx/v1.0-2-gd901dd8


创建里程碑

   创建里程碑依然是使用git tag 命令.

  1. 用法1:git  tag                                                                  <tagname>[<commit>]
  2. 用法2:git tag -a                                                              <tagname>[<commit>]
  3. 用法3:git tag -m  <msg>                                               <tagname>[<commit>]
  4. 用法4:git tag -s                                                              <tagname>[<commit>]
  5. 用法5:git tag -u <key-id>                                             <tagname>[<commit>]
注:用法1是创建轻量级里程碑.
    用法2和用法3相同,都是创建带说明的里程碑,其中用法3直接通过-m参数提供里程碑创建说明.
    用法4和用法5相同,都是带创建带GnuPG签名的里程碑.其中用法5用-u参数选择指定的私钥进行签名.
如果没有提供提交的ID,则基于头指针HEAD创建里程碑.

创建轻量级的里程碑,先创建一个空提交然后创建一个轻量级里程碑,名为mytag.省略了<commit>参数,相当于在HEAD上即最新的空提交创建里程碑.
bluceshang@bluceshang:~/gittemp/user1/hello-world$ git commit  --allow-empty -m "blank commit."[master 6dda341] blank commit.bluceshang@bluceshang:~/gittemp/user1/hello-world$ git tag mytagbluceshang@bluceshang:~/gittemp/user1/hello-world$ git tag -l my*mytag
当创建一个里程碑之后,会在版本库的.git/refs/tags目录创建一个新文件,查看这个文件,会发现是一个40位SHA1的数字.
bluceshang@bluceshang:~/gittemp/user1/hello-world$ ls .git/refs/tags/mytagbluceshang@bluceshang:~/gittemp/user1/hello-world$ cat .git/refs/tags/mytag 6dda3410ff0163505ab2ab2a9e82622fdcee0814
用git cat-file命令检查轻量级的里程碑指向的对像,轻量级里程碑实际上指向的是一个提交,查看内容会发现就是刚刚进行的提交.
bluceshang@bluceshang:~/gittemp/user1/hello-world$ git cat-file -t mytagcommitbluceshang@bluceshang:~/gittemp/user1/hello-world$ git cat-file -p mytagtree 6d736b5a3287bae0d0d7a73ab792925038023cd5parent d901dd8170f67fec607828905d5fbd91e3272400author bluceshang <1323506213@qq.com> 1386326915 +0800committer bluceshang <1323506213@qq.com> 1386326915 +0800blank commit.
轻量级里程碑的缺点:
    轻量级里程碑的创建过程没有记录,因此无法知道是谁创建的里程碑,何时创建的里程碑.在团队开发时,尽量不要采用此种偷懒的方式创建里程碑.而是采用后面2种试.还有执行git describe命令默认不使用轻量级的里程碑生成的版本描述.使用--tags参数,也可以将轻量级里程碑用途版本描述符.
bluceshang@bluceshang:~/gittemp/user1/hello-world$ git describejx/v1.0-3-g6dda341bluceshang@bluceshang:~/gittemp/user1/hello-world$ git describe --tagsmytag
带说明的里程碑.使用参数-a或-m <msg>,在创建的时候提供一个关于该里程碑的说明.
bluceshang@bluceshang:~/gittemp/user1/hello-world$ git commit --allow-empty -m "blank commit for annotated tag test."[master 55fcb1d] blank commit for annotated tag test.bluceshang@bluceshang:~/gittemp/user1/hello-world$ git tag -m "My first annotated tag." mytag2bluceshang@bluceshang:~/gittemp/user1/hello-world$ git tag -l my* -n1mytag           blank commit.mytag2          My first annotated tag.
查看该里程碑,会发现指向的不是一个提交,而是一个对象,查看该内容发现不是之前我们提交的对象的内容,而是包含了创建里程碑时的说明,以及对应的提交ID的信息
bluceshang@bluceshang:~/gittemp/user1/hello-world$ ls .git/refs/tags/mytag  mytag2bluceshang@bluceshang:~/gittemp/user1/hello-world$ git cat-file -t mytag2tagbluceshang@bluceshang:~/gittemp/user1/hello-world$ git cat-file -p mytag2object 55fcb1d81041e8de2443d74d23c0a8af12bed9bbtype committag mytag2tagger bluceshang <1323506213@qq.com> Fri Dec 6 18:57:10 2013 +0800My first annotated tag.
因此发现带说明的里程碑会在版本库中建立一个新的对象(tag对象),这个对象会记录创建里程碑的用户(tagger),创建里程碑的时间以及为什么要创建该里程碑.避免了轻量级里程碑因为匿名创建而无法追踪的缺点.
虽然mytag2本身是一个tag对象,但在很多git命令中,可以直接将其视为一个提交.
bluceshang@bluceshang:~/gittemp/user1/hello-world$ git log -1 --pretty=oneline mytag2
55fcb1d81041e8de2443d74d23c0a8af12bed9bb blank commit for annotated tag test.bluceshang@bluceshang:~/gittemp/user1/hello-world$ git rev-parse mytag228f8fc298d789308c9a6734393f03aa04a8763d8
带签名的里程碑与带说明的里程碑一样,只是在创建时候带了一个签名.为里程碑对象添加GnuPG签名.使用参数-s或-u<key-id>,还可以使用-m<msg>参数直接提供描述.(如果没有安装则需要安装GnuPG)
删除里程碑,里程碑没有类似reflog的变更记录机制,一旦删除不易恢复,慎用.
bluceshang@bluceshang:~/gittemp/user1/hello-world$ git tag -d mytagDeleted tag 'mytag' (was 6dda341)bluceshang@bluceshang:~/gittemp/user1/hello-world$ git tag -l my*mytag2
一旦发现删除了,马上补救还来得及.
bluceshang@bluceshang:~/gittemp/user1/hello-world$ git tag mytag 6dda341bluceshang@bluceshang:~/gittemp/user1/hello-world$ git tag -l my*mytagmytag2
不要阴间更改里程碑,如果需要修改,可以用同样的里程碑名称重新建立,不过需要加-f或--force参数强制覆盖已有的里程碑.更改里程碑要慎用,一个原因是里程碑从概念上讲是对历史提交的标记,不应该随意发动.另一个原因是一旦被他人同步,如果修改里程碑已经同步该里程碑的用户并不会自动更新.

共享里程碑
     创建的里程碑默认只在本地版本库中可见,不会因为对分支的推送而将里程碑也推送到远程版本库.
1.显示推送以共享里程碑.需要在git push命令中明确表示出来.
bluceshang@bluceshang:~/gittemp/user1/hello-world$ git ls-remote origin my*bluceshang@bluceshang:~/gittemp/user1/hello-world$ git push origin mytagTotal 0 (delta 0), reused 0 (delta 0)To file:///home/bluceshang/gittemp/hello-world.git * [new tag]         mytag -> mytagbluceshang@bluceshang:~/gittemp/user1/hello-world$ git ls-remote origin my*6dda3410ff0163505ab2ab2a9e82622fdcee0814refs/tags/mytag
用户用git pull的时候会自动将远程版本库中tag拉到本地.里程碑可以被强制更新,但是当里程碑被修改后,已经获取到的里程碑的版本库再次使用拉操作时,不会自动更新.必须加上里程碑
git pull origin refs/tags/mytag2:refs/tags/mytag2

删除里程碑时,可以直接在本地命令删除远程版本库中的里程碑.
git push <remote-url> :<tagname>


原创粉丝点击