这个命令,其实很有用,类似clearcase中的label,就是给一个版本设置一个标记(标签),方便后期查找特定的版本。

tag和commit的sha1那串字符串的关系,不是很大,但是还是要说一下的。

1. 每一次修改,在本地仓库中,只有commit了,在ref中有新的相关的记录,才可以做push到远端。否则会提示你Everything is up-to-date.

 [root@CloudGame mueas]# ll
total
-rw-r--r-- root root Jan : pom.xml
drwxr-xr-x root root Jan : src
[root@CloudGame mueas]# git status        #git status查看当前是否有新的修改发生,结果显示没有修改。
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
[root@CloudGame mueas]# git push         #push一个干净的工作目录,你会看到什么都是最新的,意思是没有必要push
Everything up-to-date

2. 没有commit得到的那个sha1的字符串,是不允许给执行git tag加标签的。否则,你会遇到错误!

 [root@CloudGame hello]# ll -al                #查看当前本地repo中的内容,显示什么都没有
total
drwxr-xr-x root root Jan : .
drwxr-xr-x. shihuc shihuc Jan : ..
drwxr-xr-x root root Jan : .git
[root@CloudGame hello]# git status              #查看当前repo的状态信息。也是说没有东西commit
On branch master Initial commit nothing to commit (create/copy files and use "git add" to track)
[root@CloudGame hello]# git tag ht001            #创建轻量级标签ht001,提示错误
fatal: Failed to resolve 'HEAD' as a valid ref.
[root@CloudGame hello]# git tag -a ht001 -m "first one tag for initial repo"    #创建注解标签,同样报错
fatal: Failed to resolve 'HEAD' as a valid ref.

根据这个提示信息,很容易知道原因,就是没有东西commit,所以不让做tag。只有commit了,才可以打标签。我们可以看看下面:

 [root@CloudGame hello]# touch README.txt      #创建一个文件
[root@CloudGame hello]# git status          #查看状态信息。显示有意个没有被跟踪的文件
On branch master Initial commit Untracked files:
(use "git add <file>..." to include in what will be committed) README.txt nothing added to commit but untracked files present (use "git add" to track)
[root@CloudGame hello]# git add README.txt               #添加README.txt到本地repo
[root@CloudGame hello]# git commit -m "README is created"      #commit这个文件
[master (root-commit) d4e04d6] README is created
file changed, insertions(+), deletions(-)
create mode README.txt
[root@CloudGame hello]# git status                    #再次查看当前repo的信息,显示当前没有东西可以commit。工作目录是干净的。
On branch master
Your branch is based on 'origin/master', but the upstream is gone.
(use "git branch --unset-upstream" to fixup)
nothing to commit, working directory clean
[root@CloudGame hello]# vim .git/refs/heads/master           
[root@CloudGame hello]# cat .git/refs/heads/master           #由于当前在master分支,所以查看这个分支文件的内容,显示为当前的commit的sha1字符串
d4e04d68c5aa5b316167a1c3baa63faabb06cf80
[root@CloudGame hello]#
[root@CloudGame hello]# git log
commit d4e04d68c5aa5b316167a1c3baa63faabb06cf80
Author: chengsh <shihu.cheng@000000.com>
Date: Thu Jan :: + README is created

最后,在有了这个commit的内容后,先检查下.git/refs/tags/目录下有没有内容!从下面的日志看,是什么都没有的,空的。

 [root@CloudGame hello]# ll .git/refs/
total
drwxr-xr-x root root Jan : heads
drwxr-xr-x root root Jan : tags
[root@CloudGame hello]# ll .git/refs/tags/
total

那么,这个时候添加tag,会是什么样子呢?往下看:

 [root@CloudGame hello]# git tag -a ht001 -m "first tag"    #添加一个注解类型的标签,添加基本类型(轻量级)的也同样可以。
[root@CloudGame hello]#
[root@CloudGame hello]# ll .git/refs/tags/            #查看tags目录下有没有东西,显示有一个标签命名的文件  
total
-rw-r--r-- root root Jan : ht001
[root@CloudGame hello]#
[root@CloudGame hello]# cat .git/refs/tags/ht001         #查看这个标签文件内容是什么,一串字符
6e032b380494c7a230686a00533ea4ad1ab2961a

到此,可以看出tag和commit的关系了吧。要想tag,必须先有commit。clearcase当中,可以给一个节点添加多个label,那git里面能否给一个commit号(sha1字符串)添加多个标签呢?我们往下看:

 [root@CloudGame hello]# git tag ht002
[root@CloudGame hello]#
[root@CloudGame hello]# ll -al .git/refs/tags/
total
drwxr-xr-x root root Jan : .
drwxr-xr-x root root Jan : ..
-rw-r--r-- root root Jan : ht001
-rw-r--r-- root root Jan : ht002
[root@CloudGame hello]# cat .git/refs/tags/ht002
d4e04d68c5aa5b316167a1c3baa63faabb06cf80
[root@CloudGame hello]#

上面的操作日志显示,给同一个commit后的操作,添加多个tag是可以的,每次的tag标签对应的sha1字符串不同。

下面看看,操作tag后,gitk显示的内容是什么?

再来看看ht002标签的信息对应的内容:

从上图,可以发现,基本类型的tag对应的信息,那个sha1串头是tree字样。而注解类型的tag中sha1串头对应的字样是object。这也是一个小区别。

