yxsailing 发表于 2018-1-12 23:26:06

【Git】git使用

冲突的解决
  (如果git使用不熟练)建议在push不了时,pull之前。在本地创建一个新的分支并commit到local,以保证本地有commit记录,万一出什么问题,可以找回代码,以免代码丢失。
  (更甚者,把整个文件夹备份,不然出现找不回代码那就开心了)
  多人开发时Git下冲突的产生和解决
演示
  项目中有一个文件test.txt,其内容为(以下是在github仓库中截得文件内容):
https://images2015.cnblogs.com/blog/1025273/201704/1025273-20170412220514673-799904106.png
  1、保证项目的正确性,先pull到最新版本。
  2、修改local的test.txt的文件内容,修改后的内容是:
https://images2015.cnblogs.com/blog/1025273/201704/1025273-20170412220515751-752439346.png
  然后local查看状态,及commit到本地仓库。
https://images2015.cnblogs.com/blog/1025273/201704/1025273-20170412220517892-1846831260.png
  3、 (再1之后),修改test.txt,并push到remote。
  (以下是我直接在github的仓库编辑提交的。)
https://images2015.cnblogs.com/blog/1025273/201704/1025273-20170412220520923-946096534.png
  查看remote的commit log,可以发现有一次新的提交。
https://images2015.cnblogs.com/blog/1025273/201704/1025273-20170412220522501-1719185967.png
  这样就造成了冲突,因为local的test.txt版本与服务器的版本不一致。
  4、push本地的commit,发现无法push。
https://images2015.cnblogs.com/blog/1025273/201704/1025273-20170412220524017-673485358.png
  发现需要先 git pull,于是先更新。
https://images2015.cnblogs.com/blog/1025273/201704/1025273-20170412220525376-182570056.png
  pull后,给出了明确的错误提示”Automatic merge failed; fix conflicts and then commit the result.”。
  此时查看test.txt的内容:
https://images2015.cnblogs.com/blog/1025273/201704/1025273-20170412220526673-1159991671.png
  其中<<<<<<< HEAD 到 ======= 中间的内容是local提交的。
  ======= 到 >>>>>>> commit-id 是远程仓库中的内容。
  (和svn类似。)
  如何解决冲突? 删除这些注释,保证test.txt的内容是最终push版本的内容。
  修改冲突后的test.txt内容:
https://images2015.cnblogs.com/blog/1025273/201704/1025273-20170412220528455-554813689.png
  特别,在eclipse中即使是解决了冲突,文件的冲突图标还在,但并不影响commit&push。如下:
https://images2015.cnblogs.com/blog/1025273/201704/1025273-20170412220529986-1860461665.png
  5、冲突解决后,在git status可以看到当前的文件状态:
  (特别需要注意的是当前分支会处在一个MERGING状态下,以及刚才处理的冲突文件test.txt处于Unmerged paths下。)
https://images2015.cnblogs.com/blog/1025273/201704/1025273-20170412220532017-1495476709.png
  根据提示,用git add test.txt,然后在git commit和git push。
https://images2015.cnblogs.com/blog/1025273/201704/1025273-20170412220534142-372511008.png
https://images2015.cnblogs.com/blog/1025273/201704/1025273-20170412220536689-959158585.png
  此时去看remote中的test.txt内容和commit log:
https://images2015.cnblogs.com/blog/1025273/201704/1025273-20170412220538048-496688166.png
https://images2015.cnblogs.com/blog/1025273/201704/1025273-20170412220540533-995575924.png
页: [1]
查看完整版本: 【Git】git使用