参考
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命令就是取出自己本地的修改