解决冲突

现在我把gitTest中的东西全删了包括那个.git文件。

初始化仓库git init,新建一个a.txt,在里边写个master,执行git add a.txt,然后执行git commit -m ‘add a.txt’

然后创建第一个分支 git branch feature1,然后创建第二个分支并切换到第二个分支git checkout -b feature2,在a.txt中另起一行添加feature2,然后执行git add a.txt和 git commit -m ‘feature2 add a word’,然后切换到master,git checkout master,把feature2分支合并到master分支, git merge –no-ff -m ‘merge feature2’ feature2。之后切换到feature1,git checkout feature1,在a.txt中另起一行添加feature1,然后执行git add a.txt和 git commit -m ‘feature1 add a word’。

现在整理一下,master分支a.txt中原始的数据为

master
  • 1

后来创建了分支feature1里边的内容同样为

master
  • 1
  • 2

后来创建了feature2分支,并把feature2中的内容合并到了master,所以现在master中a.txt的内容为

master
feature2
  • 1
  • 2
  • 3

后来我把feature1中的信息改为

master
feature1
  • 1
  • 2
  • 3

现在master中的内容和feature1最初复制的内容不一样,现在切换到master分支,把feature1合并到master肯定会有冲突。

执行git merge feature1

$ git merge --no-ff -m 'merge feature1' feature1
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
Automatic merge failed; fix conflicts and then commit the result.
  • 1
  • 2
  • 3
  • 4
  • 5

会发现有冲突了,这时候a.txt中的内容为

master
<<<<<<< HEAD
feature2
=======
feature1
>>>>>>> feature1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Git用<<<<<<<=======>>>>>>>标记出不同分支的内容。HEAD为当前所在分支的内容,也就是说现在master中的内容。feature1为feature1分支中的内容。

现在我a.txt中内容改为

master
feature2
feature1
  • 1
  • 2
  • 3
  • 4

然后执行

YZ@YZ MINGW64 /d/gitTest (master|MERGING)
$ git add a.txt

YZ@YZ MINGW64 /d/gitTest (master|MERGING)
$ git commit -m 'fix confict'
[master 6f57951] fix confict

YZ@YZ MINGW64 /d/gitTest (master)
$
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

这样就好了。git log –graph –pretty=oneline –abbrev-commit

–pretty=oneline这个参数的目的是不再显示Author和Date,只显示带有id号的一行。–abbrev-commit的意思是缩写id号。

YZ@YZ MINGW64 /d/gitTest (master)
$ git log --graph --pretty=oneline --abbrev-commit
*   7f2f3b5 (HEAD -> master) fix conflict
|\
| * f56a04d (feature1) feature1 add a word
* |   1387cb8 Merge branch 'feature2'
|\ \
| |/
|/|
| * 14ef937 (feature2) feature2 add a word
|/
* cf361f6 add a.txt

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

要修改上一条提交信息,可以使用git commit –amend命令。上一条的提交信息为fix config,但它其实是feature1分支的合并,解决合并时发生的冲突只是过程之一,这样标记是在不妥。于是,我们修改这条提交信息。

$ git commit -m 'merge feature1' --amend
[master dea2e9d] merge feature1
 Date: Sun Aug 27 21:24:11 2017 +0800
  • 1
  • 2
  • 3
  • 4

再次查看日志,可以发现其变成了

$ git log --graph --pretty=oneline --abbrev-commit
*   dea2e9d (HEAD -> master) merge feature1
|\
| * f56a04d (feature1) feature1 add a word
* |   1387cb8 Merge branch 'feature2'
|\ \
| |/
|/|
| * 14ef937 (feature2) feature2 add a word
|/
* cf361f6 add a.txt

