目录
多人协作
一、不同人修改不同文件
1、场景描述
在GitHub网站gitLearning仓库创建一个分支:feature/add_git_commonds
在本地仓库gitLearning,创建一个feature/add_git_commonds分支并跟踪远端feature/add_git_commonds
在本地克隆一个新仓库gitLearning_02,创建一个feature/add_git_commonds分支并跟踪远端feature/add_git_commonds
在gitLearning仓库,feature/add_git_commonds分支修改index.html文件并commit,并push
在gitLearning_02仓库,feature/add_git_commonds分支修改readme文件并commit,并push
2、命令演示
#第一步:
#在GitHub创建一个新分支feature/add_git_commonds
#第二步:
bogon:gitLearning LC$ git checkout -b feature/add_git_commonds github/feature/add_git_commonds
#第三步:
#将GitHub的Git仓库clone到本地,仓库名称为gitLearning_02
git clone git@github.com:LC/gitLearning.git gitLearning_02
#并在本地基于origin/feature/add_git_commonds创建本地分支feature/add_git_commonds
#通过该指令本地分支会与orign的feature/add_git_commonds建立关联
bogon:gitLearning_02 LC$ git checkout -b feature/add_git_commonds origin/feature/add_git_commonds
Branch 'feature/add_git_commonds' set up to track remote branch 'feature/add_git_commonds' from 'origin'.
Switched to a new branch 'feature/add_git_commonds'
#第四步:
#在gitLearning仓库,feature/add_git_commonds分支修改index.html文件并commit,并push
#第五步:
#在gitLearning_02仓库,feature/add_git_commonds分支修改readme文件并commit,并push
bogon:gitLearning_02 LC$ git push
To github.com:JiangHuHiKe/gitLearning.git
! [rejected] feature/add_git_commonds -> feature/add_git_commonds (fetch first)
error: failed to push some refs to 'git@github.com:JiangHuHiKe/gitLearning.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.
bogon:gitLearning_02 LC$
3、分析
由于两个仓库在同一个分支修改了仓库内容,并且gitLearning推送了其内容,
gitLearning_02仓库、feature/add_git_commonds分支在没有拉取最新的内容的情况下做了commit
会产生冲突无法正常推送到远程的feature/add_git_commonds分支
我们通过git fetch 命令将远端的更新拉取下来(不会merge),查看下状态
bogon:gitLearning_02 LC$ git status
On branch feature/add_git_commonds
Your branch and 'origin/feature/add_git_commonds' have diverged,
and have 1 and 1 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
nothing to commit, working tree clean
bogon:gitLearning_02 LC$
4、因为不是同一个文件,该冲突可以通过merge解决。
bogon:gitLearning_02 LC$ git merge origin/feature/add_git_commonds
bogon:gitLearning_02 LC$ git push
bogon:gitLearning_02 LC$ git log --oneline -n 4
16a59ac (HEAD -> feature/add_git_commonds, origin/feature/add_git_commonds) Merge remote-tracking branch 'origin/feature/add_git_commonds' into feature/add_git_commonds
fa370ba update raedme file
9b88a9f Add command
48deee6 Add git commands description in readme
二、不同人修改同一文件的不同部分
1、场景描述
gitLearning:修改index.html文件的部分内容1,commit,push
gitLearning_02:修改index.html文件的部分内容2,commit,push,跟上边的情景相同这时Git拒绝了提交
2、命令演示
#使用git fetch 将内容拉取下来
bogon:gitLearning_02 LC$ git fetch
#使用git merge origin/feature/add_git_commonds 合并分支(这两部可以合并为一个命令,git pull)
bogon:gitLearning_02 LC$ git merge origin/feature/add_git_commonds
3、虽然是同一个文件,该冲突也可以通过merge解决。
三、不同人修改同一文件的相同部分
1、场景描述
gitLearning:修改index.html文件的部分内容1,commit,push
gitLearning_02:修改index.html文件的部分内容1,commit,push,这时Git拒绝了提交
2、命令演示
bogon:gitLearning LC$ git pull
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
bogon:gitLearning LC$
#看提示我们现在产生了冲突
#查看下当前Git的状态
bogon:gitLearning LC$ git status
On branch feature/add_git_commonds
Your branch and 'github/feature/add_git_commonds' have diverged,
and have 1 and 1 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")
bogon:gitLearning LC$
3、冲突解决
冲突解决有两种方式一种是命令行的方式,一种是图像化的方式
我们采用命令行的方式
#通过vi index.html进入文件查看冲突
<<<<<<< HEAD
<li>mv</li>
<li>rm</li>
=======
<li>stash</li>
<li>log</li>
>>>>>>> aceffed30bb2110e4749c7873c57fd3d8317b09b
#上半部分是当前分支的改动,下半部分是远程分支的改动,此时我们可以根据实际需要对冲突进行手动解决
#我们保留所有的变更
<li>mv</li>
<li>rm</li>
<li>stash</li>
<li>log</li>
#手动修改完成后我们进行commit和push
bogon:gitLearning LC$ git commit -am 'Resolved conflict by hand with 4 git commands'
bogon:gitLearning LC$ git push
多人协作2
一、同时变更文件名和文件内容
1、场景描述
gitLearning:修改index.html文件为index.htm,commit,push
gitLearning_02:修改index.html文件的部分内容,commit,push,这时Git拒绝了提交
2、命令演示
#我们使用git pull来拉取远端分支
git pull
#自动弹出了merger,我们采用缺省值
#使用push命令,现在可以正常push了
git push
#我们看下提交记录
bogon:gitLearning_02 LC$ git log -n 3 --oneline
4c632b4 (HEAD -> feature/add_git_commonds, origin/feature/add_git_commonds) Merge branch 'feature/add_git_commonds' of github.com:JiangHuHiKe/gitLearning into feature/add_git_commonds
69a34ab modify index.html
e2ddad7 mv index.html to index.htm
bogon:gitLearning_02 LC$
#在看下现在的文件
bogon:gitLearning_02 LC$ ls
LICENSE doc images index.htm js readme
#再看下文件内容
cat index.htm
我们发现Git自动帮我们merge了内容:修改了文件名,变更了文件内容
如果是这样呢?
gitLearning_02:修改index.html文件的部分内容,commit,push
gitLearning:修改index.html文件为index.htm,commit,push
也是一样的第二次push会被拒,执行git pull 会自动帮我们merge
二、把同一文件改成不同文件名
1、场景描述
gitLearning:修改index.html文件为index1.html,commit,push
gitLearning_02:修改index.html文件为index2.html,commit,push ,这时Git拒绝了提交
2、命令演示
#执行git pull 提示我们有冲突
bogon:gitLearning_02 LC$ git pull
#查看下当前的Git的状态
bogon:gitLearning_02 LC$ git status
On branch feature/add_git_commonds
Your branch and 'origin/feature/add_git_commonds' have diverged,
and have 1 and 1 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add/rm <file>..." as appropriate to mark resolution)
both deleted: index.html
added by them: index1.html
added by us: index2.html
no changes added to commit (use "git add" and/or "git commit -a")
bogon:gitLearning_02 LC$
#根据提示
#当前分支和远程分支都删除了index.html文件
#远程分支添加了index1.html
#当前分支添加了index2.html
#使用git add/rm 来解决
#通过以下指令解决问题
bogon:gitLearning_02 LC$ git rm index.html
bogon:gitLearning_02 LC$ git rm index1.html
bogon:gitLearning_02 LC$ git add index2.html
bogon:gitLearning_02 LC$ git commit -am "resolve conflicts"
bogon:gitLearning_02 LC$ git push
行者常至,为者常成!