CI使用篇1开发者使用GIT常见场景Word文档格式.docx
《CI使用篇1开发者使用GIT常见场景Word文档格式.docx》由会员分享,可在线阅读,更多相关《CI使用篇1开发者使用GIT常见场景Word文档格式.docx(29页珍藏版)》请在冰豆网上搜索。
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'
(RSA)tothelistofknownhosts.
remote:
Countingobjects:
3,done.
Compressingobjects:
100%(2/2),done.
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:
/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到本地,命令是:
/home/git/git.repository/test.gittest2
test2'
因为公私密匙对的设置上文已经描述,这里不再做说明。
这样在当前目录下就多了一个test2/目录。
(3)修改文件,并上传到localrepository.
这里要先了解git中workspace/index(有时也称为stagingarea)/localrepository三个概念。
首先要知道它们三个有个共同点,都在local也就是在你的笔记本电脑上——这是GIT比SVN上传快的原因。
workspace:
类似SVN的workcopy概念,开发者修改文件后会保存在这个区域。
index:
有时也称为stagingarea,用于存储被修改文件的索引。
localrepositor:
最终保存修改后并commit的文件的空间。
另外还有一个叫remoterepositor,它是所有开发者交互的远程仓库,因为既然localrepositor在每个开发者的本地电脑上,那必然要有一个可供所有开发者交换代码的空间,本例中它的地址就是
下面以修改一个文件为例说明:
kursk@KURSK-PC/D/gitworkspace/test2(master)
$cattest2.txt
gitisgoodtoolforsourcecodemanage
$vitest2.txt
gitisfasterthansvn#我增加了一行
$gitstatus
#Onbranchmaster
#Changesnotstagedforcommit:
#(use"
gitadd<
file>
..."
toupdatewhatwillbecommitted)
gitcheckout--<
todiscardchangesinworkingdirectory)
#
#modified:
test2.txt
nochangesaddedtocommit(use"
gitadd"
and/or"
gitcommit-a"
)
这时workspace已经改变,但是index的内容没有改变,通过gitstatus可以看到“Changesnotstagedforcommit”的提示,并说明修改的内容
操作过程中可以随时使用gitstatus了解修改了哪些文件,以及这些文件存于什么状态。
下一步,我们通过gitadd将修改文件从workspace推到index
$gitadd.#gitadd.注意后面有一个dot,不管修改、新增、删除了多少文件,只要在工程顶级目录执行这个命令,就可以将所有修改内容从workspace推到index
#Changestobecommitted:
gitresetHEAD<
tounstage)
这时index内容已经改变,但是还没有commit到localrepository,通过gitstatus可以看得很清楚。
接下来,我们将修改文件从indexcommit到localrepository
$gitcommit-m"
modifytest2.txt"
#commit命令用于将修改文件从indexcommit到localrepository,这里的-m是说明内容,必须
[master6349e7f]modifytest2.txt
1filechanged,2insertions(+),1deletion(-)
$gitstatus#commit后再查看文件状态,已经没有修改的文件了,这时workspace/index/localrepository三个空间的文件状态完全一致了。
#Yourbranchisaheadof'
origin/master'
by1commit.
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
$gitadd.
$gitcommit-a-m"
adda.html"
[master31b9fe0]adda.html
1filechanged,1insertion(+)
createmode100644A.html
$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'
modewasintroducedinGit1.7.11.Usethesimilarmode
'
current'
insteadof'
ifyousometimesuseolderversionsofGit)
Countingobjects:
4,done.
Deltacompressionusingupto4threads.
Compressingobjects:
Writingobjects:
100%(3/3),321bytes|0bytes/s,done.
Total3(delta0),reused0(delta0)
Tossh:
/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
addfileb.html"
[master7262964]addfileb.html
createmode100644b.html
!
[rejected]master->
master(fetchfirst)
error:
failedtopushsomerefsto'
/home/git/git.repository/test.git'
hint:
Updateswererejectedbecausetheremotecontainsworkthatyoudo
nothavelocally.Thisisusuallycausedbyanotherrepositorypushing
tothesameref.Youmaywanttofirstintegratetheremotechanges
(e.g.,'
gitpull...'
)beforepushingagain.
Seethe'
Noteaboutfast-forwards'
in'
gitpush--help'
fordetails.
当开发者B在开发者A后PUSH时就报错了,原因是B目前的localrepository的版本与remoterepository的版本没有同一个版本节点。
因为开发者A的localrepository和remoterepository的版本一致,所以我们可以开发者A的机器执行gitlog查看日志列表,检查当前remoterepository的版本历史。
$gitlog
commit31b9fe04c05ff33c18fe889ca5a12d36bd6dcb71
Author:
yelei.kursk<
kursk.ye@>
Date:
WedApr915:
59:
262014+0800
adda.html
commitf2945305c9a619b4d5f74754d0783195c489bf93
02:
312014+0800
modifytest.txt
commit8228bf6902acb5bfd849bc7298d3c38e4d59b735
kursk-ye<
WedApr914:
53:
032014+0800
test2
commite653668d9ad6516fbd7038fe67fcbe0bd9bcf434
00:
362014+0800
test
remoterepository当前版本号是:
31b9fe04c05ff33c18fe889ca5a12d36bd6dcb71
而开发者B的localrepository呢?
726296473f028b753a87c8a50b0506ef926a3421
commit726296473f028b753a87c8a50b0506ef926a3421
WedApr916:
addfileb.html
312014+08