记录一下在项目里使用git遇到代码冲突时的解决方法

问题:当我和我同事两个人改了相同的一个文件,他在我提交前提交了,这时候我就提交不了了,并且也pull不下来他的代码

会报错:

Your local changes to the following files would be overwritten by merge:

解决方法一:git checkout还原然后再pull(就是覆盖更新的意思)

解决方法二:先add 再commit 最后pull 就会在本地合并你的代码,最后检查没问题再push

------------------------------------------------------------------------------------------------------------------------

下面是搜到的一篇比较详细的git解决冲突文章分享给大家↓

假设冲突文件是 test/TestCase.php

下面分5种情况讨论。

1、本地不变。 
  然后远程别人有更新。 
  git pull 
  这种最简单,没有冲突,本地工作区直接更新 
  
2、我本地修改,但是不add。 
然后远程别人有更新,此时 :     
git pull, 
git会告诉你: 
error: Your local changes to the following files would be overwritten by merge: 
        tests/TestCase.php 
  此时,我  
   git checkout -- tests/TestCase.php 
  千万注意,上条命令会导致你自己的修改丢失了!! 
  然后git pull.成功。 
  这种情况下,你自己的修改完全丢失。本地接受了远程的修改。 
  
3、我本地修改,但是已经add了。 
  然后别人远程更新, 
  git pull 
  这种情况,和上面几乎一样。 
  git reset HEAD test/TestCase.php  
  上面这条命令可以理解为add命令的逆命令。即,取消add操作。 
  
  然后继续 
  git checkout -- tests/TestCase.php 
  千万注意,上条命令会导致你自己的修改丢失了!! 
  然后git pull.成功。 
  这种情况下,你自己的修改完全丢失。 本地接受了远程的修改。

4、我本地修改,add 且 commit了 
  然后别人远程更新, 
  git pull 
  注意:这种情况和第2,第3种情况不同。git会认为你的commit也很重要。同等重要。 
突然发现,自动进入vi界面。 
Merge branch 'master' of https://github.com/xxxx 
这什么意思呢?就是git会主动再帮你添加一个commit。 
先让你写点注释,并且它已经帮你写了几句话 
那自己写点东西,然后保存退出vi 
请注意,这个commit是在本地的,还没有发到远程。 
这个commit会自动合并 你添加到代码,和 别人远程更新的代码, 
所以,之后请自己检查一下这个代码有无问题。

如果都没有问题,则你现在 
  git push 
  这个命令可以把你的本地改动推送到远程。 
  push之后,远程得到你和他人的所有更新。push之前,仅本地得到你和他人的所有更新。 
  
5、使用分支功能。 
  git的分支功能很强大,应该多使用。 
  上面几种情况都没有使用分支。使用分支会更好。 
  首先,我本地新建并切换分支branch_1,自己起有意义名字,并修改文件。 
  git checkout -b branch_1 
    
  vi test/TestCase.php 
  git add tests/TestCase.php 
  git commit -m "change test" 
  
  现在,别人已经更新了远程主分支代码(没更新就极其方便了,也不必说了,快速更新) 
  我想把分支合并到主分支。 
  我应该,先回到主分支,并更新。 
  git  checkout master 
  git pull 
  
  然后,在本地,把我的修改分支合并上去。 
  git merge branch_1 
  自动进入vi界面,让你写注释。 
  
  然后会合并好。 
  类似提示: 
  tests/TestCase.php | 3 ++- 
  上面,两个加号表示文件增加了两行,一个减号表示文件减少了一行。 
  
  此时,注意,凡是进入vi界面后, 
  最好自己再检查一下程序,检查那些被提示修改过的文件,(这是一个好习惯,但我从来不遵守) 
  如果认为正确,最后, 
  git push,推送到远程。 
  
  push之后,远程得到你和他人的所有更新。push之前,仅本地得到你和他人的所有更新。

========== 
总结,大部分情况下,都推荐使用第5种,使用分支的方法,来管理文件修改冲突。

转自:https://www.iteye.com/blog/xieye-2433229

