美文网首页效率与工具
git commit后,push时的冲突解决

git commit后,push时的冲突解决

作者: 89ae7180db04 | 来源:发表于2017-08-14 16:41 被阅读4224次

在使用git进行commit之后,提交代码进行push的时候,可能会遇到下面的问题:

To git@git.****.com:****/djangoProject.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@git.****.com:****/djangoProject.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

出现这个问题的原因,是因为在本地commit完了之后,远端仓库上有别人提交了代码,进行了更新。

要解决这个问题,需要首先更新自己的本地代码。

运行

git pull

结果分为两种情况:

没有冲突

终端会显示:

remote: Counting objects: 4, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 3), reused 0 (delta 0)
Unpacking objects: 100% (4/4), done.
From git.sami.int.****.com:****/djangoProject
   33d5978..a41fda9  master     -> origin/master
Merge made by the 'recursive' strategy.
 dssWebPortal/views.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

表示git自动进行了merge,接下来再运行

git push

结果显示:

remote: Counting objects: 4, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 3), reused 0 (delta 0)
Unpacking objects: 100% (4/4), done.
From git.***.com:***/djangoProject
   33d5978..a41fda9  master     -> origin/master
Merge made by the 'recursive' strategy.
 dssWebPortal/views.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

提交代码成功。

有冲突

终端会显示:

Auto-merging dssWebPortal/tests.py
CONFLICT (content): Merge conflict in dssWebPortal/tests.py
Removing dssWebPortal/static/resources/PFExcel/p1.txt
Automatic merge failed; fix conflicts and then commit the result.

git尝试去进行merge,但是检测到了不能自动解决的冲突,需要用户手动merge,稍微复杂了一点。

先运行

git status

来查看当前文件状态

C:\Users\*****\www\django\mysite>git status
On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 7 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)
You have unmerged paths.
  (fix conflicts and run "git commit")

Changes to be committed:

        modified:   dssWebPortal/dssTools.py
        modified:   dssWebPortal/static/localjs/p10_factmaps_table.js
        modified:   dssWebPortal/static/localjs/p1_pffields.js
        modified:   dssWebPortal/static/localjs/p6_validaterule.js
        modified:   dssWebPortal/static/localjs/p6_validaterule_table.js
        deleted:    dssWebPortal/static/resources/PFExcel/p1.txt
        modified:   
        ....

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:   dssWebPortal/tests.py

这里要引起注意,本地只修该了 tests.py,要解决的冲突也是这个文件。但是,与本地不一致的文件(由其他用户修改), 由于有一个冲突文件的存在,显示为changes to be commited。

这个意思也就是说,远端仓库有多个文件的修改,在执行完了pull之后,如果有若干个文件跟本地冲突,那么,有冲突的文件会显示为

Unmerged paths:
      (use "git add <file>..." to mark resolution)
    
            both modified:   dssWebPortal/tests.py

而其他的文件则会是:

 Changes to be committed:
    
            modified:   dssWebPortal/dssTools.py
            modified:   dssWebPortal/static/localjs/common/leftMenus.js
            modified:   dssWebPortal/static/localjs/dssfield.js
            ...

用户接下来需要做的操作是:

  • 解决冲突,手动merge test.py文件

  • 运行 git add test.py, 把解决冲突后的文件add进来, 然后运行git status,显示

       Changes to be committed:
    
          modified:   dssWebPortal/dssTools.py
          modified:   dssWebPortal/static/localjs/common/leftMenus.js
          modified:   dssWebPortal/static/localjs/dssfield.js
    
  • 把所有的这些文件进行 commit。

  • git push

  • 遇到问题的话,再重复之前的步骤

切记,解决冲突文件后,不可以用本地的文件去覆盖没有冲突的文件, 不然就会导致远端仓库的代码丢失。

因为commit了两次,所以在gitlab上看到两条记录:

gitlab.PNG

从Graph里看到是这个样子:

graph.PNG

之所以会这样,是因为,本地用户在

add select version page, p2, p6 p10

这个状态时修改了代码,进行了commit为

update unit test

但是在还没有提交的时候,有其他用户向仓库进行了

url of p1

的提交。为了让代码进行一致,本地用户进行了以上的操作后,再次commit, 在gitlab上显示为一次 Merge

Merge branch 'master' of git.****.com:****/djangoProject

相关文章

网友评论

    本文标题:git commit后,push时的冲突解决

    本文链接:https://www.haomeiwen.com/subject/hfwurxtx.html