下面,列举一下tag常见的操作指令:

切换到标签 git checkout <tagname>
查看标签的版本信息 git show <tagname>
删除标签 git tag -d <tagname>
给指定的commit打标签 git tag -a <tagname> [-m <your comment>] <commit-sha1-id>
发布指定的标签到远程仓库 git push origin <tagname>
将本地的标签全部发布到远程仓库 git push origin --tags
查看当前分支下的所有标签信息 git tag
删除远程tag  

git tag -d <tagname>;

git push origin :<tagname>

最后补充说明一下,通常git push的时候,标签信息是不会被一起发布到远程仓库的。删除远程分支和标签类似,都是先删除本地的分支或则标签,然后push这个分支或则标签到远程仓库。都要注意的一点就是origin与冒号之间有一个空格。否则会出错的。

git tag知多少的更多相关文章

  1. git必知必会

    1. Git 配置 --system #系统级别--global #用户全局--local #单独一个项目 git config --global user.name "xxxx" ...

  2. repo 导出本地 git tag 给他人

    背景 使用 repo 管理了多个 git 仓库,有时需要将本地仓库的tag同步给其他人,但又不能直接推到远程(例如权限问题). 实际场景举例 本地复现了一个问题,需要让其他人回退到相同环境来排查. 本 ...

  3. git tag使用标记

    git跟其它版本控制系统一样,可以打标签(tag), 作用是标记一个点为一个版本号,如0.1.3, v0.1.7, ver_0.1.3.在程序开发到一个阶段后,我们需要打个标签,发布一个版本,标记的作 ...

  4. Git tag push 到远端仓库

    很早之前,我们就提到过用Git tag来给工程打上标签,但是这个命令只是在本地仓库打标签而已, 为了能把标签同步到远程服务器,我们可以这样做: 默认情况下,git push并不会把tag标签传送到远端 ...

  5. git tag — 标签相关操作

    标签可以针对某一时间点的版本做标记,常用于版本发布. 列出标签 $ Git tag # 在控制台打印出当前仓库的所有标签$ git tag -l 'v0.1.*' # 搜索符合模式的标签 打标签 gi ...

  6. Git tag 给当前分支打标签

    原文已经找不到出处,重新整理格式,仅作个人收藏! 标签(Tag)可以针对某一时间点的版本做标记,常用于版本发布. 列出tag $ git tag # 在控制台打印出当前仓库的所有tag $ git t ...

  7. git命令之git tag 给当前分支打标签

    git tag - 标签相关操作 发表于 2011年06月29日 由 机器猫 标签可以针对某一时间点的版本做标记,常用于版本发布. 列出标签 $ git tag # 在控制台打印出当前仓库的所有标签$ ...

  8. git tag查看、创建与删除

    tag管理 查看tag 太多的话用 grep 过滤 $ git tag | grep dev_20150525 # 创建tag $ git tag -a dev_20150525_16 -m 'xxx ...

  9. git tag的使用

    查看所有的标签git tag 删除某一个标签git tag -d tagName 创建带注释的标签 git tag -a tagName -m "annotate" 轻量级标签 g ...

随机推荐

  1. 第二个Sprint冲刺第七天

    讨论地点:宿舍 讨论成员:邵家文.李新.朱浩龙.陈俊金 讨论:整理已完成的功能

  2. stdobj to array php

    The lazy one-liner method You can do this in a one liner using the JSON methods if you're willing to ...

  3. 进程process和线程thread的关系

    写的很好很明白cpu每次只能执行一个进程,所以其他进程会挂起 在一个进程中,允许存在n个线程,n个线程共享这个进程中的资源 多个线程在共享的时候存在资源互斥,一次只能一个线程,会需要加锁 一次存在固定 ...

  4. What are the 10 algorithms one must know in order to solve most algorithm challenges/puzzles?

    QUESTION : What are the 10 algorithms one must know in order to solve most algorithm challenges/puzz ...

  5. RViz 实时观测机器人建立导航2D封闭空间地图过程 (SLAM) ----27

    原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/ ROS提供了非常强大的图形化模拟环境 RViz,这个 RViz 能做的事情非常多.今天我们学习一下如何使 ...

  6. python--迭代--7

    原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 一.什么是迭代 在Python中,如果给定一个list或tuple,我们可以通过for循环来遍历 ...

  7. c#部分---用结构体的题目- //请输入班级人数,输入每个人的学号,姓名,和语文分数、数学分数和英语分数(要求使用结构体)

    //请输入班级人数,输入每个人的学号,姓名,和语文分数.数学分数和英语分数(要求使用结构体), //求班级里两个语文分数是最高分的学生的所有信息:数学分数是最高分的两个学生的所有信息:英语平均分 建立 ...

  8. Apache运行模式

    Apache 2.X 支持插入式并行处理模块,称为多路处理模块(MPM: Multi-Processing Modules).在编译apache时必须选择也只能选择一个MPM,对类UNIX系统,有几个 ...

  9. psp0

    周活动总结表 姓名:苗堃                                                                                         ...

  10. JAG Practice Contest for ACM-ICPC Asia Regional 2016

    2016弱校联盟十一专场10.3 传送门 B. Help the Princess! 计算皇后和士兵谁先到达出口即可. C. We don't wanna work! 两个优先队列维护工作中积极性最小 ...