g27qip5n69 发表于 2016-2-26 15:04:28

Linux 下一步步安装 Git Server

  版

本控制系统经历 CVS , SVN, 现在到了 Git 的世界。 Git 与 CVS, SVN 相比有很多的优势,其中最为人津津乐道的就是 branch 的切换都是在本地执行,速度非常之快,非常利于分布式开发进程。现在也是开源世界了最流行的版本控制系统。比如:Fedora 社区(http://git.fedorahosted.org/git), JBoss AS 7 的 source(https://github.com/jbossas/jboss-as) 等。
  
  GitHub
提供免费的 Git server, 但是它仅仅针对 Public 的 repository 是免费的,如果你想有你个人的 Git repsotories, 就需要交费了。 但是, 依照国人一向不信他人,只信自己的美德, 应该很少有人把机密的代码交由外人掌管吧,万一 GitHub 被黑了, 那岂不是一起遭殃? 怎么办呢, 自己建立个 Server 不就完了吗。
  

第一步: 安装 Git
  

NOTE:

接下来的操作都是在 Server 机器上运行
  
  Fedora 下运行:

yum install git-core
  
  Ubuntu 下运行:

apt-get install git-core
  
  

第二步: 初始化一个 repository
  

$ cd /home/lgao/sources/my_own/repositories/
$ git init --bare myprj
Initialized empty Git repository in /home/lgao/sources/my_own/repositories/myprj/.git/
  
  指定 --bare,当前 repository 下就只有 .git/ 下的 objects,而没有真实文件。一般在 Server 端。
  

第三步: 初始化提交
  
  当一个 repository 初始化后, 里面什么也没有。我们需要建立个 master branch,才能被正常 clone 到其他地方。

$ mkdir initial.commit
$ cd initial.commit/
$ git init
Initialized empty Git repository in /home/lgao/sources/my_own/myclient/initial.commit/.git/
$ git remote add origin /home/lgao/sources/my_own/repositories/myprj/
$ touch Readme
$ git add Readme
$ git commit -m "initial commit"
initial commit
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 Readme
$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 206 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /home/lgao/sources/my_own/repositories/myprj/
*       master -> master

 第四步  正常使用
  
  经过初始化提交过程,相当于激活了该 repository, 我们可以正常使用了。

$ git clone /home/lgao/sources/my_own/repositories/myprj/
Cloning into 'myprj'...
done.
$ cd myprj
$ l
total 0
-rw-rw-r-- 1 lgao lgao 0 Jan 21 15:15 Readme
$ vim Readme
$ git commit -m "modify readme" Readme
modify readme
1 files changed, 1 insertions(+), 0 deletions(-)
$ git push
Counting objects: 5, done.
Writing objects: 100% (3/3), 247 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /home/lgao/sources/my_own/repositories/myprj/
032dad8..1bf69b4master -> master

   

 第五步   与他人共享 Repository
  
  按照上面的步骤,我们已经可以使用 Git 了。 但是 Git repository 的 URL 都是本地目录, 怎么能方便的让合作伙伴方便的访问该 Repository 呢? 这就要介绍下 Git 支持的 4 中传输协议了: Local, SSH, Git, HTTP。 我们一个个的介绍:

Local
  Local 就是指本地文件系统,可以是本机器文件,也可以是通过 NFS 加载的网络映射。这种情形下与人共享就是把你的 repository 所在的目录作为 NFS 共享出去,让能访问到你机器的人像本地文件一样操作。具体怎么 NFS 共享超出本文范围,不加讨论(有兴趣的请参考: http://docs.fedoraproject.org/en-US/Fedora/14/html/Storage_Administration_Guide/ch-nfs.html
)。不过不推荐这种方式共享, 因为效率太低。

SSH
  SSH 协议大家都很熟悉,只要你的 Server 能 ssh 登录就可以。假设你的 Server 可以远程 ssh 登录,我们看如何从 Server clone 出 Git Repository:


$ git clone lgao@10.66.14.143:git_repos/myprj from_remote
Cloning into 'from_remote'...
lgao@10.66.14.143's password:
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.


 
也就是说只要你在 Server 端给一个用户创建完帐号后, 他就可以 clone 代码了。 具体的权限设置和 Linux 下权限配置是一样的。 一般跟一组 repositories 指定一个新的 group, 把新建的用户加入到该 group 后就可以有相应的读写权限了。
   还有另外一种方式,是通过提交公钥到 Server 端来获得访问和提交权限,而不需要创建用户。 具体细节本文不加讨论。


Git
  该协议一般只是只读权限。
  
  第一步需要安装 git-daemon:

# yum install git-daemon
  
  接着启动 git-daemon:

$ git daemon --base-path=/home/lgao/sources/my_own/repositories --export-all
  
  在你的工程下创建 git-daemon-export-ok 文件:


$ cd myprj
$ touch git-daemon-export-ok
  
  现在就可以通过 Git 协议 clone repository 了:

$ git clone git://lgao.nay.redhat.com/myprj
Cloning into 'myprj'...
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (6/6), done.
HTTP
  该协议一般只是只读权限。GitWeb 包提供 CGI , 它可以被部署到任何支持静态 web 服务的服务器中去。我们还以最常见的 Apache 为例:
  
  版本信息:

Httpd:Apache/2.2.21 (Fedora)
Git:git version 1.7.7.5
GitWeb:1.7.7.5-1.fc16
   安装 Httpd 和 gitweb:

$ sudo yum install httpd gitweb

   修改 /etc/gitweb.conf:

$ vim /etc/gitweb.conf
修改:
our $projectroot = "/home/lgao/sources/my_own/repositories";

  
  在 httpd 的 DocumentRoot 所在目录创建 Link 文件:

# cd /var/www/html
# ln -s /home/lgao/sources/my_own/repositories/ git
# chown -R lgao.lgao git
   修改 httpd.conf 中的 user 和 group:

User lgao
Group lgao
  
  重启 httpd:

# service httpd restart
Restarting httpd (via systemctl):                        
  
  OK 了, 现在就可以访问了:

  
  你可以修改 Server 机器下 <Your Repo>/description ,这样可以显示在 git web 页面上。
  
  修改 <Your Repo>/config , 加上 owner 和 url:
  
  


owner = Your Name <name@example.com>
url = git://lgao.nay.redhat.com/myprj
   
  之后,我们再看看截图:

  
  
  
页: [1]
查看完整版本: Linux 下一步步安装 Git Server