新文件冲突
假设有client1和client2两个客户端都第一次从远程仓库克隆下来,现在client1里面加个文件1.cpp提交
#include <stdint>
$ cd client1
$ git add .
$ git commit -m '1.cpp commit'
$ git push
回到client2,假如client2并不知道client1提交了1个1.cpp,现在client2添加了个2.cpp提交
#include <stdint2>
$ cd client2
$ git add .
$ git commit -m '2.cpp commit'
$ git push
! [rejected]
error: failed to push some refs to 'xxx'
hint: Updates were rejected because the remote contains work that you do not have locally...
在执行push的时候会出现错误,也就是本地版本不是最新的,因此先拉取远程分支进行合并
$ git pull
$ git merge
fatal: refusing to merge unrelated histories
这个时候拒绝合并,git认为本地和远程是两个独立仓库不能直接合并历史,这问题第一次提交冲突时会出现,下次提交不会出现了,这时使用–allow-unrelated-histories命令合并
$ git merge --allow-unrelated-histories
这时候应该会弹出vim编辑的界面让你提交合并日志,保存关闭后直接push本次提交,使用log命令可以看到3次提交记录,最新一次提交为merge
$ git push
$ git log
回到client1使用pull命令可以将client2添加的2.cpp拉取下来
$ cd client1
$ git pull
...
Updating 2fdcd60..c94510e
Fast-forward
2.cpp | 1 +
1 file changed, 1 insertion(+)
create mode 100644 2.cpp
...
文件内容冲突
回到client1,将1.cpp修改成如下并提交
#include <stdint>
#include <stdlib>
$ cd client1
$ git add .
$ git commit -m 'modify 1.cpp'
$ git push
回到client2也修改1.cpp提交,这时并没有拉取最新版本
#include <stdint>
#include <sb>
$ cd client2
$ git add .
$ git commit -m 'add sb to 1.cpp'
$ git push
! [rejected]
error: failed to push some refs to 'xxx'
hint: Updates were rejected because the remote contains work that you do not have locally...
当然,不是最新分支报错,执行pull拉取最新提交,可以看到自动合并失败提示
$ git pull
Auto-merging 1.cpp
CONFLICT (content): Merge conflict in 1.cpp
Automatic merge failed; fix conflicts and then commit the result.
这个时候需要手动解决冲突,现在1.cpp内容如下
#include <stdint>
<<<<<<< HEAD
#include <sb>
=======
#include <stdlib>
>>>>>>> 7d7a5b1a1604ce9e487f98707bc563bbd58ff974
这个“=======”上面就是本地修改的内容,下面是远程分支内容。将1.cpp修改成如下,然后commit并提交到远程
#include <stdint>
#include <sb>
#include <stdlib>
$ git add .
$ git commit -m 'merge sb and stdlib'
$ git push
$ git log
通过log可以看到产生了最新3条提交记录,回到client1执行pull拉取到刚才的合并后的代码
$ cd client1
$ git pull
...
Updating 7d7a5b1..7c4f042
Fast-forward
1.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
...