一般一个项目有一个默认的分支 master 主分支,然后可以有许多个分支,在别的分支上的操作不会影响到主分支。使用git branch查看当前多多少分支以及当前处于哪个分支上;执行git branch 分支名称  创建分支;执行 git checkout 分支名称 切换当前分支。

使用 git branch -d 分支名称 删除分支 使用git branch -m 当前分支名称 新分支名称   修改分支名称

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git branch
* master laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git branch slave laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git branch
* master
slave laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git checkout slave
Switched to branch 'slave' laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git branch
master
* slave laoni@DESKTOP-TPP

在分支上提交做的修改,不会影响到master分支,可以这么做,把原有master分支的源码下到本地,然后创建新的分支,把自己修改后的代码commit到新分支上。

eg:在slave分支commit info.py文件,然后再切换到master分支

切换到master分支后查看提交的记录没有add info.py 文件内容也消失了。(如果是新建的文件,那文件也会消失。)

git merge 分支名称 合并分支,最理想的状态是被合并的分支(slave)里的文件,是当前分支(master)没有的,这样就完全没冲突。

$ git checkout slave
Switched to branch 'slave' laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git branch
master
* slave laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ ls
bb.css dd.css index.html info.py laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ cat info.py
asdfsadfsfasfasdf
laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git checkout master
Switched to branch 'master' laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ ls
bb.css dd.css index.html laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git merge slave
Updating c5b475a..a068c80
Fast-forward
info.py | +
file changed, insertion(+)
create mode info.py laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ cat info.py
asdfsadfsfasfasdf

git merge 分支名称

使用git diff可以查看当前分支做了哪些修改,使用git diff master..slave 可以对比两个分支的不同。

PS:修改文件后需要重新add和commit

使用git diff

$ ls
bb.css dd.css index.html info.py laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ vim info.py laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git diff
warning: LF will be replaced by CRLF in info.py.
The file will have its original line endings in your working directory.
diff --git a/info.py b/info.py
index 5c895f9..7e024db
--- a/info.py
+++ b/info.py
@@ - +, @@
-asdfsadfsfasfasdf
\ No newline at end of file
+asdfsadfsfasfasdf
+modify

继续上面的例子执行git diff master..slave

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git status
On branch slave
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory) modified: info.py Untracked files:
(use "git add <file>..." to include in what will be committed) dd.css no changes added to commit (use "git add" and/or "git commit -a") laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git add info.py
warning: LF will be replaced by CRLF in info.py.
The file will have its original line endings in your working directory. laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git status
On branch slave
Changes to be committed:
(use "git reset HEAD <file>..." to unstage) modified: info.py Untracked files:
(use "git add <file>..." to include in what will be committed) dd.css laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git commit -m "modify info.py"
[slave 6abf028] modify info.py
file changed, insertions(+), deletion(-) laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git status
On branch slave
Untracked files:
(use "git add <file>..." to include in what will be committed) dd.css nothing added to commit but untracked files present (use "git add" to track) laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git diff master..slave
diff --git a/info.py b/info.py
index 5c895f9..7e024db
--- a/info.py
+++ b/info.py
@@ - +, @@
-asdfsadfsfasfasdf
\ No newline at end of file
+asdfsadfsfasfasdf
+modify 66666

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git branch
master
* slave

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git checkout master
Switched to branch 'master'

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git merge slave
Updating a068c80..6abf028
Fast-forward
info.py | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ cat info.py
asdfsadfsfasfasdf
modify 66666

关于合并冲突代码的做法:

下面我分别给master和slave创建文件new,其中master的内容是master,slave的new的内容是slave。

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ vim new laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ cat new
master laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git add new
warning: LF will be replaced by CRLF in new.
The file will have its original line endings in your working directory. laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git commit -m "add new"
[master 8b5d2fd] add new
file changed, insertion(+)
create mode new laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git checkout slave
Switched to branch 'slave' laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ vim new laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git add new
warning: LF will be replaced by CRLF in new.
The file will have its original line endings in your working directory. laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git commit -m "add new"
[slave 775f3ab] add new
file changed, insertion(+)
create mode new laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$

