任务列表:
有一个中央库Center,和三个工作站A,B,C。
初始化时,代码存放在中央库中,A,B,C三个工作站开始工作之前都要首先从中央库克隆一份代码到本地。
第一个任务:A和B合作修复一个缺陷,要求是A先开始修复,B在A修复的基础上接着修复,然后由B把修改好的缺席推送到中央库的master上。
第二个任务:C在中央库的基础上新建一个分支开始自己的开发,并把自己新建的分支推送到中央库。
第三个任务:A和B合作修复好第一个缺席之后,A自己单独修复第二个缺席,并推送到中央库的master分支上。
第四个任务:B要等A修复完第二个缺陷和C开发完自己的代码后,把中央库上的master分支和C推送的分支合并,并推送回中央库的master分支。
1.中央库
①初始化中央库,并执行一次提交
[java:showcolumns] view plain copy print ?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
- $ git init
- Initialized empty Git repository in /home/greta/GitTest/Center/.git/
- $ git add .
- $ git commit -m "new file a"
$ git init Initialized empty Git repository in /home/greta/GitTest/Center/.git/ $ git add . $ git commit -m "new file a"
图示如下:
②wsB第一次推送后,中央库的变化
图示如下:
③wsC推送后,中央库的变化
图示如下:
④wsA推送后,中央库的变化:
⑤wsB第二次推送后,中央库的变化
2.工作站A的操作
①克隆中央库到工作站A
[java] view plain copy print ?
- $ git clone /home/greta/GitTest/Center/.git/ wsA
- Cloning into wsA...
- done.
$ git clone /home/greta/GitTest/Center/.git/ wsA Cloning into wsA... done.
从Git服务器克隆数据时,Git 会自动为你将此远程仓库命名为 origin
,并下载其中所有的数据,建立一个指向它的 master
分支的指针,在本地命名为 origin/master
,但你无法在本地更改其数据。接着,Git 建立一个属于你自己的本地 master
分支,始于 origin
上 master
分支相同的位置,你可以就此开始工作。
图示如下:
②在工作站A上建立一个branch testA,并把指针切换到testA上。
[c-sharp] view plain copy print ?
- $ git checkout -b testA
- Switched to a new branch 'testA'
$ git checkout -b testA Switched to a new branch 'testA'
这里的命令$ git chekout -b testA实际上是两个命令的合并,相当于执行:
$git branch testA
$git checkout testA
图示如下(*表示当前wsA上head指针的位置):
③在wsA上执行一次提交
[c-sharp] view plain copy print ?
- $ git add .
- $ git commit -m "submit on wsA(second)"
$ git add . $ git commit -m "submit on wsA(second)"
图示如下:
3.工作站B上的操作
①克隆中央库到工作站B
[c-sharp] view plain copy print ?
- $ git clone /home/greta/GitTest/Center/.git/ wsB
- Cloning into wsB...
- done.
$ git clone /home/greta/GitTest/Center/.git/ wsB Cloning into wsB... done.
图示如下:
②把工作站A的远程分支testA添加到工作站B,并把wsA上分支testA上的数据合并到本机的master上
[c-sharp] view plain copy print ?
- $ git remote add wsA /home/greta/GitTest/wsA/.git
- $ git fetch wsA
- remote: Counting objects: 5, done.
- remote: Total 3 (delta 0), reused 0 (delta 0)
- Unpacking objects: 100% (3/3), done.
- From /home/greta/GitTest/wsA/
- * [new branch] master -> wsA/master
- * [new branch] testA -> wsA/testA
- $ git merge wsA/testA
- Updating 20f3246..eb0f47b
- Fast-forward
- a.txt | 1 +
- 1 files changed, 1 insertions(+), 0 deletions(-)
$ git remote add wsA /home/greta/GitTest/wsA/.git $ git fetch wsA remote: Counting objects: 5, done. remote: Total 3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. From /home/greta/GitTest/wsA/ * [new branch] master -> wsA/master * [new branch] testA -> wsA/testA $ git merge wsA/testA Updating 20f3246..eb0f47b Fast-forward a.txt | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
用$git remote add命令添加远程分支时,会把远程机器上所有的分支都加载到本地机器上。
图示如下:
③在wsB上做些修改,并提交
[c-sharp] view plain copy print ?
- $ git add .
- $ git commit -m "submit on wsB(third)"
- [master de9ed05] submit on wsB(third)
- 1 files changed, 1 insertions(+), 0 deletions(-)
$ git add . $ git commit -m "submit on wsB(third)" [master de9ed05] submit on wsB(third) 1 files changed, 1 insertions(+), 0 deletions(-)
图示如下:
④把wsB上master分支的内容提交到中央库中。
[c-sharp] view plain copy print ?
- $ git push
- Counting objects: 8, done.
- Compressing objects: 100% (2/2), done.
- Writing objects: 100% (6/6), 493 bytes, done.
- Total 6 (delta 0), reused 0 (delta 0)
- Unpacking objects: 100% (6/6), done.
- To /home/greta/GitTest/Center/.git/
- 20f3246..de9ed05 master -> master
$ git push Counting objects: 8, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (6/6), 493 bytes, done. Total 6 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (6/6), done. To /home/greta/GitTest/Center/.git/ 20f3246..de9ed05 master -> master
图示如下:
中央库的变化图示请参照 1.中央库-->②wsB第一次推送后,中央库的变化
4.工作站C的操作
①克隆中央库到本机
[c-sharp] view plain copy print ?
- $ git clone /home/greta/GitTest/Center/.git/ wsC
- Cloning into wsC...
- done.
$ git clone /home/greta/GitTest/Center/.git/ wsC Cloning into wsC... done.
图示如下:
②在wsC上新建一个branch testC并把testC推送到中央库
[c-sharp] view plain copy print ?
- $ git checkout -b testC
- Switched to a new branch 'testC'
- $ git add .
- $ git commit -m "submit on wsC(Fourth)"
- $ git push origin testC
- Counting objects: 5, done.
- Writing objects: 100% (3/3), 274 bytes, done.
- Total 3 (delta 0), reused 0 (delta 0)
- Unpacking objects: 100% (3/3), done.
- To /home/greta/GitTest/Center/.git/
- * [new branch] testC -> testC
$ git checkout -b testC Switched to a new branch 'testC' $ git add . $ git commit -m "submit on wsC(Fourth)" $ git push origin testC Counting objects: 5, done. Writing objects: 100% (3/3), 274 bytes, done. Total 3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. To /home/greta/GitTest/Center/.git/ * [new branch] testC -> testC
图示如下:
中央库的图示请参见:1.中央库--->③wsC推送后,中央库的变化
5.wsC推送后wsA上操作
①同步wsA与中央库
[c-sharp] view plain copy print ?
- $ git fetch
- remote: Counting objects: 8, done.
- remote: Compressing objects: 100% (2/2), done.
- remote: Total 6 (delta 0), reused 0 (delta 0)
- Unpacking objects: 100% (6/6), done.
- From /home/greta/GitTest/Center/
- 20f3246..de9ed05 master -> origin/master
- * [new branch] testC -> origin/testC
$ git fetch remote: Counting objects: 8, done. remote: Compressing objects: 100% (2/2), done. remote: Total 6 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (6/6), done. From /home/greta/GitTest/Center/ 20f3246..de9ed05 master -> origin/master * [new branch] testC -> origin/testC
图示如下:
②合并wsA上master与中央库的master,删除分支testA, 在中央库master的基础上提交
[c-sharp] view plain copy print ?
- $ git checkout master
- Previous HEAD position was e3e1972... submit on wsC(Fourth)
- Switched to branch 'master'
- Your branch is behind 'origin/master' by 2 commits, and can be fast-forwarded.
- $ git merge origin master
- Fast-forwarding to: origin
- Already up-to-date with master
- Merge made by octopus.
- a.txt | 2 ++
- 1 files changed, 2 insertions(+), 0 deletions(-)
- $ git branch -d testA
- Deleted branch testA (was eb0f47b).
- $ git add .
- $ git commit -m "submit on wsA(fifth)"
- [master 52ef8ce] submit on wsA(fifth)
- 1 files changed, 2 insertions(+), 0 deletions(-)
$ git checkout master Previous HEAD position was e3e1972... submit on wsC(Fourth) Switched to branch 'master' Your branch is behind 'origin/master' by 2 commits, and can be fast-forwarded. $ git merge origin master Fast-forwarding to: origin Already up-to-date with master Merge made by octopus. a.txt | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-) $ git branch -d testA Deleted branch testA (was eb0f47b). $ git add . $ git commit -m "submit on wsA(fifth)" [master 52ef8ce] submit on wsA(fifth) 1 files changed, 2 insertions(+), 0 deletions(-)
图示如下:
③推送到中央库
[c-sharp] view plain copy print ?
- $ git push
- Counting objects: 6, done.
- Compressing objects: 100% (2/2), done.
- Writing objects: 100% (4/4), 447 bytes, done.
- Total 4 (delta 0), reused 0 (delta 0)
- Unpacking objects: 100% (4/4), done.
- To /home/greta/GitTest/Center/.git/
- de9ed05..52ef8ce master -> master
$ git push Counting objects: 6, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (4/4), 447 bytes, done. Total 4 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (4/4), done. To /home/greta/GitTest/Center/.git/ de9ed05..52ef8ce master -> master
图示如下:
wsA推送后,中央库的变化请参照:1.中央库---->④wsA推送后,中央库的变化
6.wsA推送后wsB的操作
①同步中央库
[c-sharp] view plain copy print ?
- $ git fetch
- remote: Counting objects: 9, done.
- remote: Compressing objects: 100% (3/3), done.
- remote: Total 7 (delta 0), reused 0 (delta 0)
- Unpacking objects: 100% (7/7), done.
- From /home/greta/GitTest/Center/
- de9ed05..52ef8ce master -> origin/master
- * [new branch] testC -> origin/testC
$ git fetch remote: Counting objects: 9, done. remote: Compressing objects: 100% (3/3), done. remote: Total 7 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (7/7), done. From /home/greta/GitTest/Center/ de9ed05..52ef8ce master -> origin/master * [new branch] testC -> origin/testC
图示如下:
②合并34ac4和34ac3两个操作:
[c-sharp] view plain copy print ?
- $ git merge origin master
- Fast-forwarding to: origin
- Already up-to-date with master
- Merge made by octopus.
- a.txt | 2 ++
- 1 files changed, 2 insertions(+), 0 deletions(-)
- $ git merge origin/testC
- Auto-merging a.txt
- CONFLICT (content): Merge conflict in a.txt
- Automatic merge failed; fix conflicts and then commit the result.
- $ git mergetool
- $ git commit -m "merge on wsB"
- $ git push
- Counting objects: 8, done.
- Compressing objects: 100% (2/2), done.
- Writing objects: 100% (4/4), 472 bytes, done.
- Total 4 (delta 0), reused 0 (delta 0)
- Unpacking objects: 100% (4/4), done.
- To /home/greta/GitTest/Center/.git/
- 52ef8ce..bf54de7 master -> master
$ git merge origin master Fast-forwarding to: origin Already up-to-date with master Merge made by octopus. a.txt | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-) $ git merge origin/testC Auto-merging a.txt CONFLICT (content): Merge conflict in a.txt Automatic merge failed; fix conflicts and then commit the result. $ git mergetool $ git commit -m "merge on wsB" $ git push Counting objects: 8, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (4/4), 472 bytes, done. Total 4 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (4/4), done. To /home/greta/GitTest/Center/.git/ 52ef8ce..bf54de7 master -> master
图示如下: