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

相关推荐
fengyehongWorld5 小时前
Git 认证凭据管理
git
暮云星影13 小时前
git版本发布
git·gitee·github·gitea
乌夷13 小时前
Git Flow 分支模型
git
独隅14 小时前
VS Code Git 工作树多分支并行开发实战评测
git
暮云星影19 小时前
git仓库分支管理
git·gitee·github·gitea
互联网中的一颗神经元2 天前
ZZ — Git 速查表
大数据·git·elasticsearch
早安试言2 天前
git使用
git
燐妤2 天前
梦绘片场 ProjectDream故事总纲场景剧本篇:从项目到成片的 AI 漫剧工作台
人工智能·git·ai·项目开发
JackieDYH2 天前
Git 与 GitHub 新电脑配置指南(从零开始)
git·github·工具·使用教程
Jeanyia3 天前
GitLab 配套 Git 常用命令
git·gitlab