Git如何checkout远程tag

拉取项目

执行命令git clone:

git clone git@github.com:secbr/nacos.git

查看远程tag

执行命令git tag:

appledeMacBook-Pro-2:nacos apple$ git tag
0.2.1
0.2.1-RC1
0.3.0
0.3.0-RC1
0.4.0
...

此时可找到需要拉取的tag名称。

checkout需要的tag

执行命令git checkout:

(base) appledeMacBook-Pro-2:nacos apple$ git checkout 2.0.2
Note: switching to '2.0.2'.

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 1fac5c833 Merge pull request #6052 from alibaba/develop

其中2.0.2为tag(分支)名称。

通过git branch命令可以查看当前的分支情况:

(base) appledeMacBook-Pro-2:nacos apple$ git branch
* (HEAD detached at 2.0.2)
  develop

通过此种方式,获得的分支Head处于游离状态,我们可以很方便地在历史版本之间互相切换,比如需要回到某次提交,直接checkout对应的 commit id或者tag名即可。

但在这个基础上的提交会新开一个匿名分支!也就是说我们的提交是无法可见保存的,一旦切到别的分支,游离状态以后的提交就不可追溯了。

解决办法就是新建一个分支保存游离状态后的提交。

checkout作为一个分支

执行git checkout -b tagName (将tag checkout出去作为一个branch):

(base) appledeMacBook-Pro-2:nacos apple$ git checkout -b tag-2.0.2
Switched to a new branch 'tag-2.0.2'
(base) appledeMacBook-Pro-2:nacos apple$ git branch
  develop
* tag-2.0.2
(base) appledeMacBook-Pro-2:nacos apple$ git checkout -b tag-2.0.2
Switched to a new branch 'tag-2.0.2'
(base) appledeMacBook-Pro-2:nacos apple$ git branch
  develop
* tag-2.0.2

在游离状态下的tag中执行git checkout -b tag-2.0.2来新建一个分支。

当然上述checkout tag和checkout tag作为一个分支,可以合并成一个命令:

(base) appledeMacBook-Pro-2:nacos apple$ git checkout -b tag-1.4.2 1.4.2
Switched to a new branch 'tag-1.4.2'

上述命令,将远程版本为1.4.2的tag,新建一个本地分支,名称为tag-1.4.2。

添加远程仓库

(base) appledeMacBook-Pro-2:nacos apple$ git remote add tag-2.0.2 git@github.com:secbr/nacos.git

push并设置upstream

(base) appledeMacBook-Pro-2:nacos apple$ git push
fatal: The current branch tag-2.0.2 has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin tag-2.0.2

(base) appledeMacBook-Pro-2:nacos apple$ git push --set-upstream origin tag-2.0.2
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for 'tag-2.0.2' on GitHub by visiting:
remote:      https://github.com/secbr/nacos/pull/new/tag-2.0.2
remote:
To github.com:secbr/nacos.git
 * [new branch]          tag-2.0.2 -> tag-2.0.2
Branch 'tag-2.0.2' set up to track remote branch 'tag-2.0.2' from 'origin'.

此时已经将本地新建的分支作为一个新分支提交到代码库。

博主简介:《SpringBoot技术内幕》技术图书作者,酷爱钻研技术,写技术干货文章。

公众号:「程序新视界」,博主的公众号,欢迎关注~

技术交流:请联系博主微信号:zhuan2quan


程序新视界

公众号“ 程序新视界”,一个让你软实力、硬技术同步提升的平台,提供海量资料

微信公众号:程序新视界

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序新视界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值