原创作品,转载请标明:http://blog.csdn.net/jackystudio/article/details/12309531
1.多人协作冲突
如果多人同时修改了同一个文件,那会出现什么样的结果呢?我们试着这么做。
(1)修改jackygit2
在jackygit2中修改jackydata01,提交修改并推送到远程仓库(这里使用本地远程仓库)。正常,没问题。
- #切换到jackygit2本地库,这是远程仓库那节建立的,从本地远程仓库克隆而来
- $ cd ../jackygit2
- #修改jackygit2/Jackydata01
- $ echo "It's modified in jackygit2" > jackydata01
- #提交jackygit2的修改
- $ git commit -a -m "jackygit2 modify"
- [master 15a6406] jackygit2 modify
- 1 file changed, 1 insertion(+), 1 deletion(-)
- #推送到远程仓库origin
- $ git push origin
- Counting objects: 5, done.
- Delta compression using up to 2 threads.
- Compressing objects: 100% (2/2), done.
- Writing objects: 100% (3/3), 283 bytes | 0 bytes/s, done.
- Total 3 (delta 1), reused 0 (delta 0)
- To d:/jackygit2/../remote-jackygit.git
- c0449de..15a6406 master -> master
(2)修改jackygit
切换到jackygit仓库,同样修改jackydata01,提交修改并推送到远程仓库。这时候就出错了,提示有人改过这个文件了。推送不成功。
- #切换到jackygit
- $ cd ../jackygit
- #修改jackygit/Jackydata01
- $ echo "It's modified in jackygit" >jackydata01
- #提交jackygit的修改
- $ git commit -a -m "jackygit modify"
- [master 9ab7206] jackygit modify
- 1 file changed, 1 insertion(+), 1 deletion(-)
- 推送到远程仓库testremote,这个仓库和上面的origin是同一个远程仓库
- $ git push testremote
- To ../remote-jackygit.git
- ! [rejected] master -> master (fetch first)
- error: failed to push some refs to '../remote-jackygit.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.
(3)抓取远程仓库内容
抓取远程仓库中的文件,这里需要指定分支。
- #抓取远程仓库中的文件,需要指定分支
- $ git pull testremote master
- From ../remote-jackygit
- * branch master -> FETCH_HEAD
- Auto-merging Jackydata01
- CONFLICT (content): Merge conflict in Jackydata01
- Automatic merge failed; fix conflicts and then commit the result.
(4)查看冲突文件内容
冲突用<<<<<< ====== >>>>>>隔开冲突代码,上面的是当前修改内容,下面的则是别人修改的内容。
- #输出当前jackydata01内容
- $ cat jackydata01
- <<<<<<< HEAD
- It's modified in jackygit
- =======
- It's modified in jackygit2
- >>>>>>> 15a6406ed7f889bab7e812f9e6bedb6e78431232
(5)解决冲突
修改冲突内容,重新提交,推送。
- #修改Jackydata01产生的冲突
- $ echo "there is no conflict now" > jackydata01
- #提交修改冲突后文件
- $ git commit -a -m "there is no confilct now"
- [master ac2846f] there is no confilct now
- #推送到远程仓库testremote
- $ git push testremote
- Counting objects: 10, done.
- Delta compression using up to 2 threads.
- Compressing objects: 100% (4/4), done.
- Writing objects: 100% (6/6), 567 bytes | 0 bytes/s, done.
- Total 6 (delta 2), reused 0 (delta 0)
- To ../remote-jackygit.git
- 15a6406..ac2846f master -> master
2.分支合并冲突
这种情况和多人协作冲突处理办法是相似的。在这里也模拟一下。
(1)创建分支并修改内容
- #创建并跳转到分支br
- $ git checkout -b br
- Switched to a new branch 'br'
- #修改br分支中的Jackydata01
- $ echo "It's modified in br" > jackydata01
- #提交br分支中的修改
- $ git commit -a -m "br modify"
- [br 32c8755] br modify
- 1 file changed, 1 insertion(+), 1 deletion(-)
(2)跳转到主分支,同样进行内容修改
- #跳转到master分支
- $ git checkout master
- Switched to branch 'master'
- #修改master分支中的jackydata01
- $ echo "It's modified in master" > jackydata01
- #提交master分支中的修改
- $ git commit -a -m "master modify"
- [master ed84e67] master modify
- 1 file changed, 1 insertion(+), 1 deletion(-)
(3)合并分支
- #合并br到主分支上
- $ git merge br
- Auto-merging Jackydata01
- CONFLICT (content): Merge conflict in Jackydata01
- Automatic merge failed; fix conflicts and then commit the result.
(4)显示冲突文件内容,冲突隔开方式如上所述
- #显示br中Jackydata01内容
- $ cat jackydata01
- <<<<<<< HEAD
- It's modified in master
- =======
- It's modified in br
- >>>>>>> br
(5)解决冲突,重新提交
- 修改Jackydata01产生的冲突
- $ echo "It's no conflict now" > jackydata01
- #提交修改冲突后文件
- $ git commit -a -m "It's no conflict now"
- [master eb073a6] It's no conflict now
(6)删除无效分支
- #删除br分支
- $ git branch -d br
- Deleted branch br (was 32c8755).