Git操作流程,基本命令演示

任务列表:

有一个中央库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
  1. $ git init 
  2. Initialized empty Git repository in /home/greta/GitTest/Center/.git/ 
  3. $ git add . 
  4. $ 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第一次推送后,中央库的变化

图示如下:

Git操作流程,基本命令演示_第1张图片

③wsC推送后,中央库的变化

图示如下:

Git操作流程,基本命令演示_第2张图片

④wsA推送后,中央库的变化:

Git操作流程,基本命令演示_第3张图片

⑤wsB第二次推送后,中央库的变化

Git操作流程,基本命令演示_第4张图片

2.工作站A的操作

①克隆中央库到工作站A


[java] view plain copy print ?
  1. $ git clone /home/greta/GitTest/Center/.git/ wsA 
  2. Cloning into wsA... 
  3. done. 
$ git clone /home/greta/GitTest/Center/.git/ wsA Cloning into wsA... done.

从Git服务器克隆数据时,Git 会自动为你将此远程仓库命名为 origin,并下载其中所有的数据,建立一个指向它的 master分支的指针,在本地命名为 origin/master,但你无法在本地更改其数据。接着,Git 建立一个属于你自己的本地 master分支,始于 originmaster分支相同的位置,你可以就此开始工作。

图示如下:

②在工作站A上建立一个branch testA,并把指针切换到testA上。

[c-sharp] view plain copy print ?
  1. $ git checkout -b testA 
  2. 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指针的位置):
Git操作流程,基本命令演示_第5张图片

③在wsA上执行一次提交

[c-sharp] view plain copy print ?
  1. $ git add . 
  2. $ git commit -m "submit on wsA(second)" 
$ git add . $ git commit -m "submit on wsA(second)"

图示如下:
Git操作流程,基本命令演示_第6张图片

3.工作站B上的操作

①克隆中央库到工作站B

[c-sharp] view plain copy print ?
  1. $ git clone /home/greta/GitTest/Center/.git/ wsB 
  2. Cloning into wsB... 
  3. 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 ?
  1. $ git remote add wsA /home/greta/GitTest/wsA/.git 
  2. $ git fetch wsA 
  3. remote: Counting objects: 5, done. 
  4. remote: Total 3 (delta 0), reused 0 (delta 0) 
  5. Unpacking objects: 100% (3/3), done. 
  6. From /home/greta/GitTest/wsA/ 
  7. * [new branch]      master     -> wsA/master 
  8. * [new branch]      testA      -> wsA/testA 
  9. $ git merge wsA/testA 
  10. Updating 20f3246..eb0f47b 
  11. Fast-forward 
  12. a.txt |    1 + 
  13. 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命令添加远程分支时,会把远程机器上所有的分支都加载到本地机器上。

图示如下:

Git操作流程,基本命令演示_第7张图片

③在wsB上做些修改,并提交

[c-sharp] view plain copy print ?
  1. $ git add . 
  2. $ git commit -m "submit on wsB(third)" 
  3. [master de9ed05] submit on wsB(third) 
  4. 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(-)

图示如下:

Git操作流程,基本命令演示_第8张图片

④把wsB上master分支的内容提交到中央库中。

[c-sharp] view plain copy print ?
  1. $ git push 
  2. Counting objects: 8, done. 
  3. Compressing objects: 100% (2/2), done. 
  4. Writing objects: 100% (6/6), 493 bytes, done. 
  5. Total 6 (delta 0), reused 0 (delta 0) 
  6. Unpacking objects: 100% (6/6), done. 
  7. To /home/greta/GitTest/Center/.git/ 
  8.    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

图示如下:

Git操作流程,基本命令演示_第9张图片

中央库的变化图示请参照 1.中央库-->②wsB第一次推送后,中央库的变化

4.工作站C的操作

①克隆中央库到本机

[c-sharp] view plain copy print ?
  1. $ git clone /home/greta/GitTest/Center/.git/ wsC 
  2. Cloning into wsC... 
  3. done. 
$ git clone /home/greta/GitTest/Center/.git/ wsC Cloning into wsC... done.

图示如下:

Git操作流程,基本命令演示_第10张图片

②在wsC上新建一个branch testC并把testC推送到中央库

