git branch

文章目录

1.简介

git branch 用于管理分支,包括查看、创建、删除、重命名和关联。

git branch 是 Git 版本控制系统中用于管理分支的命令。分支是 Git 的核心功能之一,允许开发者在同一个代码库中并行开发不同的功能或修复问题。

2.格式

shell 复制代码
git branch [--color[=<when>] | --no-color] [--show-current]
	[-v [--abbrev=<n> | --no-abbrev]]
	[--column[=<options>] | --no-column] [--sort=<key>]
	[--merged [<commit>]] [--no-merged [<commit>]]
	[--contains [<commit>]] [--no-contains [<commit>]]
	[--points-at <object>] [--format=<format>]
	[(-r | --remotes) | (-a | --all)]
	[--list] [<pattern>...​]
git branch [--track[=(direct|inherit)] | --no-track] [-f]
	[--recurse-submodules] <分支名> [<起始点>]
git branch (--set-upstream-to=<上游> | -u <上游>) [<分支名>]
git branch --unset-upstream [<分支名>]
git branch (-m | -M) [<旧分支>] <新分支>
git branch (-c | -C) [<旧分支>] <新分支>
git branch (-d | -D) [-r] <分支名>...​
git branch --edit-description [<分支名>]

3.选项

-a, --all
	列出所有分支(包括本地分支和远程跟踪分支)。
-r, --remotes
	仅列出远程跟踪分支。
-d, --delete
	删除指定的分支。如果分支未合并,Git 会拒绝删除。
-D
	强制删除分支,即使分支未合并。
-m, --move
	重命名分支。
-c
	复制分支。
--list
	列出本地分支(默认行为)。
--no-merged
	列出尚未合并到当前分支的分支。
--merged
	列出已经合并到当前分支的分支。
-v, --verbose
	显示每个分支的最新提交信息。
--set-upstream-to
	设置当前分支跟踪指定的远程分支。

4.示例

(1)查看分支。

shell 复制代码
# 查看本地分支
git branch

# 查看远端分支
git branch -r

# 查看所有分支
git branch -a

# 查看本地分支 commit id 与 commit comment
git branch -v|--verbose

# 查看本地分支commit id与commit comment,以及关联的上游分支
git branch -vv

(2)创建分支。

shell 复制代码
# 基于当前分支创建本地分支不切换
git branch <branchname>

# 基于当前分支创建本地分支并切换
git checkout -b <branchname>
# 或
git switch -c <branchName>

# 将创建的本地分支推送到远端,远端分支不存在则创建。
git push origin <local_branchname>:<remote_branchname>

(3)更新分支。

shell 复制代码
# 使用远端分支更新本地分支
git pull [远程仓库名] [远程分支名]:[本地分支名]

# 使用关联的远端分支更新当前本地分支
git pull

(5)合并分支。

shell 复制代码
# 合并某分支到当前分支
git merge <srcbranch>

(6)删除分支。

shell 复制代码
# 删除本地分支
git branch (-d | --delete) <branchname>

# 强制删除本地分支
git branch (-D | -df | --delete --force)  <branchname>

# 删除远端分支
git push origin :<remote_branchname>
# 或
git push  origin (-d | --delete) <branchname>

# 注意,该命令无法删除远端分支,只是删除 git branch -r 列表中的远端追踪分支
git branch -dr origin/<branchname>

(7)本地分支关联远端分支。

shell 复制代码
# 第一种情况,远端分支已经存在。不指定 local_branchname 为当前分支。
git branch (--set-upstream-to=origin/<branchname> | -u origin/<branchname>) [<local_branchname>]

# 第二种情况,远端分支不存在
# 1.将当前本地分支推送至远端并关联(远端分支与本地分支同名)
git push (-u | --set-upstream) origin HEAD

# 2.将当前本地分支推送至远端并关联(指定远端分支名)
git push (-u | --set-upstream) origin <remote_branchname>

# 3.将本地分支推送至远端并关联(指定本地与远端分支名)
git push (-u | --set-upstream) origin <local_branchname>:<remote_branchname>

(8)删除本地分支与远端分支的关联。

shell 复制代码
git branch --unset-upstream [<local_branch>]

(9)重命名本地分支。

shell 复制代码
# 重命名当前分支
git branch (-m | --move) <newbranch>

# 重命名指定分支
git branch (-m | --move ) <oldbranch> <newbranch>

# 强制重命名本地分支
git branch (-M -f | --move --force) [<oldbranch>] <newbranch>

(10)重命名远端分支。

Git 没有直接修改远端分支名的命令,我们可以通过删除重建的方式来间接重命名远端分支。

shell 复制代码
# 先删除远端分支
git push origin -d <branch_name>

# 再重命名当前本地分支
git branch (-m | --move) <newbranch>

# 将当前本地分支推送至远端并关联
git push origin (-u | --set-upstream) HEAD

参考文献

git-branch - 列出、创建或删除分支

相关推荐
绿白尼7 小时前
git 使用常见错误整理
git
一抓掉一大把10 小时前
git -学习笔记
笔记·git·学习
一代...13 小时前
Git基本使用
git
抽象具象化15 小时前
BUG日志:使用热点或免费加速器时git链接github出现端口22拒绝访问的解决方法
git·github·bug
2501_9032386521 小时前
Git Bash:Windows下的强大命令行工具
windows·git·bash·个人开发
神明木佑1 天前
datalist 是什么?以及作用是什么?
git
那片海还在吗1 天前
使用 Git、Postman、Newman、Jenkins 与邮件构建自动化接口测试及通知流程
git·jenkins·postman
heeheeai1 天前
git 国内源
git
the_seventh_dog1 天前
git | 团队协作开发注意事项
git