Git如何解决冲突的更多相关文章

  1. git 提交解决冲突(转载)

    转载 git 提交解决冲突 http://www.cnblogs.com/qinbb/p/5972308.html   一:git命令在提交代码前,没有pull拉最新的代码,因此再次提交出现了冲突. ...

  2. git如何解决冲突(代码托管在coding)

    分支A提交合并请求到分支B,有冲突 git fetch code 拉取远程仓库的其他分支代码(我拉代码是remote add code所以这里是code,可以用git remote查看) git ch ...

  3. git之解决冲突

    前面几次使用git,一直对于冲突的这个问题不是很理解,感觉有些时候就会产生冲突,在此记录一下解决冲突的流程 1.git bash上面冲突显示 2.在idea上面可以看到冲突的文件 3.去解决冲突 4. ...

  4. git 提交解决冲突

    一:git命令在提交代码前,没有pull拉最新的代码,因此再次提交出现了冲突. error: You have not concluded your merge (MERGE_HEAD exists) ...

  5. git如何解决冲突(master分支的上的冲突--太岁头上动土)

    欢迎加入前端交流群交流知识&&获取视频资料:749539640 git是什么就不废话了,详情点击 出现以下情况怎么解决? 有个index.ts文件 export const ENV = ...

  6. git pull解决冲突

    git报错:Please commit your changes or stash them before you merge. 解决:1.不需要保留本地修改的话,直接将有冲突的文件还原再pull:g ...

  7. git学习(十一) idea git pull 解决冲突

    测试如下: 先将远程的代码修改,之后更新: 之后将工作区修改的代码(这里修改的代码跟远程修改的位置一样)提交到本地,之后拉取远程的代码,会发现有冲突: Accept Yours 就是直接选取本地的代码 ...

  8. git -- 如何解决冲突

    遇到冲突,首先要编辑冲突文件,可以使用vim或者其他工具,冲突文件变现为: <<<<HEAD 到 ==== :代表本地分支的修改内容 ==== 到 >>>&g ...

  9. Git 在解决冲突的时候文件覆盖

    更新代码导致被还原或覆盖的场景:1.触发冲突的必要条件是修改同一个文件且修改的位置非常近,否则Git会自动合并其内容避免更新代码导致被还原或覆盖的解决方案1.少修改的地方(生产环境.公网测试环境):推 ...

随机推荐

  1. CI框架源码学习笔记3——Log.php

    上一节说完了Common.php,然而跟代码打交道总是免不了日志记录,所以这一节我们说说Log.php文件. 先看看类里面的几个属性, protected $_log_path;  日志路径 prot ...

  2. P4196 [CQOI2006]凸多边形 半平面交

    \(\color{#0066ff}{题目描述}\) 逆时针给出n个凸多边形的顶点坐标,求它们交的面积.例如n=2时,两个凸多边形如下图: 则相交部分的面积为5.233. \(\color{#0066f ...

  3. CF862B Mahmoud and Ehab and the bipartiteness 二分图染色判定

    \(\color{#0066ff}{题目描述}\) 给出n个点,n-1条边,求再最多再添加多少边使得二分图的性质成立 \(\color{#0066ff}{输入格式}\) The first line ...

  4. P2117 小Z的矩阵

    题意: 给你一个初始01矩阵 三种操作 1.给一个x,把第x行01互换 2.给一个x,把第x列01互换 3.求$(\sum_{i=1}^n\sum_{j=1}^nf[i][j]*f[j][i])%2$ ...

  5. HDU6312 Game (多校第二场1004) 简单博弈

    Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. PHPExcel类库的使用

    首先下载PHPEXCEL 下载地址:https://github.com/PHPOffice/PHPExcel 一.生成Excel <?php require "PHPExcel-1. ...

  7. Kibana6.2.x 插件理解

    官方地址:https://www.elastic.co/guide/en/kibana/current/development-uiexports.html Type Purpose hacks An ...

  8. O - 听说下面都是裸题 (最小生成树模板题)

    Economic times these days are tough, even in Byteland. To reduce the operating costs, the government ...

  9. POJ2886Who Gets the Most Candies?(线段树之约瑟夫)

    约瑟夫问题的升级版,每次出去的是前一个出去的人位置+手上的数字(正往前,负往后).第i个出去的人拿的糖是i的约数的个数.求拿糖最多的人和他的糖果数. 这里用到了反素数的知识,在这直接打表 题目 AC代 ...

  10. Ansible故障

    常见问题一: [root@m01 ~]# ansible  -k 172.16.1.51 -m ping SSH password: [WARNING]: No hosts matched, noth ...