master slave

接下来要合并冲突代码:

PS:git返回信息提示有内容冲突文件new,让我们修改了new后再commit

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git checkout master
Switched to branch 'master' laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git merge slave
Auto-merging new
CONFLICT (add/add): Merge conflict in new
Automatic merge failed; fix conflicts and then commit the result.

等号上面是当前分支(master)的,等号下面是要被合并的分支(slave),我们修改这个文件,在冲突内容中选择一个,删除另外的,再commit就可以了。

$ git add new

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master|MERGING)
$ git commit -m "modify new"
[master 112da02] modify new laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed) dd.css nothing added to commit but untracked files present (use "git add" to track) laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git log --oneline
112da02 modify new
775f3ab add new
8b5d2fd add new
6abf028 modify info.py
a068c80 add info.py
c5b475a Revert "add 123"
cfcbd5c add
13f5bcb add
Revert "add adc"
74f7cb6 add bb.css
577fab6 Revert "revert abc"
e1f2701 add adc
358cdac 添加UI.js
04c94a8 添加一个文件index.html

接下来有第三种合并方式,会保留合并分支的记录

$ git branch
* master
slave laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git checkout slave
Switched to branch 'slave' laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ ls
bb.css dd.css index.html info.py new laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ vim laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git add
warning: LF will be replaced by CRLF in .
The file will have its original line endings in your working directory. laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git commit -m "add 998"
[slave 6957dae] add
file changed, insertion(+)
create mode laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git checkout master
Switched to branch 'master' laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git merge slave --no-ff -m "merge slave --no--ff"
Merge made by the 'recursive' strategy.
| +
file changed, insertion(+)
create mode laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git log --oneline
bdba943 merge slave --no--ff
6957dae add
112da02 modify new
775f3ab add new
8b5d2fd add new
6abf028 modify info.py
a068c80 add info.py
c5b475a Revert "add 123"
cfcbd5c add
13f5bcb add
Revert "add adc"
74f7cb6 add bb.css
577fab6 Revert "revert abc"
e1f2701 add adc
358cdac 添加UI.js
04c94a8 添加一个文件index.html

总结:分支合并有三种:

  1、faster快进的合并分支,这种最理想没文件内容冲突,但git log里查看不到合并记录。

  2、冲突合并,修改冲突的文件再add和commit。

  3、禁用faster,这种方式会记录合并分支记录,在git log里可以查到。

git branch 分支和分支合并的更多相关文章

  1. git branch 命令查看分支、删除远程分支、本地分支

    git branch 命令操作 1.查看本地分支 : git branch 前面带有*号的是当前分支 2 .删除本地分支: git branch -d [branchname] 提示删除了一个名为li ...

  2. 【转】git branch 命令查看分支、删除远程分支、本地分支

    git branch 命令操作 1.查看本地分支 : git branch 前面带有*号的是当前分支 2 .删除本地分支: git branch -d [branchname] 提示删除了一个名为li ...

  3. git branch 分支与合并

    在使用 git 进行分支开发与合并的时候需要用到这些命令.其他基本 git 命令参考 Git 简易食用指南 git branch 查看分支 git branch 查看当前分支情况 创建分支 git b ...

  4. git 使用详解(9)-- 分支的新建与合并 git branch -d、merge、 --merged/--no-merged/-v

    现在让我们来看一个简单的分支与合并的例子,实际工作中大体也会用到这样的工作流程: 开发某个网站. 为实现某个新的需求,创建一个分支. 在这个分支上开展工作. 假设此时,你突然接到一个电话说有个很严重的 ...

  5. git branch分支管理用法总结

    查看分支(远程和本地) 1 查看本地分支: $ git branch 2 查看远程分支: $ git branch -r 3.查看本地和远程分支 $ git branch -a 创建分支 1.创建本地 ...

  6. Git知识总览(三) 分支的创建、删除、切换、合并以及冲突解决

    前两篇博客集中的聊了git的一些常用命令,具体请参见<Git知识总览(一) 从 git clone 和 git status 谈起>.<Git知识总览(二) git常用命令概览> ...

  7. git branch 分支

    几乎所有的版本控制系统都以某种形式支持分支. 使用分支意味着你可以把你的工作从开发主线上分离开来,以免影响开发主线. 在很多版本控制系统中,这是一个略微低效的过程——常常需要完全创建一个源代码目录的副 ...

  8. 10.Git分支-分支管理(git branch命令)、分支开发工作流

    1.分支管理  git branch 不仅可以创建和删除分支,还可以做一些其他工作. 1.不带参数的 git branch ,得到本地仓库当前的分支列表.并且会显示,当期所在的分支,也就是HEAD所指 ...

  9. git branch 分支操作

    一.git分支命令 Git鼓励大量使用分支: 查看分支:git branch 创建分支:git branch <name> 切换分支:git checkout <name> 创 ...

