文档:https://git-scm.com/book/zh/v2/Git-分支-分支简介

分支理解

  • master分支是项目在创建时候的默认分支,除此之外,它并没有更多的含义。

  • 剩下的 “开发分支”,“灰度分支”, “预发布分支”, “需求分支”,“测试分支” 都是根据项目和需求约定的。它们本质上只是一个分支而已。

分支在项目中的应用

1、首先,我们创建了一个项目:

http://10.2.16.183/zhiheng/myproject

这是我局域网搭建的gitlab,我们就以这个项目为例。

2、项目的基本流程:

  • 克隆项目到本地
> git clone http://10.2.16.183/zhiheng/myproject

Cloning into 'myproject'...
warning: redirecting to http://10.2.16.183/zhiheng/myproject.git/
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done. 切换到项目:
> cd myproject
  • 查看当前状态
> git status

On branch master
Your branch is up to date with 'origin/master'. Untracked files:
(use "git add <file>..." to include in what will be committed) .idea/
a.py nothing added to commit but untracked files present (use "git add" to track)
  • 提交代码
> git add a.py
> git commit -m "first file"
> git push origin master

3、分支的使用

为什么要使用分支?

1、你在开发项目里面一个很大的模块,这个模块需要连续开发一个月,你可以选择一个月提交一次,但一个月的开发代码都存在你的本地电脑是很危险的。万一电脑丢失,代码被误删,损失很大!

2、你们团队的项目有十几个人在维护,每天会有N多次的提交,一旦你拉取和提交的间隙,被别人提交了代码,当你在提交的时候别人就需要解决冲突。每次解决和提交冲突是很浪费时间的。

分支的使用

  • 查看所有分支(远程分支和本地分支)
> git branch -a

* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
  • 查看本地分支
> git branch -a
* master
  • 创建分支
> git branch dev
  • 切换分支
> git checkout dev

当你当前分支有未提交的文件时,不允许你提交分支。

  • 在 dev 分支上面操作

创建 dev_a.py 文件

dev> git status

On branch dev
Untracked files:
(use "git add <file>..." to include in what will be committed) .idea/
dev_a.py nothing added to commit but untracked files present (use "git add" to track) dev> git add dev_a.py dev> git commit -m "dev a file"
[dev ace2539] dev a file
1 file changed, 1 insertion(+)
create mode 100644 dev_a.py
  • 目前虽然本地多了一个 dev 分支, 但远程是没有的。
dev> git branch -a

* dev
master
remotes/origin/HEAD -> origin/master
remotes/origin/master
  • 提交到远程分支。
git push origin dev
warning: redirecting to http://10.2.16.183/zhiheng/myproject.git/
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 320 bytes | 320.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: To create a merge request for dev, visit:
remote: http://10.2.16.183/zhiheng/myproject/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote:
To http://10.2.16.183/zhiheng/myproject
* [new branch] dev -> dev
  • 再次查看所有分支, 远程分支也多了一个dev
dev> git branch -a
* dev
master
remotes/origin/HEAD -> origin/master
remotes/origin/dev
remotes/origin/master
  • 不同分支下面,文件数量不一样。
## dev 分支
dev> ls
README.md a.py dev_a.py ## master分支
master> ls
README.md a.py

4、代码的冲突与解决。

假设A 和 B 在一个分支上开发

1、A 拉取 common.py 文件,修改。

2、B 拉取 common.py 文件,修改。

3、B 提交了 common.py 文件的修改。

4、A 在提交 common.py 文件时就会遇到冲突, A 应该怎么做?

  • 拉取远程代码
git pull origin master
warning: redirecting to http://10.2.16.183/zhiheng/myproject.git/
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From http://10.2.16.183/zhiheng/myproject
* branch master -> FETCH_HEAD
ed59b2e..c166f93 master -> origin/master
Updating ed59b2e..c166f93
error: Your local changes to the following files would be overwritten by merge:
common.py
Please commit your changes or stash them before you merge.
Aborting

