0%

git pull远程仓库代码冲突

常用git与远程仓库进行交互, 难免会遇到冲突的问题. 现在来了解一下, 当出现这种情况时, 该要如何处理.

起始

代码冲突, 无非就是同时有两个人在改动同一个文件所造成的冲突. 当你使用git pull后, git会提示你:

1
2
3
4
5
6
$ git pull
error: Your local changes to the following files would be overwritten by merge:
src/components/page/file.vue
Please commit your changes or stash them before you merge.
Aborting
Updating 8e191be..c301cdc

这时候你可以使用git stash将本地修改存储起来. 使用git status查看, 是不是干净啦~

1
2
3
4
5
6
$ git stash
Saved working directory and index state WIP on master: 8e191be Merge branch 'master' of https://github.com/anran758/test
HEAD is now at 8e191be Merge branch 'master' of https://github.com/anran758/test
$ git status
# On branch master
nothing to commit, working directory clean

这时再使用将远程仓库的代码pull下来

1
2
3
4
5
6
  $ git pull
Updating 8e191be..c301cdc
Fast-forward
src/base/fonts/iconfont.eot | Bin 7320 -> 7548 bytes
src/components/page/file.vue | 10 +-
create mode 100644 static/font_503074_8sfhbdjlucjtt9/iconfont.eot

现在要查看现有的储藏, 可以使用git stash list

1
2
$ git stash list
stash@{0}: WIP on master: 8e191be Merge branch 'master' of https://github.com/anran758/test

这时我们还原储藏的内容, 使用命令git stash pop 'stash@{0}'. 注意这里的要使用引号'stash@{0}', 因为shell正在使用扩展里的内容, 否则会报一个unknown option: -encodedCommand

1
2
3
4
5
6
7
8
9
10
11
$ git stash pop 'stash@{0}'
Auto-merging src/components/page/file.vue
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

modified: src/components/page/film.vue

no changes added to commit (use "git add" and/or "git commit -a")

之后就可以使用git diff或者利用图形工具, 如Sourcetree(可视化git), vscode编辑器上对比代码, 手动解决冲突即可.

「请笔者喝杯奶茶鼓励一下」

欢迎关注我的其它发布渠道