[c-sharp] view plain copy print ?
  1. $ git checkout -b testC 
  2. Switched to a new branch 'testC' 
  3. $ git add . 
  4. $ git commit -m "submit on wsC(Fourth)" 
  5. $ git push origin testC 
  6. Counting objects: 5, done. 
  7. Writing objects: 100% (3/3), 274 bytes, done. 
  8. Total 3 (delta 0), reused 0 (delta 0) 
  9. Unpacking objects: 100% (3/3), done. 
  10. To /home/greta/GitTest/Center/.git/ 
  11. * [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

图示如下:

Git操作流程,基本命令演示_第11张图片

中央库的图示请参见:1.中央库--->③wsC推送后,中央库的变化

5.wsC推送后wsA上操作

①同步wsA与中央库

[c-sharp] view plain copy print ?
  1. $ git fetch 
  2. remote: Counting objects: 8, done. 
  3. remote: Compressing objects: 100% (2/2), done. 
  4. remote: Total 6 (delta 0), reused 0 (delta 0) 
  5. Unpacking objects: 100% (6/6), done. 
  6. From /home/greta/GitTest/Center/ 
  7.    20f3246..de9ed05  master     -> origin/master 
  8. * [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 图示如下:

Git操作流程,基本命令演示_第12张图片

②合并wsA上master与中央库的master,删除分支testA, 在中央库master的基础上提交

[c-sharp] view plain copy print ?
  1. $ git checkout master 
  2. Previous HEAD position was e3e1972... submit on wsC(Fourth) 
  3. Switched to branch 'master' 
  4. Your branch is behind 'origin/master' by 2 commits, and can be fast-forwarded. 
  5. $ git merge origin master 
  6. Fast-forwarding to: origin 
  7. Already up-to-date with master 
  8. Merge made by octopus. 
  9. a.txt |    2 ++ 
  10. 1 files changed, 2 insertions(+), 0 deletions(-) 
  11. $ git branch -d testA 
  12. Deleted branch testA (was eb0f47b). 
  13. $ git add . 
  14. $ git commit -m "submit on wsA(fifth)" 
  15. [master 52ef8ce] submit on wsA(fifth) 
  16. 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(-)

图示如下:

Git操作流程,基本命令演示_第13张图片

③推送到中央库

[c-sharp] view plain copy print ?
  1. $ git push 
  2. Counting objects: 6, done. 
  3. Compressing objects: 100% (2/2), done. 
  4. Writing objects: 100% (4/4), 447 bytes, done. 
  5. Total 4 (delta 0), reused 0 (delta 0) 
  6. Unpacking objects: 100% (4/4), done. 
  7. To /home/greta/GitTest/Center/.git/ 
  8.    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

图示如下:

Git操作流程,基本命令演示_第14张图片


wsA推送后,中央库的变化请参照:1.中央库---->④wsA推送后,中央库的变化

6.wsA推送后wsB的操作

①同步中央库

[c-sharp] view plain copy print ?
  1. $ git fetch 
  2. remote: Counting objects: 9, done. 
  3. remote: Compressing objects: 100% (3/3), done. 
  4. remote: Total 7 (delta 0), reused 0 (delta 0) 
  5. Unpacking objects: 100% (7/7), done. 
  6. From /home/greta/GitTest/Center/ 
  7.    de9ed05..52ef8ce  master     -> origin/master 
  8. * [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

图示如下:

Git操作流程,基本命令演示_第15张图片

②合并34ac4和34ac3两个操作:

[c-sharp] view plain copy print ?
  1. $ git merge origin master 
  2. Fast-forwarding to: origin 
  3. Already up-to-date with master 
  4. Merge made by octopus. 
  5. a.txt |    2 ++ 
  6. 1 files changed, 2 insertions(+), 0 deletions(-) 
  7. $ git merge origin/testC 
  8. Auto-merging a.txt 
  9. CONFLICT (content): Merge conflict in a.txt 
  10. Automatic merge failed; fix conflicts and then commit the result. 
  11. $ git mergetool 
  12. $ git commit -m "merge on wsB" 
  13. $ git push 
  14. Counting objects: 8, done. 
  15. Compressing objects: 100% (2/2), done. 
  16. Writing objects: 100% (4/4), 472 bytes, done. 
  17. Total 4 (delta 0), reused 0 (delta 0) 
  18. Unpacking objects: 100% (4/4), done. 
  19. To /home/greta/GitTest/Center/.git/ 
  20.    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

图示如下:

Git操作流程,基本命令演示_第16张图片

你可能感兴趣的:(Git操作流程,基本命令演示)