2019-06-18 不同人修改了同一个文件的同一个区域如何处理

[root@VM_0_3_centos test2]# vim f2
f2
dddd
fff hello world
[root@VM_0_3_centos test2]# git commit -am "add hello world f2"
[root@VM_0_3_centos test2]# git push  #第一个人修改了f2文件后提交到远程
[root@VM_0_3_centos test]# vim f2  #第二个人没有git pull就修改相同的文件的相同位置
f2
dddd
fff hello
[root@VM_0_3_centos test]# git commit -am "add f2 hello"
[root@VM_0_3_centos test]# git push  #会报错如下
warning: push.default is unset; its implicit value is changing in
  t 2.0 from 'matching' to 'simple'. To squelch this message
▽d maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

To git@github.com:qilinzhicai/test.git
 ! [rejected]        分支2 -> 分支2 (fetch first)
error: failed to push some refs to 'git@github.com:qilinzhicai/test.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 merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details
[root@VM_0_3_centos test]# git pull  #拉取远程分支并和本地分支进行合并
remote: Enumerating objects: 15, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 9 (delta 1), reused 9 (delta 1), pack-reused 0
Unpacking objects: 100% (9/9), done
From github.com:qilinzhicai/test
   d7d0dec..787c79e  分支2    -> origin/分支2
Auto-merging f2
CONFLICT (content): Merge conflict in f2  #发生冲突
Automatic merge failed; fix conflicts and then commit the result
[root@VM_0_3_centos test]# vim f2  #打开文件发现是两个人修改了同一行
f2 
dddd
<<<<<<< HEAD
fff hello
=======
fff hello world
>>>>>>> 787c79e628329f3b1a4b95e847aee354797640bb
如果两个人的修改都保留,则去掉多余的行后如下
f2
dddd
fff hello
fff hello world
[root@VM_0_3_centos test]# git commit -am "add hello and hello world f2"  #提交
[分支2 eb12a34] add hello and hello world f2
[root@VM_0_3_centos test]# git status
# On branch 分支2
# Your branch is ahead of 'origin/分支2' by 2 commits.
#   (use "git push" to publish your local commits)
#
nothing to commit, working directory clean
[root@VM_0_3_centos test]# git push  #同步到远程

总结:如果不同人修改了不同文件或者相同文件的不同位置或者一个人修改了文件名一个人修改了文件内容,都可以用git pull解决git push报错,但如果修改了相同文件的相同位置,需要先git pull,但会发生冲突。此时需要修改文件后进行git add和git commit操作。