赞
踩
标签可以针对某一时间点的版本做标记,常用于版本发布。
$ git tag # 在控制台打印出当前仓库的所有标签
git标签分为两种类型:轻量标签和附注标签。轻量标签是指向提交对象的引用,附注标签则是仓库中的一个独立对象。建议使用附注标签。
# 创建轻量标签
$ git tag v0.1.2-light
# 创建附注标签
$ git tag -a v0.1.2 -m “0.1.2版本”
创建轻量标签不需要传递参数,直接指定标签名称即可。
创建附注标签时,参数a即annotated的缩写,指定标签类型,后附标签名。参数m指定标签说明,说明信息会保存在标签对象中。
与切换分支命令相同,用git checkout [tagname]
查看标签信息
用git show
命令可以查看标签的版本信息:
$ git show v0.1.2
误打或需要修改标签时,需要先将标签删除,再打新标签。
$ git tag -d v0.1.2 # 删除标签
参数d即delete的缩写,意为删除其后指定的标签。
-----------------------------
如果标签打错了,也可以删除:
- $ git tag -d v0.1
- Deleted tag 'v0.1' (was e078af9)
因为创建的标签都只存储在本地,不会自动推送到远程。所以,打错的标签可以在本地安全删除。
如果要推送某个标签到远程,使用命令git push origin <tagname>
:
- $ git push origin v1.0
- Total 0 (delta 0), reused 0 (delta 0)
- To git@github.com:michaelliao/learngit.git
- * [new tag] v1.0 -> v1.0
或者,一次性推送全部尚未推送到远程的本地标签:
- $ git push origin --tags
- Counting objects: 1, done.
- Writing objects: 100% (1/1), 554 bytes, done.
- Total 1 (delta 0), reused 0 (delta 0)
- To git@github.com:michaelliao/learngit.git
- * [new tag] v0.2 -> v0.2
- * [new tag] v0.9 -> v0.9
如果标签已经推送到远程,要删除远程标签就麻烦一点,先从本地删除:
- $ git tag -d v0.9
- Deleted tag 'v0.9' (was 6224937)
然后,从远程删除。删除命令也是push,但是格式如下:
- $ git push origin :refs/tags/v0.9
- To git@github.com:michaelliao/learngit.git
- - [deleted] v0.9
要看看是否真的从远程库删除了标签,可以登陆GitHub查看。
打标签不必要在head之上,也可在之前的版本上打,这需要你知道某个提交对象的校验和(通过git log
获取)。
# 补打标签
$ git tag -a v0.1.1 9fbc3d0
------------------------------
默认标签是打在最新提交的commit上的。有时候,如果忘了打标签,比如,现在已经是周五了,但应该在周一打的标签没有打,怎么办?
方法是找到历史提交的commit id,然后打上就可以了:
- $ git log --pretty=oneline --abbrev-commit
- 6a5819e merged bug fix 101
- cc17032 fix bug 101
- 7825a50 merge with no-ff
- 6224937 add merge
- 59bc1cb conflict fixed
- 400b400 & simple
- 75a857c AND simple
- fec145a branch test
- d17efd8 remove test.txt
- ...
比方说要对add merge
这次提交打标签,它对应的commit id是6224937
,敲入命令:
$ git tag v0.9 6224937
再用命令git tag
查看标签:
- $ git tag
- v0.9
- v1.0
注意,标签不是按时间顺序列出,而是按字母排序的。可以用git show <tagname>
查看标签信息:
- $ git show v0.9
- commit 622493706ab447b6bb37e4e2a2f276a20fed2ab4
- Author: Michael Liao <askxuefeng@gmail.com>
- Date: Thu Aug 22 11:22:08 2013 +0800
-
- add merge
- ...
可以看到,v0.9
确实打在add merge
这次提交上。
通常的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标签时的代码状态,(但现在处于一个空的分支上)
你处于“脱离头”状态。 你可以环顾四周,做实验更改并提交它们,并且您可以放弃您在此提交的任何提交通过执行另一个结账而不影响任何分支。
如果你想创建一个新的分支来保留你创建的提交,你可以
(现在或以后)再次使用-b和checkout命令。 Example: git checkout -b <new-branch-name>
从当前的develop分支,切换到201712130927develop这个tag,现在处于一个空分支,
git checkout -b CurrentTimeBus201712130927 创建一个新分支CurrentTimeBus201712130927,这个新分支就是打201712130927develop标签时的代码状态。
usage: git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] <tagname> [<head>]
or: git tag -d <tagname>...
or: git tag -l [-n[<num>]] [--contains <commit>] [--no-contains <commit>] [--points-at <object>]
[--format=<format>] [--[no-]merged [<commit>]] [<pattern>...]
or: git tag -v [--format=<format>] <tagname>...
-l, --list list tag names
-n[<n>] print <n> lines of each tag message
-d, --delete delete tags
-v, --verify verify tags
Tag creation options
-a, --annotate annotated tag, needs a message
-m, --message <message>
tag message
-F, --file <file> read message from file
-s, --sign annotated and GPG-signed tag
--cleanup <mode> how to strip spaces and #comments from message
-u, --local-user <key-id>
use another key to sign the tag
-f, --force replace the tag if exists
--create-reflog create a reflog
Tag listing options
--column[=<style>] show tag list in columns
--contains <commit> print only tags that contain the commit
--no-contains <commit>
print only tags that don't contain the commit
--merged <commit> print only tags that are merged
--no-merged <commit> print only tags that are not merged
--sort <key> field name to sort on
--points-at <object> print only tags of the object
--format <format> format to use for the output
-i, --ignore-case sorting and filtering are case insensitive
/
原文:http://blog.csdn.net/wangjia55/article/details/8793577/
原文:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/001376951758572072ce1dc172b4178b910d31bc7521ee4000
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。