Ubuntu使用git来管理代码
首先安装:
sudoapt-getinstallgit-core
sudoapt-getinstallopenssh-server
sudoapt-getinstallgitosis
更改python默认版本
sudorm/usr/bin/python
sudoln-spython2.6/usr/bin/python
然后:
增加名为Git的用户,命令行禁用密码
sudoadduser--system--shell/bin/sh--gecos'gitversioncontrol'--group--disabled-password--home/home/gitgit
因为git需要ssh验证,所以在本地机器上生成sshkey
默认会在~/.ssh下生成两个文件
私钥(本地保存)id_rsa
公钥(上传服务器)id_rsa.pub
ssh-keygen-trsa
ls~/.ssh/
id_rsaid_rsa.pubknown_hosts
上传公钥到USER用户的根目录下
scp.ssh/id_rsa.pubUSER@YOUR_SERVER:
sudo-H-ugitgitosis-init</home/USER/id_rsa.pub
sudochmod755/home/git/repositories/gitosis-admin.git/hooks/post-update
如果有密钥,或迁移密钥到本地,会需要下面的步骤,(默认可忽略此步骤)
#启动ssh-agent,
ssh-agent
#添加id_rsa到ssh-agent
ssh-addid_rsa
到这一步,基本上都安装好了,可以直接到本地进行测试
gitclonegit@YOUR_SERVER:gitosis-admin.git
--------------------------
首先我们在服务器上初始化一个git项目:
[root@li96-10repository]#mkdirblog.git
[root@li96-10repository]#cdblog.git/
[[email protected]]#gitinit--bare#一定要加上--bare,且该目录不能有任何文件和目录,否则报错:fatal:Outofmemory?mmapfailed:Nosuchdevice
InitializedemptyGitrepositoryin/usr/local/system/repository/blog.git/
接下来我们在创建一个git项目:
[[email protected]]#cd..
[root@li96-10repository]#mkdirtest.git
[root@li96-10repository]#cdtest.git/
[[email protected]]#echo"somecontent">>file
[[email protected]]#gitaddfile
fatal:Notagitrepository(oranyoftheparentdirectories):.git
[[email protected]]#gitinit
InitializedemptyGitrepositoryin/usr/local/system/repository/test.git/.git/
[[email protected]]#gitaddfile
[[email protected]]#gitcommit-am"firstcommit"
[master(root-commit)3ea3176]firstcommit
1fileschanged,1insertions(+),0deletions(-)
createmode100644file
将test.gitpush到blog.git项目上
[[email protected]]#gitremoteaddorigin../blog.git
[[email protected]]#gitpushoriginmaster
Countingobjects:3,done.
Writingobjects:100%(3/3),213bytes,done.
Total3(delta0),reused0(delta0)
Unpackingobjects:100%(3/3),done.
To../blog.git
*[newbranch]master->master
现在我们从blog.git中clone出来:
[[email protected]]#cd..
[root@li96-10repository]#gitcloneblog.git#因为我们的reposi是blog.git,clone下来后就会是blog了
InitializedemptyGitrepositoryin/usr/local/system/repository/blog/.git/
[root@li96-10repository]#cdblog
[root@li96-10blog]#ll
total4
-rw-r--r--1rootroot13Jan422:09file
可以看到项目中果然有个file文件,是刚才我们push进去的
那么下面我们在新clone的branchblog里面增加一个文件看看,
[root@li96-10blog]#echo'Secondfilecontents'>file2
[root@li96-10blog]#gitaddfile2
[root@li96-10blog]#gitcommit-am'addedsecondfile'
[master63be855]addedsecondfile
1fileschanged,1insertions(+),0deletions(-)
createmode100644file2
[root@li96-10blog]#gitstatus
#Onbranchmaster
#Yourbranchisaheadof'origin/master'by1commit.
#
nothingtocommit(workingdirectoryclean)
然后我们push到blog.git
[root@li96-10blog]#gitpush
Countingobjects:4,done.
Deltacompressionusingupto4threads.
Compressingobjects:100%(2/2),done.
Writingobjects:100%(3/3),280bytes,done.
Total3(delta0),reused0(delta0)
Unpackingobjects:100%(3/3),done.
To/usr/local/system/repository/blog.git
3ea3176..63be855master->master
这样的话,我们的blog.git已经有两个文件了file,file2
下面我们进到test.git里面,并且将刚才在first的修改合并过来:
[root@li96-10blog]#cd../test.git/
[[email protected]]#gitfetchorigin
remote:Countingobjects:4,done.
remote:Compressingobjects:100%(2/2),done.
remote:Total3(delta0),reused0(delta0)
Unpackingobjects:100%(3/3),done.
From../blog
3ea3176..63be855master->origin/master
[[email protected]]#gitmergeorigin/master
Updating3ea3176..63be855
Fastforward
file2|1+
1fileschanged,1insertions(+),0deletions(-)
createmode100644file2
我们可以看到,已经将在first的修改合并过来了,在上面的操作中我们用了fetch和merge,其实我们可以将两个命令简化为一个pull命令:
[[email protected]]#gitpulloriginmaster
From../blog
*branchmaster->FETCH_HEAD
Alreadyup-to-date.
总结一下,将版本库拿下来的命令是pull,而将本地修改与版本库合并的命令是push.并且push之前先要pull哦!
当然了,这些操作都是在一台机器上操作的,下面演示一下使用ssh协议连线到真正的remote机器:
从remote上clone下来:
[root@masterrepository]#gitclonessh://[email protected]/path/to/repository/blog.git
InitializedemptyGitrepositoryin/root/repository/blog/.git/
remote:Countingobjects:1055,done.
remote:Compressingobjects:100%(822/822),done.
remote:Total1055(delta115),reused1055(delta115)
Receivingobjects:100%(1055/1055),1.29MiB|19KiB/s,done.
Resolvingdeltas:100%(115/115),done.
[root@masterrepository]#ll
总计4
drwxr-xr-x17rootroot409601-0520:57blog
然后,当我们在本地做了修改,想要提交到remote的时候,我们就可以直接push了:
[root@masterrepository]#cdblog/
[root@masterblog]#gitpush
Everythingup-to-date
这样就基本OK了!
提交之后,在另外一个客户端通过gitpull就可以更新到代码了。
附上参考资料:
http://www.stylejar.com/archives/ubuntu_install_git_server.html
http://xf986321.iteye.com/blog/702974
http://www.linuxsir.org/bbs/thread281294.html
git原理http://www.iteye.com/topic/732199