目录
标签管理
在版本管理中,我们想要退回一个历史版本,要使用 git reset --hard <版本编号/^HEAD> 指令,使用指针^HEAD太麻烦,使用版本编号版本编号不容易记住,有没有好点的办法,更容易对各个版本的管理呢,办法当然有,那就是使用标签,创建标签很简单,使用git tag 命令就可以:
$ git branch
* master
$ git tag v1.0
这样就在master分支上创建了一个标签,标签名字v1.0,通过git tag可以查看创建的所有标签
$ git tag
v1.0
通过git log --pretty=oneline --abbrev-commit命令可以查看所有的历史版本。
$ git log --pretty=oneline --abbrev-commit
18233d5 (HEAD -> master, tag: v1.0) test file add new
561ca86 (origin/master, origin/HEAD) add file test.txt
b0e2922 add file git1/git1.log
e1c4305 add dir git1
50bbda8 rm test.txt
d6b3450 add file test.txt
09dd2d4 aaaaaa
9ba73aa dddd
bf80d0d add midfi readme.txt
4757dea modif 'readme.txt' ,add file LICENSE
26d2b1f {readme.txt}
b88de02 {readme.txt} {add file if_for}
f63d6c1 add file hello.py
8ff01a6 add fun.py
457f80c append GPL
70e2010 wrote a readme file
现在我们看看打标签有什么好处,我们先回到以前的历史版本561ca86
$ git reset --hard 561ca86
HEAD is now at 561ca86 add file test.txt
现在看看历史版本,已经回到了版本561ca86
$ git log --pretty=oneline --abbrev-commit
561ca86 (HEAD -> master, origin/master, origin/HEAD) add file test.txt
b0e2922 add file git1/git1.log
e1c4305 add dir git1
50bbda8 rm test.txt
d6b3450 add file test.txt
09dd2d4 aaaaaa
9ba73aa dddd
bf80d0d add midfi readme.txt
4757dea modif 'readme.txt' ,add file LICENSE
26d2b1f {readme.txt}
b88de02 {readme.txt} {add file if_for}
f63d6c1 add file hello.py
8ff01a6 add fun.py
457f80c append GPL
70e2010 wrote a readme file
现在要想回到v1.0版本,就不用在记住原来的commit编号了,只要急着标签就行了:
$ git reset --hard v1.0
HEAD is now at 18233d5 test file add new
$ git log --pretty=oneline --abbrev-commit
18233d5 (HEAD -> master, tag: v1.0) test file add new
561ca86 (origin/master, origin/HEAD) add file test.txt
b0e2922 add file git1/git1.log
e1c4305 add dir git1
50bbda8 rm test.txt
d6b3450 add file test.txt
09dd2d4 aaaaaa
9ba73aa dddd
bf80d0d add midfi readme.txt
4757dea modif 'readme.txt' ,add file LICENSE
26d2b1f {readme.txt}
b88de02 {readme.txt} {add file if_for}
f63d6c1 add file hello.py
8ff01a6 add fun.py
457f80c append GPL
70e2010 wrote a readme file
版本又回到了v1.0,是不是很方便,这样只要给每个历史版本打上标签,就可以不用记commit编号就可以对各个历史版本进行方便的管理。给历史版本打上标签的方法是使用命令git tag <tag_name> <commit_id>:
$ git tag v0.9 561ca86
$ git log --pretty=oneline --abbrev-commit
18233d5 (HEAD -> master, tag: v1.0) test file add new
561ca86 (tag: v0.9, origin/master, origin/HEAD) add file test.txt
b0e2922 add file git1/git1.log
e1c4305 add dir git1
50bbda8 rm test.txt
d6b3450 add file test.txt
09dd2d4 aaaaaa
9ba73aa dddd
bf80d0d add midfi readme.txt
4757dea modif 'readme.txt' ,add file LICENSE
26d2b1f {readme.txt}
b88de02 {readme.txt} {add file if_for}
f63d6c1 add file hello.py
8ff01a6 add fun.py
457f80c append GPL
70e2010 wrote a readme file
这样就给版本561ca86版本加上标v0.9。
还可以创建带说明的标签,使用命令 git tag -a <tag_name> -m <“说明文字”> <commit_id>。
$ git tag -a v0.1 -m "this is version v0.1" 70e2010
$ git log --pretty=oneline --abbrev-commit
18233d5 (HEAD -> master, tag: v1.0) test file add new
561ca86 (tag: v0.9, origin/master, origin/HEAD) add file test.txt
b0e2922 add file git1/git1.log
e1c4305 add dir git1
50bbda8 rm test.txt
d6b3450 add file test.txt
09dd2d4 aaaaaa
9ba73aa dddd
bf80d0d add midfi readme.txt
4757dea modif 'readme.txt' ,add file LICENSE
26d2b1f {readme.txt}
b88de02 {readme.txt} {add file if_for}
f63d6c1 add file hello.py
8ff01a6 add fun.py
457f80c append GPL
70e2010 (tag: v0.1) wrote a readme file
使用git show <tag_name>可以看到文字说明
$ git show v0.1
tag v0.1
Tagger: hyd <hydfreeman@163.com>
Date: Sat Mar 23 19:21:01 2019 +0800
this is version v0.1
commit 70e2010d0a2b1ccd9d7734d5c2219f770c219588 (tag: v0.1)
Author: hyd <hydfreeman@163.com>
Date: Wed Mar 6 14:38:49 2019 +0800
wrote a readme file
diff --git a/readme.txt b/readme.txt
new file mode 100644
index 0000000..4db76c8
--- /dev/null
+++ b/readme.txt
@@ -0,0 +1,2 @@
+Git is a version control system.
+Git is a free software.
\ No newline at end of file
现在我创建了一个标签,突然发现创建错了,怎么办,这时可以删除比如我们将v0.9标签删除:
$ git tag -d v0.9
Deleted tag 'v0.9' (was 561ca86)
再用git tag查看,发现v0.9标签不见了,但是版本561ca86还在,可见只是删除了标签,版本没有删除
$ git tag
v0.1
v1.0
$ git log --pretty=oneline --abbrev-commit
18233d5 (HEAD -> master, tag: v1.0) test file add new
561ca86 (origin/master, origin/HEAD) add file test.txt
b0e2922 add file git1/git1.log
e1c4305 add dir git1
50bbda8 rm test.txt
d6b3450 add file test.txt
09dd2d4 aaaaaa
9ba73aa dddd
bf80d0d add midfi readme.txt
4757dea modif 'readme.txt' ,add file LICENSE
26d2b1f {readme.txt}
b88de02 {readme.txt} {add file if_for}
f63d6c1 add file hello.py
8ff01a6 add fun.py
457f80c append GPL
70e2010 (tag: v0.1) wrote a readme file
因为创建的标签都只存储在本地,不会自动推送到远程。所以,打错的标签可以在本地安全删除。如果要推送某个标签到远程,使用命令git push origin :
$ git push origin v1.0
Warning: Permanently added the RSA host key for IP address '52.74.223.119' to the list of known hosts.
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 257 bytes | 128.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:hyd861001/my_python.git
* [new tag] v1.0 -> v1.0
或者一次性推送所有尚未推送的本地标签
$ git push origin --tags
Counting objects: 1, done.
Writing objects: 100% (1/1), 160 bytes | 160.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To github.com:hyd861001/my_python.git
* [new tag] v0.1 -> v0.1
如果要删除远程标签,就要首先删除本地标签:
$ git tag -d v0.1
Deleted tag 'v0.1' (was 3ecb4b0)
然后再远程删除,删除命令也是使用push:
$ git push origin :refs/tags/v0.1
To github.com:hyd861001/my_python.git
- [deleted] v0.1