当你执行git push
命令时,如果报错:
error: failed to push some refs to ‘https://github.com/Lifeier/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 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.
该报错是提示你和别人的代码提交产生了冲突,
可以用如下操作解决:
1、执行git pull
命令
这里提示了冲突的文件是哪一个,并自动合并了冲突文件。我们这里冲突的文件是a2.txt。
2、此时可以用git status
命令来查看当前的状态和操作提示
提示你可以有两种方式操作:
(1)你可以先解决这个冲突,然后再提交(后面我们用这种方式解决)
(2)或者你可以不再接收远程仓库里的变化
3、打开冲突文件,你会发现:
冲突文件里有如下的结构:
<<<<<<< HEAD
。。。这一块是你和对方冲突的代码(自己的代码)
=======
。。。这一块是对方和你冲突的代码(对方的代码)
>>>>>>> 。。。
4、查看冲突文件里的代码后,可以和对方商量修改代码,然后删掉多余的<<< 、=== 、 >>>
,然后将这个修改后的文件加入暂存区、提交到本地仓库、发送到远程仓库就可以了。
例如:a2.txt文件中的代码经过商量后发现这两段代码都有用,所以直接删除掉<<< 、=== 、 >>>
,将a2.txt文件修改为:
然后将修改后的a2,txt文件执行下列操作:
加入暂存区、提交到本地仓库、发送到远程仓库:
会发现git push
成功执行。
视频教程:https://www.bilibili.com/video/BV1n441157Zt?p=4