Contents

git pull 冲突解决

Contents

git菜鸟应该会经常遇到的问题, 就像博主这样。服务器上pull更新时遇到冲突。主要是环境配置文件冲突,由于从SVN转到git,所以保留了SVN那种部署代码的方式,git有更好的部署方法flow,可惜还不会使用。配置文件分为本地和线上。有时候为了方便直接上服务器上修改配置文件,然后在本地完成一个新功能的时候,push到仓库里。再在服务器上拉取更新。就会爆冲突。这时候,索引的版本号会变为最新。但是更新的代码并没有实际合并到当前分支,再次尝试拉取的时候会提示已是最新版本。类似如下:

[root@smsms]$ git pull origin master:master
remote: Counting objects: 680, done.
remote: Compressing objects: 100% (517/517), done.
remote: Total 680 (delta 166), reused 664 (delta 156)
Receiving objects: 100% (680/680), 11.17 MiB | 22.13 MiB/s, done.
Resolving deltas: 100% (166/166), completed with 7 local objects.
From https://gitlab.oneit.com/mei/smsms
   d1cfc68..9e7ba97  master     -> master
Warning: fetch updated the current branch head.
Warning: fast-forwarding your working tree from
Warning: commit d1cfc689545cdf70a9e29254d4473fcb7fe66efc.
error: Entry 'inc/global.php' would be overwritten by merge. Cannot merge.
Cannot fast-forward your working tree.
After making sure that you saved anything precious from
$ git diff d1cfc689545cdf70a9e29254d4473fcb7fe66efc
output, run
$ git reset --hard
to recover.
[root@smsms]$ git pull origin master:master
Already up-to-date.


error: Entry 'inc/global.php' would be overwritten by merge. Cannot merge.  
Cannot fast-forward your working tree.

有冲突,不能合并。 再次拉取显示已是最新。

解决此问题,根据提示使用git reset 还原pull,但是博主还不是很会用这个reset命令,踩了不少坑。

reset

git reset --hard 还原所有更改到某一个版本,后面指定一个版本号。

git reset 还有其他的参数,详细的可以看帮助文档 git help reset

这里博主首先使用 git log 看一下日志。

[root@smsms]$ git log
commit 9e7ba97035f4330d84d3e15b0b7ccf62acd9deda
Author: mei
Date:   Tue Sep 13 10:29:42 2016 +0800

    js计算短信数量

commit 111c53878e3479f82a96fd925857d6c5d15134b2
Author: mei
Date:   Mon Sep 12 18:18:26 2016 +0800

    发送页面发送逻辑实现

commit b9afaa0f0959edddf7357605bbbd8972fb8aee27
Author: [email protected]
Date:   Mon Sep 12 10:21:54 2016 +0800

    function.php中关于excel文件的操作函数

根据日志选择还原到某一个版本号(即为commit后面的字符串):如下

[root@smsms]$ git reset --hard d1cfc689545cdf70a9e29254d4473fcb7fe66efc
HEAD is now at d1cfc68 员工登出

stash

还原成功后看一下状态 git status

[root@smsms]$ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	modified:   inc/global.php
#	modified:   login.php
#	modified:   nav.php
#
no changes added to commit (use "git add" and/or "git commit -a")

可以看到有修改未提交的文件。这时为了能够成功的拉取最新的代码,所以要使用git stash暂存一下修改,如下:

[root@smsms]$ git stash
Saved working directory and index state WIP on master: d1cfc68 员工登出
HEAD is now at d1cfc68 员工登出

重新拉取更新,git pull origin master:master

拉取完成后将暂存的文件恢复,如下:

[root@smsms]$ git stash pop
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	modified:   inc/global.php
#	modified:   login.php
#	modified:   nav.php
#
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (a796e0f92691e78b37dbfe9be173629f7b962c84)