Git 拒绝 pull,本地有未提交的修改

问题描述

bash 复制代码
cxq@ubuntu:~/RPC/rpc/Rpc_Project$ git pull
Updating b67e13c..d0f3ee9
error: Your local changes to the following files would be overwritten by merge:
	Rpc_Project/source/CMakeLists.txt
Please commit your changes or stash them before you merge.
Aborting
cxq@ubuntu:~/RPC/rpc/Rpc_Project$ cd ..
cxq@ubuntu:~/RPC/rpc$ ks
ks: command not found
cxq@ubuntu:~/RPC/rpc$ git pull
Updating b67e13c..d0f3ee9
error: Your local changes to the following files would be overwritten by merge:
	Rpc_Project/source/CMakeLists.txt
Please commit your changes or stash them before you merge.
Aborting

解决方案

Git 拒绝 pull 是因为本地对 Rpc_Project/source/CMakeLists.txt 还有未提交的修改 ,而远程仓库同一文件也有更新,会冲突。

先把本地改动"暂存"或"提交"即可继续 pull。下面给你 3 种常用做法,按场景任选其一。


① 最快:暂存(stash)→ 拉取 → 弹回

bash 复制代码
# 1. 把当前所有改动压入栈
git stash push -m "WIP: adjust include path"

# 2. 拉远程最新代码
git pull

# 3. 把刚才的改动再弹出来(如有冲突会提示,手动解决)
git stash pop

适合临时调试,不想留提交记录。


② 正规:提交(commit)→ 拉取 → 如有冲突解决再提交

bash 复制代码
# 1. 加到暂存区
git add Rpc_Project/source/CMakeLists.txt
# 或一次性添加所有改动
git add .

# 2. 本地提交
git commit -m "fix: add missing include dir for muduo"

# 3. 现在 pull 会触发"merge"或"rebase",Git 自动合,合不了就提示冲突
git pull
# 若有冲突,手动编辑 → git add → git commit 再推

适合改动已测试完,要留历史。


③ 放弃本地改动(确定不要了)

bash 复制代码
git restore Rpc_Project/source/CMakeLists.txt   # 把文件还原成上次提交状态
git pull                                       # 再拉就干净

慎用,还原后本地修改会丢失。


30 秒自检

bash 复制代码
git status                  # 看哪些文件被改了
git diff CMakeLists.txt     # 看具体改了什么,再决定 stash / commit / 放弃

一句话

先 commit 或 stash 本地改动 → 再 git pull ,冲突提示就会消失;改完记得 git stash pop 或继续提交即可。

相关推荐
OsDepK5 小时前
OSMDE手机AI编程,一键Git
git·ai编程
用什么都重名11 小时前
Git分支合并与远程服务器同步实战:保留关键配置文件
运维·服务器·git
得要找到一束光11 小时前
git详细命令
git·github
2601_9618752413 小时前
花生十三公考课程|网课|视频
数据库·windows·git·svn·eclipse·github
EleganceJiaBao16 小时前
【Git】Git reset 完整指南:真正理解 HEAD、暂存区与工作区
git·github·reset
我最爱吃鱼香茄子18 小时前
【保姆级】Git配置用户名和邮箱(全局/局部)解决提交记录不识别、贡献图不亮问题
git·gitee·github·小白教程·git配置·程序员开发工具
weixin_5716674118 小时前
git fork 并建立自己的dev分支 保持与源仓库的联系
git
master33619 小时前
git仓库通过脚本完成多个远程仓库同步
大数据·git·elasticsearch
用什么都重名19 小时前
Git 合并两个无共同历史的分支:从报错到解决全记录
git·gitlab
2601_9618752419 小时前
花生十三资料网盘|百度云|下载
数据库·windows·git·svn·eclipse·github