【Git常用】之分支

1. 基础

1.1 Git存储资料的方式

前提内容:【Git常用】之深入理解Git存储文档的方式

1.2 分支浅谈

Git中的分支,本质上是一个指向commit物件的可变指针。Git通过存储一个名为HEAD的指针记录你当前的分支。

在这里插入图片描述

2. 常用操作

2.1 展示

1.列出本地分支

$ git branch    # 不加任何参数
  branch_1
* master
  dev

2.列出远端分支

$ git branch -r
  remotes/origin/Branch_2
  remotes/origin/Branch_3

3.列出本地及远端分支

$ git branch -a
  remotes/origin/Branch_2
  remotes/origin/Branch_3

4.查看提交资讯

$ git branch -v
  branch_1  e6b1e03a8cf fix one
* master    f3ab88c72b7 fix two
  dev       bd239c9cc13 fix three

2.2 创建

1.创建一个本地分支

$ git branch [branch_name]

2.切换分支

$ git checkout [branch_name]    # 切换成本地已存在的分支

当分支是本地分支时,直接切换

注意,若写成如下形式,将会进入 " ‘detached HEAD’ state"

$ git checkout [remote_name]/[branch_name]    # 切换远端分支,进入'detached HEAD' state

Git提示为:

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 performing another checkout.

3.创建并切换一个本地分支

$ git checkout -b [branch_name]

4.创建并切换一个跟踪分支(tracking branch)

$ git checkout -b [local_branch_name] [remote_name]/[remote_branch_name]

版本1.6.2以上,可以简化为

$ git checkout --track [remote_name]/[remote_branch_name]

2.3 合并

1.合并

$ git merge [branch_name]

Fast Forward,快进提示,合并进来的分支,是当前分支的直接上游。
CONFLICT,冲突提示,此时git status会发现unmerged状态文件。

2.4 删除

1.删除

$ git branch -d [branch_name]

参数-D是强制删除,建议谨慎使用

2.5 推送

1.当前分支是一个跟踪分支

$ git push

2.当前分支是自己新建的本地分支,不是一个跟踪分支

$ git push --set-upstream [remote_name] [remote_brach_name]

2.6 拉取

$ git pull [remote_name]

3. 总结

本篇列出的都是常用的操作,可以调用git help […]来查看某个信息的全部操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值