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

相关推荐
程序员沉梦听雨1 小时前
【Git】git-submodule子模块管理
git
全栈陈序员1 天前
Git】从入门到精通(八)—— git log:查看提交历史
大数据·git·elasticsearch
7177771 天前
Git自建用什么工具比较好:Gitea、Forgejo、GitLab CE与Gitee的选型对比
git·gitlab·gitea
lsc_blog2 天前
年底还在招的岗位,先确认缺口和到岗要求
git·缓存·ci/cd·职场和发展·github
全栈陈序员2 天前
【Git】从入门到精通(十一)—— git reset 三种模式详解
git·elasticsearch
全栈陈序员2 天前
【Git】从入门到精通(十)—— 撤销与恢复:git checkout与git restore
运维·网络·数据库·git
全栈陈序员3 天前
【Git】从入门到精通(五)—— git add:将文件加入暂存区
大数据·git·elasticsearch
全栈陈序员3 天前
【Git】从入门到精通(七)—— git status:查看仓库状态
大数据·git·elasticsearch
莫晓燚3 天前
Ansible 13 个常用模块---企业实战
git·github
无言听风雨丶3 天前
Kubernetes GitOps 持续交付实战:用 Argo CD 让 Git 成为集群事实源
git·elasticsearch·kubernetes