这个时候发现代码被 B 修改了,因为我本地也做了更新,所以不允许拉取。

  • 先提交提交代码,再拉取。
> git add common.py
> git commit -m "A 也修改了文件"
> git pull origin master
warning: redirecting to http://10.2.16.183/zhiheng/myproject.git/
From http://10.2.16.183/zhiheng/myproject
* branch master -> FETCH_HEAD
Auto-merging common.py
CONFLICT (content): Merge conflict in common.py
Automatic merge failed; fix conflicts and then commit the result.
  • 解决冲突
vim common.py
# 这是一个公共文件

def common(a, b):
<<<<<<< HEAD
c = a + b
return c
=======
return a + b
>>>>>>> c166f934de72779168cd00ef40bb96ff65e09ab5

开发的过程尽量避免多人改一个方法,像这样的冲突就比较解决了。 A和B需要坐到一起,这个冲突解决。

  • 重新提交冲突
> git add common.py
> git commit -m "合并冲突"
[master 485cfaa] 合并冲突
> git push origin master

5、分支的合并。

如果多个开发同时在一个分支上开发,上面的冲突每天要发生很多次。这将严重影响开发效率。 每个开发都在自己的分支上面开发。

  • A开发在 dev 分支。
git branch
master
* dev test> ls
README.md a.py dev1.py dev2.py dev_a.py
  • B开发在 test 分支。
git branch
master
* test test> ls
a.py common.py README.md test1.py test2.py

此时,两个分支的上的代码出现了较大的不同。

testdev合并到master

1、 在A电脑上有本地只有 master 和 dev ,可以直接合并。

> git merge dev
Merge made by the 'recursive' strategy.
dev_a.py | 1 +
1 file changed, 1 insertion(+)
create mode 100644 dev_a.py

2、B电脑本地只有 master 和 test 分支。

  • B电脑:先把 test 分支推送
git add .
> git commit -m "test分支代码"
[test 14032ea] test分支代码
2 files changed, 2 insertions(+)
create mode 100644 test1.py
create mode 100644 test2.py
> git push origin test
  • A电脑:本地创建 test 分支,拉取远程 test 分支的代码
> git branch test
> git checkout test
Switched to branch 'test'
> git pull
  • A电脑:回到 master 分支,合并 test 分支。
> git merge test
Updating 34fa4b3..7677952
Fast-forward
test1.py | 1 +
test2.py | 1 +
2 files changed, 2 insertions(+)
create mode 100644 test1.py
create mode 100644 test2.py > ls
README.md a.py common.py dev1.py dev2.py dev_a.py test1.py test2.py

master 分支就拥有了所有分支的代码。 在这个过程中,

1、在代码分支合并的过程中如果出现冲突,就要解决冲突。

2、我们在分支开发的过程中,也可以时长拉取master分支的内容,一般合并到master的代码都是相对完整的。这样可以避免master合并的时候出现过多的冲突。

