commit-your-changes-or-stash-them-before-you-can-merge

50次阅读

共计 577 个字符,预计需要花费 2 分钟才能阅读完成。

今天用 git pull 来更新代码,遇到了下面的问题:

error: Your local changes to the following files would be overwritten by merge:
    .gitignore
Please, commit your changes or stash them before you can merge.
Aborting

1. stash

通常遇到这个问题,你可以直接 commit 你的修改;

但我这次不想这样。

看看 git stash 是如何做的。

git stash
git pull
git stash pop

接下来 diff 一下此文件看看自动合并的情况,并作出相应修改。

git stash: 备份当前的工作区的内容,从最近的一次提交中读取相关内容,让工作区保证和上次提交的内容一致。同时,将当前的工作区内容保存到 Git 栈中。

git stash pop: 从 Git 栈中读取最近一次保存的内容,恢复工作区的相关内容。由于可能存在多个 Stash 的内容,所以用栈来管理,pop 会从最近的一个 stash 中读取内容并恢复。

git stash list: 显示 Git 栈内的所有备份,可以利用这个列表来决定从那个地方恢复。

git stash clear: 清空 Git 栈。此时使用 gitg 等图形化工具会发现,原来 stash 的哪些节点都消失了。

2. 放弃本地修改,直接覆盖之

git reset --hard
git pull

正文完
 0