文章内容
2025/9/2 23:55:45,作 者: 黄兵
git checkout -b 命令解析
这条命令经常被使用:
git checkout -b <branch-name>
作用
它相当于 创建并切换分支 的组合命令,等价于:
git branch <branch-name> # 创建新分支 git checkout <branch-name> # 切换到新分支
所以 git checkout -b 会:
-
基于当前所在分支(默认是你现在的 HEAD)创建一个新的分支;
-
立即切换到这个新分支上。
举例
假设你现在在 main 分支:
git checkout -b feature/login
执行后:
-
会新建一个
feature/login分支,基于当前main的最新提交; -
并把工作目录切换到
feature/login上。
⚠️ 需要注意:
-
如果目标分支已经存在,
git checkout -b会报错;这时候要用git checkout <branch-name>。 -
在新版 Git(2.23+)里,更推荐用
git switch:
git switch -c <branch-name> # 等价于 git checkout -b git switch <branch-name> # 等价于 git checkout <branch-name>
一窝蜂的力量:解读中国产业潮涌的制度、资本与从众动因
warning: in the working copy of 'document.css', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'document.css', LF will be replaced by CRLF the next time Git touches it
评论列表