git创建分支

本文介绍了如何使用Git进行分支管理,包括创建、切换、提交分支到远程仓库等操作,并提供了实用的Git命令列表。

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

参考:https://www.cnblogs.com/bluestorm/p/6252900.html

https://my.oschina.net/u/219482/blog/285584

https://blog.youkuaiyun.com/zxy987872674/article/details/79273148

1.查看

远端分支 git branch -r 

本地分支 git branch 

2.从已有的分支创建新的分支(如从master分支),创建一个dev分支

git checkout -b 1

创建完可以查看一下,分支已经切换到dev

或者:

如果用命令行,运行 git fetch,可以将远程分支信息获取到本地,再运行 git checkout -b local-branchname origin/remote_branchname  就可以将远程分支映射到本地命名为local-branchname  的一分支

3.提交该分支到远程仓库

git push origin 1

4.测试

git pull origin 1

5.设置git push,pull默认的提交获取分支,这样就很方便的使用git push 提交信息或git pull获取信息

git branch --set-upstream-to=origin/dev

6.git commit ,git push,之后就可以直接提交到远程的1分支中,而不会是master

 

git命令

在本地新建一个分支: git branch newBranch 
切换到你的新分支: git checkout newBranch 
将新分支发布在github上: git push origin newBranch 
在本地删除一个分支: git branch -d newBranch 
在github远程端删除一个分支: git push origin :newBranch (分支名前的冒号代表删除) 
/git push origin –delete newBranch 
注意删除远程分支后,如果有对应的本地分支,本地分支并不会同步删除!
 

 

<think>我们正在讨论Git创建分支的方法。根据引用内容,创建分支主要有两种方式:基于当前所在分支创建分支,或基于特定分支/提交创建分支。 引用[1]提到:从特定分支创建分支,命令为:git branch new_branch_name different_branch_name 引用[3]提到:创建分支后切换到该分支,并推送到远程仓库:git checkout browser-1.8.0 和 git push --set-upstream origin browser-1.8.0 另外,我们通常使用更简洁的方式:创建并切换分支git checkout -b 新分支名),或者使用较新的git switch命令。 因此,创建分支的常用方法如下: 1. 基于当前分支创建分支并切换到该分支git checkout -b new_branch_name 或者(Git 2.23版本及以上): git switch -c new_branch_name 2. 基于特定分支创建分支(不切换): git branch new_branch_name different_branch_name 3. 基于某个提交创建分支(先获取提交的哈希值): git branch new_branch_name commit_hash 4. 创建分支后切换到该分支git branch new_branch_name # 创建分支 git checkout new_branch_name # 切换到新分支 或者 git switch new_branch_name # 切换分支Git 2.23+) 5. 将新分支推送到远程仓库(使远程也有该分支): git push -u origin new_branch_name # -u 设置上游,以后可以直接git push 注意:在推送之前,本地分支必须存在。 下面我们详细说明这些步骤。</think>### Git创建分支的方法 在 Git创建分支主要有以下几种方式,根据使用场景可选择最适合的方法: #### 1. **基于当前分支创建分支(最常用)** ```bash git checkout -b new_branch_name ``` - 创建 `new_branch_name` 分支并自动切换到该分支 - 等价于两条命令: ```bash git branch new_branch_name # 创建分支 git checkout new_branch_name # 切换分支 ``` #### 2. **基于特定分支创建分支** ```bash git checkout -b new_branch_name different_branch_name ``` - 从已存在的 `different_branch_name` 分支创建分支[^1] - 示例:从 `develop` 分支创建 `feature/login` 分支: ```bash git checkout -b feature/login develop ``` #### 3. **基于特定提交创建分支** ```bash git branch new_branch_name commit_hash ``` - 通过指定提交哈希值创建分支: ```bash git branch hotfix 3a4b5c6d ``` #### 4. **创建远程分支** 创建本地分支后推送到远程仓库: ```bash git push -u origin new_branch_name ``` - `-u` 参数设置上游跟踪分支,后续可直接用 `git push`[^3] - 首次推送后,其他开发者可通过 `git fetch` 获取该分支 #### 5. **Git 2.23+ 推荐方式** ```bash git switch -c new_branch_name ``` - `switch` 命令专用于分支切换,比 `checkout` 语义更清晰 > **最佳实践**:创建分支后立即推送到远程仓库,避免本地分支丢失: > ```bash > git push -u origin new_branch_name > ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值