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 或继续提交即可。

相关推荐
程序员鱼皮3 小时前
Git WorkTree 是什么?凭什么能让 AI 编程效率翻倍?
git·ai·程序员·编程·ai编程
懵逼的小黑子5 小时前
git与远程仓库创建连接
git
前端若水5 小时前
Git 撤销与恢复完全指南(超级详细版)
大数据·git·elasticsearch
golang学习记6 小时前
Git 2.54 来了,这个新命令让我终于敢重写历史了
git·后端
其实防守也摸鱼6 小时前
AWVS下载和安装保姆级教程
linux·服务器·git
前端若水6 小时前
Git 可以做的所有操作(完整分类)
大数据·git·elasticsearch
叹一曲当时只道是寻常6 小时前
Reference 工具安装与使用教程:一条命令管理 Git 仓库引用与知识沉淀
人工智能·git·ai·开源·github
前端若水7 小时前
Git 仓库管理命令完全指南(超级详细版)
大数据·git·elasticsearch
小MarkK7 小时前
[Doc]git的使用之不上传服务器,只在本地管理
git
YoseZang18 小时前
【手工】git的使用 – 使用git进行项目协作
git