Git 处理tag和branch的命令

本文介绍了如何在GitHub上为项目设置Tag的方法,包括使用命令行创建、查看和删除Tag,以及如何将Tag推送到远程仓库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近想给GitHub 上的项目设置tag,可是使用GitHub Desktop,找了一圈都没找到快速设置Tag 的地方,最后只能通过终端命令来添加了。
想要查看Git 的命令,可以使用

git --help

可是大致看一下git的命令:

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone      Clone a repository into a new directory
   init       Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add        Add file contents to the index
   mv         Move or rename a file, a directory, or a symlink
   reset      Reset current HEAD to the specified state
   rm         Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
   bisect     Use binary search to find the commit that introduced a bug
   grep       Print lines matching a pattern
   log        Show commit logs
   show       Show various types of objects
   status     Show the working tree status

grow, mark and tweak your common history
   branch     List, create, or delete branches
   checkout   Switch branches or restore working tree files
   commit     Record changes to the repository
   diff       Show changes between commits, commit and working tree, etc
   merge      Join two or more development histories together
   rebase     Forward-port local commits to the updated upstream head
   tag        Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch      Download objects and refs from another repository
   pull       Fetch from and integrate with another repository or a local branch
   push       Update remote refs along with associated objects

然后使用 git help -agit help -g 可以查看git 的一些子命令和一些概念性的东西。

使用 git help <command>git help <concept> 可以查看某个命令的参数 和描述等详细信息。

要对某个Git 工程做处理,我们首先要先进入该工程的根目录。
可以使用 cd 工程路径 来进入某个工程目录。
然后可以使用 ls -a 或者 ls -al 查看工程目录下是否有隐藏的 .git 文件。
例如我的操作:

bogon:HLBluetoothDemo harvey$ ls -al
total 32
drwxr-xr-x   7 harvey  staff   238 11 18 17:41 .
drwxr-xr-x  63 harvey  staff  2142 11  1 11:03 ..
-rw-r--r--@  1 harvey  staff  6148  9 28 15:53 .DS_Store
drwxr-xr-x  16 harvey  staff   544 11 18 17:41 .git
drwxr-xr-x  23 harvey  staff   782 11 18 17:41 HLBluetoothDemo
drwxr-xr-x   5 harvey  staff   170 11 18 17:41 HLBluetoothDemo.xcodeproj
-rw-r--r--   1 harvey  staff  5066 11 18 17:41 README.md

Commit 操作

//查看所有提交记录
git log

// 查看简写的提交记录
git log --oneline

Tag 操作

我们可以对某一个稳定的版本设置一个Tag,这样在以后回头来看,也更方便和直接。

查看Tag 命令

// 查看本地的所有Tag
git tag
// 查看某一系列tag
git tag -l v1.*

创建Tag 命令

git tag -a v1.0 -m "对Tag的描述信息"

我们也可以对以前的某次commit 设置tag。
具体的做法是,首先执行 git log --oneline ,找到这次提交的唯一id。
例如:

bogon:HLBluetoothDemo harvey$ git log --oneline
2b04b38 利用iOS 9后的系统API获取特性可写入的最大长度来分割发送。
1468bbe Update README.md
fba0db7 断开连接时,将connectedperpwheral 置为nil
ee76062 Update README.md
576e179 Update README.md

上面的 描述信息前的值,就是id。

然后执行 git tag -a v1.1 2b04b38 -m "Tag的描述信息"

我们可以在本地添加多个tag 后,执行以下 git tag 查看一下本地的tag。

提交本地的Tag 到远程服务器的命令

git push origin --tags

然后,我们就可以去github 上看自己的工程里的tags 了。

删除本地的tag 命令

git tag -d v1.0

上面这个命令只能删除本地的tag,如果这个tag 已经提交到远程服务器之后,上面的命令并不能删除远程服务器上的tag。
要删除远程服务器上的tag,可以使用如下的命令:

git push origin --delete tag v1.0

branch 操作

查看branch 的命令

git branch -a

在终端里,远程的branch会显示为红色,如下图所示:

这里写图片描述

删除远程branch 的命令

git push origin --delete <branchName>