git文件冲突合并的报错:Your local changes to the following files would be overwritten by merge的更多相关文章

  1. 【Git】pull遇到错误:error: Your local changes to the following files would be overwritten by merge:

    这种情况下,如何保留本地的修改同时又把远程的合并过来呢? 首先取决于你是否想要保存本地修改.(是 /否) 是 git stash git pull origin master git stash po ...

  2. Git出现error: Your local changes to the following files would be overwritten by merge: ... Please, commit your changes or stash them before you can merge.的问题解决(Git代码冲突)

    在使用git pull拉取服务器最新版本时,如果出现error: Your local changes to the following files would be overwritten by m ...

  3. Laravel 5.2--git冲突error: Your local changes to the following files would be overwritten by merge:

    今天在服务器上git pull是出现以下错误: error: Your local changes to the following files would be overwritten by mer ...

  4. 解决git pull出现: Your local changes to the following files would be overwritten by merge: ...的问题

    今天在服务器上git pull是出现以下错误: error: Your local changes to the following files would be overwritten by mer ...

  5. git error: Your local changes to the following files would be overwritten by merge:xxxxxx ,Please commit your changes or stash them before you merge.的phpstorm解决办法

    git报错 error: Your local changes to the following files would be overwritten by merge: .idea/encoding ...

  6. "Your local changes to the following files would be overwritten by merge" on git

    运行: git merge --ff origin/master 得到错误信息: error: Your local changes to the following files would be o ...

  7. Git版本控制工具使用:Error pulling origin: error: Your local changes to the following files would be overwritten by merge

    摘自: CSDN 逆觞 git在pull时,出现这种错误的时候,可能很多人进进行stash,相关stash的请看:Error pulling origin: error: Your local cha ...

  8. git pull 提示错误,Your local changes to the following files would be overwritten by merge

    error: Your local changes to the following files would be overwritten by merge: Please commit your c ...

  9. git pull的时候发生冲突的解决方法之“error: Your local changes to the following files would be overwritten by merge”

    今天在使用git pull 命令的时候发生了以下报错 目前git的报错提示已经相关友好了,可以直观的发现,这里可以通过commit的方式解决这个冲突问题,但还是想看看其他大佬是怎么解决这类问题的 在网 ...

随机推荐

  1. Android之SimpleAdapter简单实例和SimpleAdapter参数说明

    SimpleAdapter基本上认知了其参数含义 用起来就简单多了 SimpleAdapter的参数说明 第一个参数 表示访问整个android应用程序接口,基本上所有的组件都需要  第二个参数表示生 ...

  2. Java使用反射实现根据字符串类名及参数创建对象

    要根据字符串创建对象,可以使用 Class.forName(String) 方法: 而要新建一个可以指定初始值参数的对象,就必须得使用 getConstructor(Class<T>... ...

  3. java中list的sort()功能如何使用?

    排序时正序/倒序处理起来可能会混淆,可以用更简单的方法.可以使用java.util自带的比较器来做 Comparator.comparingInt(Integer::intValue).reverse ...

  4. JMeter接口测试-接口签名校验

    前言 很多HTTP接口在传参时,需要先对接口的参数进行数据签名加密 如pinter项目的中的签名接口 http://localhost:8080/pinter/com/userInfo 参数为: {& ...

  5. [ERROR]pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]SQL Server 阻止了对组件“xp_cmdshell”的 过程“sys.xp_cmdshell”的访问

    环境: Windows 2012 R2 SQL Server 2014 通过MSSQL查询数据库服务器时间,报错如下: pyodbc.ProgrammingError: (', '[42000] [M ...

  6. mqttnet3.0用法

    .net常用的mqtt类库有2个,m2mqtt和mqttnet两个类库 当然了,这两个库的教程网上一搜一大把 但mqttnet搜到的教程全是2.7及以下版本的,但3.0版语法却不再兼容,升级版本会导致 ...

  7. 如何在SQL Server中生成和使用CRUD存储过程

    在本文中,请参阅如何在SQL Server中生成和使用CRUD存储过程. 大多数数据库系统基于缩写CRUD调用的最简单的4种数据操作操作进行操作. 此首字母缩写词代表CREATE,READ,UPDAT ...

  8. Java日志介绍(3)-Logback

    Logback 继承自Log4j,它建立在有十年工业经验的日志系统之上.它比其它所有的日志系统更快并且更小,包含了许多独特并且有用的特性. 1.配置 1.1.加载配置 Logback能够在初始化期间自 ...

  9. hive中parquet存储格式数据类型timestamp的问题

    当存储格式为parquet 且 字段类型为 timestamp 且 数据用hive执行sql写入. 这样的字段在使用impala读取时会少8小时.建议存储为sequence格式或者将字段类型设置为st ...

  10. abp集成IdentityServer4和单点登录

    在abp开发的系统后,需要使用这个系统作单点登录,及其他项目登录账号依靠abp开发的系统.在官方文档上只找到作为登录服务Identity Server Integration,但是host项目却无法使 ...