前言
在git代码管理时,有时候我们想对某个特定的commit 添加标记,比如要标识版本信息,这时候就可以用的git中的打标签功能。打tag就类似于我们看书放书签一样,以后可以直接用tag找到提交的位置,不然的话,就只有看commit的哈希值返回指定位置,比较繁琐。
创建标签
git tag -a v20230605 -m '618活动发布版本'
-a 创建指令 后面是标签名称
-m 添加备注 后面是备注内容
查看标签
git tag
V20230605
删除标签
git tag -d v20230605
-d 删除指令 后面是标签名称
打标签
如果直接使用 git tag -a v20230605 -m '618活动发布版本' 打标签,绑定的是最近commit的版本。如果你想给某个指定的commit版本打标签,则使用:
git tag -a v20230605 55d8e71fc7d0b8cefbb4cbee339beb9d987d9b81 -m '618活动发布版本'
55d8e71fc7d0b8cefbb4cbee339beb9d987d9b81为commit的版本号
标签同步到远程服务器
同提交代码后,使用git push来推送到远程服务器一样,tag也需要进行推送才能到远端服务器。使用git push origin v20230605
推送本地所有tag
git push origin --tags
查看远程服务器标签
git ls-remote --tags
检出标签
我们可以根据标签,检出相应版本的代码
git checkout v20230605
根据标签v20230605之后会有如下提示
$ git checkout v20230605
Note: switching to 'v20230605'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at 070153f [修改代码]测试打标签
大致意思就是使用 git checkout 命令检出历史提交版本会使当前的代码库处于"detached HEAD"状态下,此状态下对代码的修改不能被保存。若需要修改建议使用git switch -c命令创建新分支,在新分支基础上修改再提交。