起因:在家里台式更新了文件并同步到远程,而另外一台电脑在本地修改了文件没有提交到库中。
解决办法: 处理的方式非常简单,主要是使用git stash命令进行处理,分成以下几个步骤进行处理。 1. git stash将本地修改存储起来
git stash保存当前的工作进度,会分别对暂存区和工作区的状态进行保存 。用git stash list可以显示进度列表,也暗示了git stash可以多次保存工作进度,并在恢复的时候进行选择。 其中stash@{}这样标记的就是刚才保存的信息。WIP表示work in progress,就是工作区的意思。 2. git pull远程到本地
保存了本地进度之后,就可以pull了,git pull相当于git fetch和git merge两个操作。
git pull可以看到我的本地信息被远程的覆盖掉了。remote file changed是我在github上直接提交的commit。覆盖了我本地的另外一句话。
不用担心,因为我们保存了本地的工作进度,可以从stash list中恢复之前的信息。
3. 还原暂存区的内容
git stash pop stash@{1} or git stash apply stash@{1}此时,遇到了另外一个问题,提示:
fatal: ambiguous argument 'stash@': unknown revision or path not in the working tree.因为我使用的是powershell上操作,在SO上找到的答案:
Your shell is eating your curly brackets, so while you say stash@{1}, git sees stash@1 and that makes no sense to it. Quote the argument or reconfigure your shell to only expand curly brackets when there is a comma between them (zsh can be configured either way, bash only expands curly brackets with comma or range between them, other shells may behave one or other way).简而言之,windows上为了escape {}被吃掉,加上backtick `就好。
$ git stash apply stash@`{1`}系统可能提示如下类似的信息: 意思就是系统自动合并修改的内容,但是其中有冲突,需要解决其中的冲突。
4. 解决文件中冲突的的部分 直接打开文件冲突的文件。
其中Updated upstream 和=====之间的内容就是pull下来的内容,====和stashed changes之间的内容就是本地修改的内容。碰到这种情况,git也不知道哪行内容是需要的,所以要自行确定需要的内容。完成后再执行提交操作。 会用mergetool的也可以用这个工具去做。
5. 最后记得git stash clear删除进度列表,不然用的多了会stash list会变得很复杂。