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

相关推荐
何中应9 小时前
使用SSH地址拉取远程仓库代码报下面的错误
git
何中应9 小时前
Git本地仓库命令补充
git
sun00770011 小时前
执行repo sync -c -d -j4以后,提交未git push的代码看不到了。要怎么恢复?
git
胖虎114 小时前
Git 一个本地仓库同时推送到两个远程仓库(详细教程)
git·多远程仓库·双远程仓库·git双远程·git备份
春日见1 天前
如何创建一个PR
运维·开发语言·windows·git·docker·容器
stevenzqzq2 天前
git 常用操作
大数据·git
Curvatureflight2 天前
Git工作流最佳实践:从混乱到优雅
git
wu~9702 天前
GitHub永不遗忘,使用git push -f来覆盖的提交依旧保留
git·github
Vermouth_002 天前
git clone的时候提示access denied
git
qq_437657272 天前
清楚本地的git并重新登录
git