CI使用篇1开发者使用GIT常见场景.docx
- 文档编号:7772066
- 上传时间:2023-01-26
- 格式:DOCX
- 页数:29
- 大小:28.59KB
CI使用篇1开发者使用GIT常见场景.docx
《CI使用篇1开发者使用GIT常见场景.docx》由会员分享,可在线阅读,更多相关《CI使用篇1开发者使用GIT常见场景.docx(29页珍藏版)》请在冰豆网上搜索。
CI使用篇1开发者使用GIT常见场景
【11】当认为目前的代码在一定阶段内稳定,可以提交到测试环境下,git管理员如何打标签。
【12】为什么CI要求remoterepository只能由一个master分支。
作者:
yelei
email:
kursk.ye@
本文内容:
针对各种使用GIT典型场景,描述git操作过程。
【1】开发者如何生成自己的SSHgit账号
(1)客户端产生公私密匙。
$ssh-keygen-trsa-C"git@192.170.100.15yelei.kursk"
Generatingpublic/privatersakeypair.
Enterfileinwhichtosavethekey(/c/Users/kursk/.ssh/id_rsa):
Enterpassphrase(emptyfornopassphrase):
#可以不输入密码,直接回车,密码为空,简单但不安全
Entersamepassphraseagain:
#与上面一致
Youridentificationhasbeensavedin/c/Users/kursk/.ssh/id_rsa.
Yourpublickeyhasbeensavedin/c/Users/kursk/.ssh/id_rsa.pub.
Thekeyfingerprintis:
e7:
32:
dc:
8d:
e8:
f9:
b6:
0e:
2a:
7a:
71:
34:
17:
40:
4c:
34git@192.170.100.15yelei.kursk
(2)将客户端id_rsa.pub里的内容交给git管理员,由git管理员负责复制到服务端authorized_keys文件内,如果有多个直接换行复制。
$cat/home/git/.ssh/authorized_keys
ssh-rsaAAAAB3NzaC1yc2EAAAABIwAAAQEA4BpIh914++YT61aiMMjc6H+31FHB8TJjLhVQ1EKmnB72k+3VtoXa/CCrNqDrlVTJxx9Y003RCzJCMVhLmBBX+100BqSSzDLxjRt+L/WbLZtLDucSJmW1MZywlq/vFAGeLRnuWDP0VHHL2/0jBvY6sajpVdNXRWu3nuaMYPhP6ECYsuZDMBgsSALw/LvkoWA8ZCdy38B+L8FJUZ8J87Vnr+mDB/+b87u21fcE7a/fUiOIkZSAWPMJ78mznk2e2Mz0lmK2OqqJXFq5lk7+h7rAl/rd/aVayESmZ9Xx4S5JKqVwxMeZfAVBcYBNz2xkO+LXnzmX+jnSZkjV+mwN+JoSQQ==git@192.170.100.15
(3)这样开发者通过客户端就可以访问了gitrepository
$gitclonessh:
//git@192.170.100.15:
/home/git/git.repository/test.gittest6
Cloninginto'test6'...
Theauthenticityofhost'192.170.100.15:
(192.170.100.15)'can'tbeestablished.
RSAkeyfingerprintis97:
5c:
2f:
aa:
f1:
19:
0d:
81:
5d:
6a:
3e:
97:
94:
18:
01:
84.
Areyousureyouwanttocontinueconnecting(yes/no)?
yes#输入yes,如果之前有密码,这里还要输入密码
Warning:
Permanentlyadded'192.170.100.15:
192.170.100.15'(RSA)tothelistofknownhosts.
remote:
Countingobjects:
3,done.
remote:
Compressingobjects:
100%(2/2),done.
remote:
Total3(delta0),reused0(delta0)
Receivingobjects:
100%(3/3),done.
Checkingconnectivity...done
提示:
如果开发者忘记了密匙,可以在gitgui/帮助/showsshkey可以看到生成的publickey
【2】第一次如何下载代码,GIT的checkout与SVN有何不同?
答:
git也有checkout命令,但与SVN不同。
SVN的做法是第一次从svnrepository上checkout到localworkcopy,以后每次再update到svnrepository。
但git的checkout不是从repository上下载代码,而是切换分支,下面的例子中将讲到分支如何使用。
【3】我是一个GIT初学者,如何尽快上手。
答:
首先,理解workspace、index、remoteRepository的概念,掌握代码在这几个区域切换的命令,如commit、push、pull等,官方图片说明是一个很好的帮助手册。
其次,多加以练习,局域网内提供了练习用工程,地址是:
ssh:
//git@192.170.100.15:
/home/git/git.repository/test.git.
如果可以访问外网,git官方还提供了一个web版的git练习。
下面简单地说明一些常用设置和命令。
(1)安装gitclient后,要设置客户端用户名和邮箱,这样才能知道谁又上传了最新的代码,为了方便理解,要求所有的开发组成员使用"中文姓名全拼音"."个性名字",例如yelei.kursk,zhengyan.teacher。
$gitconfig--globaluser.name"yelei.kursk"
$gitconfig--globaluser.emailkursk.ye@
这样当我们上传代码到repository后,从log里可以看到哪些人何时上传或修改了什么代码。
(2)clone一个工程。
现在我们已经有一个测试工程,我们需要将它clone到本地,命令是:
$gitclonessh:
//git@192.170.100.15:
/home/git/git.repository/test.gittest2
Cloninginto'test2'...
remote:
Countingobjects:
3,done.
remote:
Compressingobjects:
100%(2/2),done.
remote:
Total3(delta0),reused0(delta0)
Receivingobjects:
100%(3/3),done.
Checkingconnectivity...done
因为公私密匙对的设置上文已经描述,这里不再做说明。
这样在当前目录下就多了一个test2/目录。
(3)修改文件,并上传到localrepository.
这里要先了解git中workspace/index(有时也称为stagingarea)/localrepository三个概念。
首先要知道它们三个有个共同点,都在local也就是在你的笔记本电脑上——这是GIT比SVN上传快的原因。
workspace:
类似SVN的workcopy概念,开发者修改文件后会保存在这个区域。
index:
有时也称为stagingarea,用于存储被修改文件的索引。
localrepositor:
最终保存修改后并commit的文件的空间。
另外还有一个叫remoterepositor,它是所有开发者交互的远程仓库,因为既然localrepositor在每个开发者的本地电脑上,那必然要有一个可供所有开发者交换代码的空间,本例中它的地址就是
ssh:
//git@192.170.100.15:
/home/git/git.repository/test.git.
下面以修改一个文件为例说明:
kursk@KURSK-PC/D/gitworkspace/test2(master)
$cattest2.txt
gitisgoodtoolforsourcecodemanage
kursk@KURSK-PC/D/gitworkspace/test2(master)
$vitest2.txt
kursk@KURSK-PC/D/gitworkspace/test2(master)
$cattest2.txt
gitisgoodtoolforsourcecodemanage
gitisfasterthansvn#我增加了一行
kursk@KURSK-PC/D/gitworkspace/test2(master)
$gitstatus
#Onbranchmaster
#Changesnotstagedforcommit:
#(use"gitadd
#(use"gitcheckout--
#
#modified:
test2.txt
#
nochangesaddedtocommit(use"gitadd"and/or"gitcommit-a")
这时workspace已经改变,但是index的内容没有改变,通过gitstatus可以看到“Changesnotstagedforcommit”的提示,并说明修改的内容
提示:
操作过程中可以随时使用gitstatus了解修改了哪些文件,以及这些文件存于什么状态。
下一步,我们通过gitadd将修改文件从workspace推到index
kursk@KURSK-PC/D/gitworkspace/test2(master)
$gitadd.#gitadd.注意后面有一个dot,不管修改、新增、删除了多少文件,只要在工程顶级目录执行这个命令,就可以将所有修改内容从workspace推到index
kursk@KURSK-PC/D/gitworkspace/test2(master)
$gitstatus
#Onbranchmaster
#Changestobecommitted:
#(use"gitresetHEAD
#
#modified:
test2.txt
这时index内容已经改变,但是还没有commit到localrepository,通过gitstatus可以看得很清楚。
接下来,我们将修改文件从indexcommit到localrepository
kursk@KURSK-PC/D/gitworkspace/test2(master)
$gitcommit-m"modifytest2.txt"#commit命令用于将修改文件从indexcommit到localrepository,这里的-m是说明内容,必须
[master6349e7f]modifytest2.txt
1filechanged,2insertions(+),1deletion(-)
kursk@KURSK-PC/D/gitworkspace/test2(master)
$gitstatus#commit后再查看文件状态,已经没有修改的文件了,这时workspace/index/localrepository三个空间的文件状态完全一致了。
#Onbranchmaster
#Yourbranchisaheadof'origin/master'by1commit.
#(use"gitpush"topublishyourlocalcommits)
#
nothingtocommit,workingdirectoryclean
实际的开发工作中的大多数时间,每天的代码提交到localrepository就可以了,只是在每天下班前才需要将当天已经解决的问题,在编译不报错的情况,集中一次从localrepository提交到remoterepository.当然,如果有些人觉得随时提交到remoterepository也可以,但是前提是必须保证编译可以通过,因为开发CI环境会自动PULLremoterepository代码到开发应用服务器上,如果编译不通过就会导致自动发布失败!
所以这里特别强调,发布到remoterepository上的代码必须保证编译通过,违者重罚!
注意:
使用commit-a-m也可以从workspace一步提交到localrepository。
下面继续说明。
当localrepository里准备好需要修改后的代码后,可以通过push将localrepostiroy的内容推到remoterepository。
虽然这一过程看起来很简单,但实际工作中存在多人交互,所以经常需要先pull再push。
以两个开发者A和B为例,A和B两个开发者使用同一个remoterepository,当A将自己的代码Push到remoterepository后,B再PUSH时就会报错,过程如下
kursk@KURSK-PC/D/gitworkspace/dev_A(master)
$viA.html
kursk@KURSK-PC/D/gitworkspace/dev_A(master)
$gitadd.
kursk@KURSK-PC/D/gitworkspace/dev_A(master)
$gitcommit-a-m"adda.html"
[master31b9fe0]adda.html
1filechanged,1insertion(+)
createmode100644A.html
kursk@KURSK-PC/D/gitworkspace/dev_A(master)
$gitpush
warning:
push.defaultisunset;itsimplicitvalueischangingin
Git2.0from'matching'to'simple'.Tosquelchthismessage
andmaintainthecurrentbehaviorafterthedefaultchanges,use:
gitconfig--globalpush.defaultmatching
Tosquelchthismessageandadoptthenewbehaviornow,use:
gitconfig--globalpush.defaultsimple
See'githelpconfig'andsearchfor'push.default'forfurtherinformation.
(the'simple'modewasintroducedinGit1.7.11.Usethesimilarmode
'current'insteadof'simple'ifyousometimesuseolderversionsofGit)
Countingobjects:
4,done.
Deltacompressionusingupto4threads.
Compressingobjects:
100%(2/2),done.
Writingobjects:
100%(3/3),321bytes|0bytes/s,done.
Total3(delta0),reused0(delta0)
Tossh:
//git@192.170.100.15:
/home/git/git.repository/test.git
f294530..31b9fe0master->master
上面开发者A增加了一个a.html文件,并push到remoterepository
kursk@KURSK-PC/D/gitworkspace
$cddev_B/
kursk@KURSK-PC/D/gitworkspace/dev_B(master)
$vib.html
kursk@KURSK-PC/D/gitworkspace/dev_B(master)
$gitadd.
kursk@KURSK-PC/D/gitworkspace/dev_B(master)
$gitcommit-a-m"addfileb.html"
[master7262964]addfileb.html
1filechanged,1insertion(+)
createmode100644b.html
kursk@KURSK-PC/D/gitworkspace/dev_B(master)
$gitpush
warning:
push.defaultisunset;itsimplicitvalueischangingin
Git2.0from'matching'to'simple'.Tosquelchthismessage
andmaintainthecurrentbehaviorafterthedefaultchanges,use:
gitconfig--globalpush.defaultmatching
Tosquelchthismessageandadoptthenewbehaviornow,use:
gitconfig--globalpush.defaultsimple
See'githelpconfig'andsearchfor'push.default'forfurtherinformation.
(the'simple'modewasintroducedinGit1.7.11.Usethesimilarmode
'current'insteadof'simple'ifyousometimesuseolderversionsofGit)
Tossh:
//git@192.170.100.15:
/home/git/git.repository/test.git
!
[rejected]master->master(fetchfirst)
error:
failedtopushsomerefsto'ssh:
//git@192.170.100.15:
/home/git/git.repository/test.git'
hint:
Updateswererejectedbecausetheremotecontainsworkthatyoudo
hint:
nothavelocally.Thisisusuallycausedbyanotherrepositorypushing
hint:
tothesameref.Youmaywanttofirstintegratetheremotechanges
hint:
(e.g.,'gitpull...')beforepushingagain.
hint:
Seethe'Noteaboutfast-forwards'in'gitpush--help'fordetails.
当开发者B在开发者A后PUSH时就报错了,原因是B目前的localrepository的版本与remoterepository的版本没有同一个版本节点。
因为开发者A的localrepository和remoterepository的版本一致,所以我们可以开发者A的机器执行gitlog查看日志列表,检查当前remoterepository的版本历史。
kursk@KURSK-PC/D/gitworkspace/dev_A(master)
$gitlog
commit31b9fe04c05ff33c18fe889ca5a12d36bd6dcb71
Author:
yelei.kursk
Date:
WedApr915:
59:
262014+0800
adda.html
commitf2945305c9a619b4d5f74754d0783195c489bf93
Author:
yelei.kursk
Date:
WedApr915:
02:
312014+0800
modifytest.txt
commit8228bf6902acb5bfd849bc7298d3c38e4d59b735
Author:
kursk-ye
Date:
WedApr914:
53:
032014+0800
test2
commite653668d9ad6516fbd7038fe67fcbe0bd9bcf434
Author:
kursk-ye
Date:
WedApr914:
00:
362014+0800
test
remoterepository当前版本号是:
31b9fe04c05ff33c18fe889ca5a12d36bd6dcb71
而开发者B的localrepository呢?
726296473f028b753a87c8a50b0506ef926a3421
kursk@KURSK-PC/D/gitworkspace/dev_B(master)
$gitlog
commit726296473f028b753a87c8a50b0506ef926a3421
Author:
yelei.kursk
Date:
WedApr916:
02:
032014+0800
addfileb.html
commitf2945305c9a619b4d5f74754d0783195c489bf93
Author:
yelei.kursk
Date:
WedApr915:
02:
312014+08
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CI 使用 开发者 GIT 常见 场景
![提示](https://static.bdocx.com/images/bang_tan.gif)