Git如何实现checkout远程tag(git如何撤回add)硬核推荐

随心笔谈3个月前更新 admin
208 00
🌐 经济型:买域名、轻量云服务器、用途:游戏 网站等 《腾讯云》特点:特价机便宜 适合初学者用 点我优惠购买
🚀 拓展型:买域名、轻量云服务器、用途:游戏 网站等 《阿里云》特点:中档服务器便宜 域名备案事多 点我优惠购买
🛡️ 稳定型:买域名、轻量云服务器、用途:游戏 网站等 《西部数码》 特点:比上两家略贵但是稳定性超好事也少 点我优惠购买

文章摘要

这篇文章介绍了如何在本地克隆远程仓库并将其远程分支设置为上游,以便将本地远程仓库的tag checkout到本地分支并推送给远程仓库。以下是总结: 1. **克隆远程仓库**:用户使用`git clone`命令克隆远程仓库,例如`git clone git@github.com:secbr/nacos.git`。 2. **创建本地分支**:用户使用`git checkout -b tagName`创建本地分支作为tag checkout。例如,创建分支`tag-2.0.2`并切换到该分支。 3. **处理tag**:用户列出本地分支以找到需要拉取的tag名称,并使用`git checkout`切换到特定tag。 4. **设置远程分支**:用户将本地分支设置为远程仓库的上游,使用`git push --set-upstream origin tagName`,以便远程仓库可以追踪本地分支。 5. **提交和push**:用户提交更改,并使用`git push`将本地分支推送给远程仓库。如果不设置上游,会提示当前分支没有 upstream。 总结:文章指导如何管理本地远程仓库的分支,确保本地提交的变更可以推送给远程仓库,解决分支切换和push问题。



目录拉取项目查看远程tagcheckout需要的tagcheckout作为一个分支添加远程仓库push并设置upstream

执行命令git clone:

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

执行命令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名称。

执行命令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名即可。

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

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

执行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
(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’.

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。 

您可能感兴趣的文章:Git 常用命令清单(整理且详细)git分支(branch)操作相关命令及分支命令的使用Git科普文,Git基本原理及各种骚操作(推荐)

© 版权声明

相关文章