Please commit your changes or stash them before you merge问题解决

问题描述

error: Your local changes to the following files would be overwritten by merge:
    xxx/xxx/xxx.c
Please commit your changes or stash them before you can merge.
Aborting

问题分析

原因是 其他人/或你自己 修改了xxx.c并提交(git add .)到版本库中去了,你本地又想再提交xxx.c,
这时候你进行git pull操作就好出现冲突了,解决方法,在上面的提示中也说的很明确了。进行git commit或者git stash操作。

解决办法

1、保留本地的修改 的改法

  1)直接git commit本地的修改 ——一般不推荐这种方法,因为可能你还想修改该文件

  2)通过git stash——通常使用该方法

git stash
git pull
git stash pop

步骤解析:

git stash: 备份当前的工作区的内容,从最近的一次提交中读取相关内容,让工作区保证和上次提交的内容一致。同时,将当前的工作区内容保存到Git栈中。

git pull 或者 git pull <remote>  <branch>:拉取代码

git stash pop: 从Git栈中读取最近一次保存的内容,恢复工作区的相关内容。由于可能存在多个Stash的内容,所以用栈来管理,pop会从最近的一个stash中读取内容并恢复。

此外还有:

git stash list: 显示Git栈内的所有备份,可以利用这个列表来决定从那个地方恢复。

git stash clear: 清空Git栈。此时使用gitg等图形化工具会发现,原来stash的哪些节点都消失了。

2、放弃本地修改 的改法  ——这种方法会丢弃本地修改的代码,而且不可找回

git reset --hard
git pull

Over....

原文地址:https://www.cnblogs.com/gjmhome/p/14061192.html