目前branch 就用到这么多,更详细的,使用git help branch 查看吧。
以后再补充了。

转载于:https://www.cnblogs.com/wanghang/p/6298811.html

<think>好的,用户想在GitHub上使用特定分支标签进行操作,我需要根据提供的引用内容来组织回答。首先,我需要明确用户的需求,他们可能是在版本控制中需要切换分支、创建标签,或者管理远程仓库的分支标签。 引用中提到了branchtag的基本操作命令,比如如何创建、查看、删除tag,以及分支的克隆、检出、提交等。用户可能希望了解如何在GitHub上执行这些操作,而不仅仅是本地的Git命令。需要将本地操作与GitHub的远程仓库操作结合起来。 首先,我应该分步骤说明分支标签的操作,包括创建、切换、推送、查看删除。例如,对于分支,用户可能需要知道如何创建本地分支并推送到远程,或者如何检出远程分支。对于标签,需要说明如何创建带注释的标签,并推送到GitHub,以及如何删除远程标签。 需要注意用户是在GitHub环境下操作,所以可能需要强调远程仓库相关的命令,比如git push origin branch_name或git push origin tag_name。同时,引用中的例子有提到检出远程分支的命令,比如git checkout -b branch_name,这需要解释清楚。 还要考虑用户可能的常见使用场景,比如版本发布时打标签,或者在不同分支上开发新功能。可能需要给出具体的示例命令,比如创建版本标签v1.0并推送到GitHub,或者如何切换到已有的远程分支进行开发。 另外,用户可能会混淆分支标签的区别,需要简单说明两者的用途。标签用于标记特定提交,不可变,而分支用于开发流程,会随着提交前进。 最后,生成相关问题时要围绕用户可能进一步想知道的内容,比如如何比较标签之间的差异,或者如何处理分支合并冲突等。确保问题覆盖用户可能遇到的后续疑问。 总结回答结构:分branchtag两部分,每部分包含常用操作,结合GitHub的远程操作,给出具体命令示例,并强调关键注意事项,如推送标签到远程的步骤。同时,参考引用中的命令确保准确性。</think>### GitHub 上使用特定分支标签的 Git 操作指南 #### 一、分支(Branch)操作 1. **查看分支** - 查看本地分支:`git branch` - 查看远程分支:`git branch -r` - 查看所有分支(本地+远程):`git branch -a` 2. **创建并切换分支** ```bash git checkout -b <branch_name> # 创建本地分支并切换 git push origin <branch_name> # 推送分支到 GitHub ``` 3. **检出远程分支** ```bash git fetch origin # 同步远程分支信息 git checkout origin/<branch_name> # 检出远程分支(只读) git checkout -b <local_branch_name> origin/<remote_branch_name> # 创建本地分支并关联远程分支 ``` 4. **删除分支** ```bash git branch -d <branch_name> # 删除本地分支 git push origin --delete <branch_name> # 删除远程分支 ``` --- #### 二、标签(Tag)操作 1. **创建标签** - 轻量标签(无注释):`git tag <tag_name>` - 带注释的标签(推荐): ```bash git tag -a v1.0 -m "Release version 1.0" # 本地创建 git push origin v1.0 # 推送到 GitHub ``` 2. **查看标签** ```bash git tag # 查看本地标签列表 git show v1.0 # 查看标签详细信息 git ls-remote --tags origin # 查看远程标签 ``` 3. **删除标签** ```bash git tag -d v1.0 # 删除本地标签 git push origin --delete v1.0 # 删除远程标签 ``` 4. **基于标签检出代码** ```bash git checkout v1.0 # 切换到标签对应的提交(进入分离头指针状态) ``` --- #### 三、关键注意事项 1. **分支与标签的区别** - 分支(Branch)用于开发流程,会随新提交移动;标签(Tag)标记特定提交(如版本发布),不可修改[^2]。 - 示例:`git tag v2.0 -m "正式版本"` 标记当前提交为稳定版本[^3]。 2. **远程操作同步** - 新建分支/标签后需显式推送:`git push origin <name>` - 删除远程分支/标签需使用专用命令(见上文)。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值