git pull: Please commit your changes or stash them before you merge

当执行`git pull`时遇到`Please commit your changes or stash them before you merge.`错误,可以采取两种策略:1) 通过`git stash`保存本地更改,然后`git pull`,最后`git stash pop`恢复更改;2) 使用`git reset --hard`放弃本地更改并直接拉取最新代码。本文详细介绍了这两个解决方案的步骤。

参考

Git冲突:commit your changes or stash them before you can merge.

总结

如果git pull 报错 Please commit your changes or stash them before you merge.

如果需要保留自己本地的修改,就依次执行以下命令即可

git status

git stash

git pull

git status

git stash pop

如果放弃本地修改,也直接覆盖

git reset --hard

git pull

解释以下命令stash

执行 git pull 命令,报错如下

# git pull

error: Your local changes to the following files would be overwritten by merge:
        README.md
Please commit your changes or stash them before you merge.
Aborting
Updating ade5dfc..518b768

1.查看状态(可省略)

# git status

On branch yourbranchname
Your branch is behind 'origin/yourbranchname' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)

2.执行 git stash 命令

# git stash

Saved working directory and index state WIP on yourbranchname: ade5dfc one edit function(commit comment)

备份当前的工作区的内容,从最近的一次提交中读取相关内容,让工作区保证和上次提交的内容一致。同时,将当前的工作区内容保存到Git栈中。就是把自己本地的修改stash,存储起来,该命令执行后,项目就是修改前的了。

3.执行 git pull 命令

# git pull

Updating ade5dfc..518b768
Fast-forward
 README.md | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

git pull 更新代码

4.执行 git status 命令,再次查看状态(可省略)

# git status

On branch yourbranchname
Your branch is up to date with 'origin/yourbranchname'.

5.执行 git stash pop 命令

# git stash pop


Auto-merging README.md
On branch yourbranchname
Your branch is up to date with 'origin/yourbranchname'.

从Git栈中读取最近一次保存的内容,恢复工作区的相关内容。由于可能存在多个Stash的内容,所以用栈来管理,pop会从最近的一个stash中读取内容并恢复。git stash pop命令就是取出自己本地的修改

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值