---
title: "Git简介以及Git在CentOS上设置共享Repository"
author: "like.eagle"
date: "2012-02-01T16:29:00Z"
tags: []
categories: ["研发管理"]
slug: "git-git-centos-repository"
draft: false
lastmod: "2012-02-01T16:29:00Z"
id: 1316988
url: "https://like-eagle.iteye.com/blog/1316988"
---

转载：[http://denger.iteye.com/blog/1010692](http://denger.iteye.com/blog/1010692)

 

   一直使用SVN至今，现在正好 SVN 的服务器硬盘挂掉了，再加上SVN早就用着不是很爽了*(由其是在每一个目录下都会生成.svn目录、另外它的分支管理和切换更是麻烦)*，于是便尝试将公司的SCM工具 SVN切换至 [GIT](http://git-scm.com/)。对于GIT以前主要还是在github上fetch、pull开源代码时用用，很少把它用到实际工作管理中，这里是 [Why Git is Better than X](http://whygitisbetterthanx.com/)，可以看出GIT比起同类SCM确实不错，现在很多知名的项目都已经投向了git，如eclipse、ROR、andorid、iteye等等之类的。 

- Git 简单介绍
  
  安装 Git 至Centos5
  
  创建团队共享资源库
  
  Git客户端工具 git-Tortoise-windows
  
  Git hub
  
  Git常用命令

Git 简单介绍   
     Git作为SCM工具与SVN最大的一个特点就是，SVN是基于服务器，而Git是分布式的(Everything is Local理念)。这便意味每个开发者本地都可以是"git server" 库，能够\[color=red]**离线**在本地进行版本管理，每个人都可以不受任何影响地维护自己的本地资源库（如果你希望的话，也可以合并他人或共享给其他人）。而SVN是每个人写完代码后都及时的checkin到服务器上，然后再checkout，然后再进行合并，在过程都需要依赖于服务器。   
     另外Git的好处实际上还有很多，可以看看 [http://whygitisbetterthanx.com](http://whygitisbetterthanx.com/)、[5-fundamental-differences-between-git-svn](http://www.aqee.net/2010/10/20/5-fundamental-differences-between-git-svn/)，其中还包括了与一些SCM工具如 hg bzr svn perforce 进行对比。 

安装 Git 至Centos5   
     在centos上安装git也是非常简单。几个简单的命令就可以搞定，我这里是centos5.5- x86\_64- 2.6.18 core。   
    

Shell代码     [![收藏代码](http://denger.iteye.com/images/icon_star.png)](http://denger.iteye.com/blog/1010692 "收藏这段代码")

1. yum install git

     如果提示无法找到 git package的话，则执行以下命令，加入源信息:   
    

Shell代码     [![收藏代码](http://denger.iteye.com/images/icon_star.png)](http://denger.iteye.com/blog/1010692 "收藏这段代码")

1. vi /etc/yum.repos.d/epel.repo

     将以下信息粘贴至该文件中:   
    

Conf代码     [![收藏代码](http://denger.iteye.com/images/icon_star.png)](http://denger.iteye.com/blog/1010692 "收藏这段代码")

01. \[epel]
02. name=Extra Packages for Enterprise Linux 5 - $basearch
03. #baseurl=http://download.fedoraproject.org/pub/epel/5/$basearch
04. mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=epel-5&arch=$basearch
05. failovermethod=priority
06. enabled=1
07. gpgcheck=1
08. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL
09. \[epel-debuginfo]
10. name=Extra Packages for Enterprise Linux 5 - $basearch - Debug
11. #baseurl=http://download.fedoraproject.org/pub/epel/5/$basearch/debug
12. mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=epel-debug-5&arch=$basearch
13. failovermethod=priority
14. enabled=0
15. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL
16. gpgcheck=1
17. \[epel-source]
18. name=Extra Packages for Enterprise Linux 5 - $basearch - Source
19. #baseurl=http://download.fedoraproject.org/pub/epel/5/SRPMS
20. mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=epel-source-5&arch=$basearch
21. failovermethod=priority
22. enabled=0
23. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL
24. gpgcheck=1

    然后再执行： 

Shell代码     [![收藏代码](http://denger.iteye.com/images/icon_star.png)](http://denger.iteye.com/blog/1010692 "收藏这段代码")

1. yum install git git-daemon

    如果安装完成之后提示了以下红色信息:   
    Downloading Packages:   
    (1/3): perl-Error-0.17010-1.el5.noarch.rpm                              |  26 kB     00:00       
    (2/3): perl-Git-1.7.4.1-1.el5.x86\_64.rpm                                   |  28 kB     00:00       
    (3/3): git-1.7.4.1-1.el5.x86\_64.rpm                                            | 4.5 MB     01:07       
    --------------------------------------------------------------------------------------------------------------------------------------------  
    Total                                                                                             68 kB/s | 4.6 MB     01:09       
    warning: rpmts\_HdrFromFdno: Header V3 DSA signature: NOKEY, key ID 217521f6   
    GPG key retrieval failed: \[Errno 5] OSError: \[Errno 2] No such file or directory: '/etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL' 

    原因是因为/etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL文件不存在，执行以下命令重新安装即可:   
    

Shell代码     [![收藏代码](http://denger.iteye.com/images/icon_star.png)](http://denger.iteye.com/blog/1010692 "收藏这段代码")

1. cd /etc/pki/rpm-gpg;wget http://download.fedora.redhat.com/pub/epel/RPM-GPG-KEY-EPEL; yum install git git-daemon;

     如果一切安装完成的话输入下面命令，即可以看到当前git版本：   
    

Shell代码     [![收藏代码](http://denger.iteye.com/images/icon_star.png)](http://denger.iteye.com/blog/1010692 "收藏这段代码")

1. git --version

创建共享资源库   
     虽然刚才说道Git是“Everything is Local”，但是在实际的工作项目开发中，还是需要为每项目建立一个 Git 中央资源库，方便term中的开发人员进行开发协作。   
     创建一个repository也是非常简单的，只需要新建一个目录，然后使用 git init 即可：   
    

Shell代码     [![收藏代码](http://denger.iteye.com/images/icon_star.png)](http://denger.iteye.com/blog/1010692 "收藏这段代码")

1. cd /usr/local/;mkdir repos;cd repos;
2. git init

    Initialized empty Git repository in /usr/local/repos/.git/   
    提示上以语句表示一个本地的 repository 已经创建好了，怎么样？简单吧。   
    当第一次创建好respository之后，如果我们将一些已有工程导入至该仓库中(类似svn 中的import操作)，只需要将工程目录copy至该 repository 下，然后通过:   
   

Shell代码     [![收藏代码](http://denger.iteye.com/images/icon_star.png)](http://denger.iteye.com/blog/1010692 "收藏这段代码")

1. git add .
2. git commit

   在commit时会提示让你加入提交注释，填写完注释之后直接 :wq 退出 vi 后, git 会为你自动commit。当然你也可以直接使用 -m 参数添加注释，一步完成提交。   
   如果 add的时候希望根据规则指定的部分文件，可通过:   
  

Shell代码     [![收藏代码](http://denger.iteye.com/images/icon_star.png)](http://denger.iteye.com/blog/1010692 "收藏这段代码")

1. git add -l .

    Add untracked&gt;&gt; ?   
    Prompt help:   
    1              - select a single item   
    3-5          - select a range of items   
    2-3,6-9   - select multiple ranges   
    foo           - select item based on unique prefix   
    -...           - unselect specified items   
    *              - choose all items   
                    - (empty) finish selecting   
    git会列出所有untracked的文件，然后你可以用以上各种形式加入文件，如根据文件编号、根据前缀、正则等。 

    当我们的repository创建好之后，你可以在团队内共享该资源库。意思是让其它人能够 对该资源库进行pull(checkout) 或 push(checkin)操作。对于git来说，有很多方式可以达到该目的([这篇文章列出来8种方式](http://www.jedi.be/blog/2009/05/06/8-ways-to-share-your-git-repository/))。另外SVN共享respository的方式基于 Apache DAV 模块方式；当然 git 也是支持这种方式的，不过我觉得最简单的方式是使用 git deamon的方式，当然对于linuxer 开发者来莫过于 ssh方式比较适合。如果我们希望刚才的 respository 被其它人能够访问到，可通过使用以下命令启动git服务：   
   

Shell代码     [![收藏代码](http://denger.iteye.com/images/icon_star.png)](http://denger.iteye.com/blog/1010692 "收藏这段代码")

1. git daemon --reuseaddr --base-path=/usr/local/repos --export-all --verbose --enable=receive-pack &

    该命令会共享/usr/local/repos 目录下的所有git repository，比如我在该目录下创建了一个 "gitTest" repository，如果其它成员需要pull(checkout)的话则使用：   
    

Shell代码     [![收藏代码](http://denger.iteye.com/images/icon_star.png)](http://denger.iteye.com/blog/1010692 "收藏这段代码")

1. git clone git://192.168.1.115/gitTest

     Cloning into gitTest...   
     remote: Counting objects: 3, done.   
     remote: Total 3 (delta 0), reused 0 (delta 0)   
     Receiving objects: 100% (3/3), done.   
    完成之后在当前目录clone一个名为 gitTest 的本地repository，在这个目录中可进行 pull 或 push 等任何操作，关于 repository 目录结构分析可以看看: [http://book.git-scm.com/1\_git\_directory\_and\_working\_directory.html](http://book.git-scm.com/1_git_directory_and_working_directory.html)   
     不得不说的是使用 git-daemon 方式话对于repository的用户、权限、访问控制等没有 ssh 或 apache DAV 全面。  
     

     Git客户端工具 gitTortoise-windows   
     对于在windows下使用过 svn 来说 Tortoise 肯定是不会陌生的，没错，它现在也有了for git 的Tortoise，可以从[http://code.google.com/p/tortoisegit/](http://code.google.com/p/tortoisegit/)找到它。当然在使用 tortoisegit 时它只作为一个git 的 gui tools，你仍然需要在windows 安装 git for windows. 

    ![](http://www.davegardner.me.uk/blog/wp-content/uploads/2010/01/tortoisegit.jpg)   
    

    Git hub   
    这里需要值的一提的是 [Git Hub](http://github.com/)，GitHub可以托管各种git库，并提供一个web界面，但与其它像 SourceForge或Google Code这样的服务不同，GitHub的独特卖点在于从另外一个项目进行分支的简易性。为一个项目贡献代码非常简单︰首先点击项目站点的“fork”的按 钮，然後将代码检出并将修改加入到刚才分出的代码库中，最後通过内建的“pull request”机制向项目负责人申请代码合并。已经有人将GitHub称为代码玩家的MySpace︰   
　在GitHub进行分支就像在Myspace（或Facebook \[...]）进行交友一样，在社会关系图的节点中不断的连线。(参考百度百科) 

  

     Git常用命令   
     对于git的常用命令，就不再 copy 了，可以收藏以下url，需要的话可以查阅，当然最好用的莫过于  **git help** 来的快。:   
     [http://book.git-scm.com/index.html](http://book.git-scm.com/index.html)   
     [http://git.or.cz/course/svn.html](http://git.or.cz/course/svn.html)   
     [http://www.kernel.org/pub/software/scm/git/docs/everyday.html](http://www.kernel.org/pub/software/scm/git/docs/everyday.html)   
     [http://gitready.com/](http://gitready.com/)