git分支概念与项目中的应用的更多相关文章

  1. Git Bash+EGit在项目中配合使用最常用方法总结(根据场景使用)

    最近在项目中使用Git进行代码管理,之前一直用SVN进行管理,现在谈一谈Git在项目中如何与EGit插件配合使用,高效同步开发. 使用过SVN一段时间的人,初识Git一定感觉很别扭,发现会遇到各种各样 ...

  2. 命令版本git 分支篇-----不断更新中

    最近应用开发的过程中出现了一个小问题,顺便记录一下原因和方法--命令版本 开发中想看看过去某个版本的代码,我们先查看log git log commit f224a720b8192165a4e70f2 ...

  3. Git 如何只更新项目中某个目录里的文件

    Git由于在远端和本地都有一个代码库, 这样更新单个文件比SVN要麻烦一点.   1. 如果想拿远端git服务器上的最新版本(或某个特定版本)覆盖本地的修改,可以使用git pull命令,   但这会 ...

  4. 【开发工具】-- IDEA集成Git在实际项目中的运用

    1.企业实际项目中Git的使用 在实际的企业项目开发中,我们一般Java的项目在公司都有自己的局域网代码仓库,仓库上存放着很多的项目.以我工作过的公司如华为的项目,一般是存放在企业内部的CodeHub ...

  5. 近期关于CI/CD策略以及git分支模型的思考

    近两个月由于个人处于新环境.新项目的适应阶段,没怎么提笔写些文章.中间有好几个想法想记录下来分享,但受限于没有很好的时间段供自己总结思考(也可以总结为间歇性懒癌和剧癌发作),便啥也没有更新.借这个周末 ...

  6. git 利用分支概念实现一个仓库管理两个项目

    需求描述:开发了一个网站,上线之际,突然另一个客户说也想要个一样的网站,但网站的logo和内部展示图片需要替换一下,也就是说大部分的后台业务逻辑代码都是一致的,以后升级时功能也要保持一致:刚开始想反正 ...

  7. 项目开发中git常用命令、git工作流、git分支模型

    #新建代码库git init # 在当前目录新建一个Git代码库git init [project-name] # 新建一个目录,将其初始化为Git代码库git clone [url] # 下载一个项 ...

  8. git分支在项目中管理

    实际项目中如何使用Git做分支管理 2018年06月24日 18:08:24 ShuSheng007 阅读数:9241   版权声明: https://blog.csdn.net/ShuSheng00 ...

  9. Git基本概念,流程,分支,标签及常用命令

    Git基本概念,流程,分支,标签及常用命令 Git一张图 Git基本概念 仓库(Repository) 分支(Branch) Git工作流程 Git分支管理(branch) 列出分支 删除分支 分支合 ...

随机推荐

  1. hdu 1301 Jungle Roads krusckal,最小生成树,并查集

    The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was s ...

  2. 迷宫问题 POJ - 3984 [kuangbin带你飞]专题一 简单搜索

    定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...

  3. CentSO7.6下部署Maridb Galera Cluster 实践记录(二)

    早上三个节点的数据库都启动正常,下午上班就都不行了,哎,VM啊,中午就是让主机休息了一些而已么. 今天继续折腾中,第二天再来一遍:重启第一台服务器上的galera时竟然报错了:错误如下:    It ...

  4. lambda表达式的一些学习

    关于lambda表达式的一些学习,例举出来. 1.学生实体类 public class Student { private String name; private int age; public S ...

  5. 最佳内存缓存框架Caffeine

    Caffeine是一种高性能的缓存库,是基于Java 8的最佳(最优)缓存框架. Cache(缓存),基于Google Guava,Caffeine提供一个内存缓存,大大改善了设计Guava's ca ...

  6. python列表排序用法

    错误用法::: a=list('hdfoiegfjil').sort()

  7. 分享一个 pycharm 专业版的永久使用方法

    刚开始接触Python,首先要解决的就是Python开发环境的搭建. 目前比较好用的Python开发工具是PyCharm,他有社区办和专业版两个版本,但是社区版支持有限,我们既然想好好学python, ...

  8. ubuntu httpie使用方法

    HTTPie 是用 Python 写的,所以你可以在几乎所有地方(Linux,MacOSX,Windows)安装它.而且,在大多数的 Linux 发行版中都有编译好的安装包. Debian,Ubunt ...

  9. 用101000张食物图片实现图像识别(数据的获取与处理)-python-tensorflow框架

    前段时间,日剧<轮到你了>大火,作为程序员的我,看到了另外一个程序员—二阶堂,他的生活作息,以及饮食规律,让我感同身受,最让我感触的是他做的AI聊天机器人,AI菜品分析机器人,AI罪犯分析 ...

  10. js 中 undefined、NaN、null

    undefined 即未定义 js 中 没有声明 或者 声明后未赋值的变量 用typeof判断后类型都是 undefined 但是直接console.log( ) 输出的话 没有声明的变量会报错:而声 ...