随机推荐

  1. MFC 模块状态的实现

    本技术备忘录介绍MFC “模块状态”结构的实现.充分理解模块状态这个概念对于在DLL中使用MFC的共享动态库是十分重要的. MFC的状态信息分为三种:全局模块状态数据.进程局部状态数据和线程局部状态数 ...

  2. filter 在CSS用的效果

    滤镜说明: Alpha:设置透明层次 blur:创建高速度移动效果,即模糊效果 Chroma:制作专用颜色透明 DropShadow:创建对象的固定影子 FlipH:创建水平镜像图片 FlipV:创建 ...

  3. 实用maven笔记一概念&构建

    maven,作为我现在每天都会使用的工具,却发现我还有很多地方了解的迷迷糊糊.老大就曾说过我的一个问题在于,做事情不够精细.大概就是太浮于表面吧.最近突然非常想把maven撸一遍.豆瓣搜了下高分书籍, ...

  4. JPA派生标识符2

    @Entity@Table(name = "adam_importfile")@IdClass(BusinessAdviserFileId.class)public class B ...

  5. docker stack利用secrets启动wordpress

    docker-compose文件 version: '3.1' services: web: image: wordpress ports: - : secrets: - my-pw environm ...

  6. 二、检索语句 SELECT、ORDER BY、WHERE

    介绍如何使用SELECT语句从表中检索一个或多个数据列   第二章: SELECT语句 SQL语句可以在一行给出,也可以分成许多行,分成多行更容易调试. 多条SQL语句必须以分号 分隔.多数DBMS不 ...

  7. mysql8的坑

    plugin caching_sha2_password could not be loaded 我在mac上用Sequel Pro连数据库的时候,会报出以上错误,这是应为8.0.11把身份认证插件改 ...

  8. java爬取猫咪上的图片

    首先是对知识点归纳 1.用到获取网页源代码,分析图片地址,发现图片的地址都是按编号排列的,所以想到用循环获取 2.保存图片要用到流操作和文件操作,对两部分知识进行了复习巩固 3.保存后的图片有一部分是 ...

  9. sort -n

    输入如下测试数据:  当按照第一列排列时是正确的:  但按照第二列排序时,喵喵喵???怎么跟说好的不一样啊!!!为什么gugu的50会排在最后?  其实是因为默认是按照第二列的第一个字符来比较的,若想 ...

  10. Tomcat 在IE中下载rar文件直接以乱码方式打开解决方案

    这几天一直很纳闷,在Tomcat部署的网站中的下载文件中,如果文件是rar类型的,一点击下载rar文件就直接打开,并且出现乱码,右键另存为浏览器也是默认为html格式,一直以为是浏览器IE的问题,后来 ...