目前,流行的用于代码版本管理的工具有:

1、SVN

SVN属于集中式的版本控制系统,都有一个单一的集中管理的服务器,保存所有文件的修订版本,而协同工作的人们都通过客户端连到这台服务器,取出最新的文件或者提交更新。

1.1、SVN的特点概括起来主要由以下几条:

1)每个版本库有唯一的URL(官方地址),每个用户都从这个地址获取代码和数据;
2)获取代码的更新,也只能连接到这个唯一的版本库,同步以取得最新数据;
3)提交必须有网络连接(非本地版本库);
4)提交需要授权,如果没有写权限,提交会失败;
5)提交并非每次都能够成功。如果有其他人先于你提交,会提示”改动基于过时的版本,先更新再提交”… 诸如此类;
6)冲突解决是一个提交速度的竞赛:手快者,先提交,平安无事;手慢者,后提交,可能遇到麻烦的冲突解决。
好处:每个人都可以一定程度上看到项目中的其他人正在做些什么。而管理员也可以轻松掌控每个开发者的权限。
缺点:中央服务器的单点故障。
若是宕机一小时,那么在这一小时内,谁都无法提交更新、还原、对比等,也就无法协同工作。如果中央服务器的磁盘发生故障,并且没做过备份或者备份得不够及时的话,还会有丢失数据的风险。最坏的情况是彻底丢失整个项目的所有历史更改记录,被客户端提取出来的某些快照数据除外,但这样的话依然是个问题,你不能保证所有的数据都已经有人提取出来。
简单来说,SVN原理上只关心文件内容的具体差异。每次记录有哪些文件作了更新,以及都更新了哪些行的什么内容。

1.2、SVN的搭建和使用

注意版本库所在的目录为:/data/svn

1.检查已安装版本
  1. rpm -qa subversion


卸载旧版本SVN

  1. yum remove subversion

2、修改源

CentOS6.6 默认yum源安装的subversion版本为1.6.11,所以先要创建SVN的yum源。

2.1、创建一个yum repo文件
  1. vi /etc/yum.repos.d/wandisco-svn.repo

2.2、添加如下内容
  1. [WandiscoSVN]
  2. name=Wandisco SVN Repo
  3. #注意:$releasever为linux系统版本
  4. baseurl=http://opensource.wandisco.com/centos/$releasever/svn-1.9/RPMS/$basearch/
  5. enabled=1
  6. gpgcheck=0

2.3、清楚源缓存
  1. yum clean all

2.4、查看SVN源的版本
  1. yum list | grep subversion | grep 1.9

3、安装SVN
  1. yum install -y subversion

4、验证安装
  1. svnserve --version

5、创建版本库
  1. mkdir -p /data/svn/
  2. svnadmin create /data/svn/data-center

6、配置用户及密码

密码的生成可以使用如下命令

  1. htpasswd /data/svn/data-center/conf/passwd username
  1. vi /data/svn/data-center/conf/passwd

参考如下内容:

  1. ### This file is an example password file for svnserve.
  2. ### Its format is similar to that of svnserve.conf. As shown in the
  3. ### example below it contains one section labelled [users].
  4. ### The name and password for each user follow, one account per line.
  5. [users]
  6. user1=k0YpEdfYFQJaUK86QgC

7、配置权限控制
  1. vi /data/svn/data-center/conf/authz

参考如下内容:

  1. [groups]
  2. data-center_m = user1,user2
  3. data-center_g = user1,user2
  4. data-center_zps_g = user1,user2
  5. data-center_op_g = user1,user2
  6. data-center_dw_g = user1,user2
  7. data-center_dev_g = user1,user2
  8. data-center_prd_g = user1,user2
  9. data-center_back = user1,user2
  10. data-center_it = user1,user2
  11. [data-center:/]
  12. @data-center_m= rw
  13. [data-center:/管理]
  14. @data-center_m= rw
  15. [data-center:/ZPS]
  16. @data-center_zps_g= rw
  17. [data-center:/部门日常事务]
  18. @data-center_m= rw
  19. @data-center_g= rw
  20. [data-center:/运营分析组]
  21. @data-center_m= rw
  22. @data-center_op_g= rw
  23. [data-center:/数据仓库组]
  24. @data-center_m= rw
  25. @data-center_dw_g= rw
  26. @data-center_prd_g= rw
  27. [data-center:/数据仓库组/01文档/04详细设计]
  28. @data-center_op_g=r
  29. [data-center:/应用开发组]
  30. @data-center_m= rw
  31. @data-center_dev_g= rw
  32. @data-center_dw_g= r
  33. [data-center:/产品设计组]
  34. @data-center_m= rw
  35. @data-center_dev_g= rw
  36. @data-center_prd_g= rw
  37. [data-center:/项目中心]
  38. @data-center_m= rw
  39. @data-center_dev_g= rw
  40. @data-center_prd_g= rw
  41. @data-center_dw_g= rw
  42. @data-center_op_g= rw
  43. [data-center:/IT组]
  44. @data-center_m= rw
  45. @data-center_it= rw

8、服务配置
  1. vi /data/svn/data-center/conf/svnserve.conf

修改如下内容

  1. [general]
  2. ### These options control access to the repository for unauthenticated
  3. ### and authenticated users. Valid values are "write", "read",
  4. ### and "none". The sample settings below are the defaults.
  5. #去出注释
  6. anon-access = none
  7. auth-access = write
  8. ### The password-db option controls the location of the password
  9. ### database file. Unless you specify a path starting with a /,
  10. ### the file's location is relative to the directory containing
  11. ### this configuration file.
  12. ### If SASL is enabled (see below), this file will NOT be used.
  13. ### Uncomment the line below to use the default password file.
  14. #去除注释
  15. password-db = passwd
  16. ### The authz-db option controls the location of the authorization
  17. ### rules for path-based access control. Unless you specify a path
  18. ### starting with a /, the file's location is relative to the the
  19. ### directory containing this file. If you don't specify an
  20. ### authz-db, no path-based access control is done.
  21. ### Uncomment the line below to use the default authorization file.
  22. #去除注释
  23. authz-db = authz
  24. ### This option specifies the authentication realm of the repository.
  25. ### If two repositories have the same authentication realm, they should
  26. ### have the same password database, and vice versa. The default realm
  27. ### is repository's uuid.
  28. #修该其为版本库地址
  29. realm = /data/svn/data-center
  30. [sasl]
  31. ### This option specifies whether you want to use the Cyrus SASL
  32. ### library for authentication. Default is false.
  33. ### This section will be ignored if svnserve is not built with Cyrus
  34. ### SASL support; to check, run 'svnserve --version' and look for a line
  35. ### reading 'Cyrus SASL authentication is available.'
  36. # use-sasl = true
  37. ### These options specify the desired strength of the security layer
  38. ### that you want SASL to provide. 0 means no encryption, 1 means
  39. ### integrity-checking only, values larger than 1 are correlated
  40. ### to the effective key length for encryption (e.g. 128 means 128-bit
  41. ### encryption). The values below are the defaults.
  42. # min-encryption = 0
  43. # max-encryption = 256

9、启动SVN
  1. svnserve -d -r /data/svn

10、注意:

1、可能涉及到防火墙设置
  1. subversion默认端口 3690
  2. 有开启3690端口的命令,在终端输入以下命令:
  3. 1>iptables -I INPUT -i eth0 -p tcp --dport 3690 -j ACCEPT
  4. 2>iptables -I OUTPUT -o eth0 -p tcp --sport 3690 -j ACCEPT
  5. 然后保存:etc/rc.d/init.d/iptables save
  6. 在看看是否已经有了360端口的开放权限:/etc/init.d/iptables status

3、SVN日志文件显示时间为1970-01-01 No data
  1. SVN服务器上打开svnserve.conf文件,将其中名为anon-access的一项设置为 anon-access = none

2、Git

Git属于分布式的版本控制系统,实际上,Git 更像是把变化的文件作快照后,记录在一个微型的文件系统中。每次提交更新时,它会纵览一遍所有文件的指纹信息并对文件作一快照,然后保存一个指向这次快照的索引。为提高性能,若文件没有变化,Git 不会再次保存,而只对上次保存的快照作一连接。Git还支持离线提交更新,等到了有网络的时候再上传到远程的镜像仓库。

2.1、Git的特点概括起来主要由以下几条:

1)Git中每个克隆(clone)的版本库都是平等的。你可以从任何一个版本库的克隆来创建属于你自己的版本库,同时你的版本库也可以作为源提供给他人,只要你愿意。
2)Git的每一次提取操作,实际上都是一次对代码仓库的完整备份。
3)提交完全在本地完成,无须别人给你授权,你的版本库你作主,并且提交总是会成功。
4)甚至基于旧版本的改动也可以成功提交,提交会基于旧的版本创建一个新的分支。
5)Git的提交不会被打断,直到你的工作完全满意了,PUSH给他人或者他人PULL你的版本库,合并会发生在PULL和PUSH过程中,不能自动解决的冲突会提示您手工完成。
6)冲突解决不再像是SVN一样的提交竞赛,而是在需要的时候才进行合并和冲突解决。

2.2、Git的搭建和配置:

本文搭建的gitlab是基于yum的方式进行安装的,具体的安装步骤如下:

1、配置yum源
  1. vim /etc/yum.repos.d/gitlab-ce.repo

2、复制如下内容并保存

注意baseurl的配置是centos7的地址
  1. [gitlab-ce]
  2. name=gitlab-ce
  3. baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/
  4. Repo_gpgcheck=0
  5. Enabled=1
  6. Gpgkey=https://packages.gitlab.com/gpg.ke
  7. 如果是centos6的,baseurl修改为
  8. [gitlab-ce]
  9. name=gitlab-ce
  10. baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el6/
  11. Repo_gpgcheck=0
  12. Enabled=1
  13. Gpgkey=https://packages.gitlab.com/gpg.ke

3、更新本地yum缓存
  1. sudo yum makecache

4、yum安装gitlab,此处安装版本是11.9.8
  1. sudo yum intall gitlab-ce #自动安装最新版
  2. sudo yum install gitlab-ce-11.9.8 #安装指定版本,此处为10.2.2
  3. #注意安装的过程中可能会报:
  4. #这个问题简单总结了一下!
  5. #GPG key retrieval failed: [Errno 14] PYCURL ERROR 7 - "couldn't connect to host"
  6. #简单的翻译:GPG密钥检索失败:[14] PYCURL errno的错误 - “无法连接到主机”
  7. #可以使用如下命令进行跳过
  8. sudo yum install gitlab-ce-11.9.8 --nogpgcheck ####跳过校验

5、更改配置
  1. vim /etc/gitlab/gitlab.rb

参考如下内容:

  1. 找到 external_url 'http://000.00.00.00:8081'
  2. 修改成你的地址,此处为http://192.168.1.157:82
  3. 找到 unicorn['port'] = 8091
  4. 修改其他端口,可能9090被其他的进程占用了。
  5. 注意:这个/etc/gitlab/gitlab.rb很多端口都需要检查下是否有被占用。

6、对GitLab进行编译
  1. gitlab-ctl reconfigure

7、清除缓存
  1. gitlab-rake cache:clear RAILS_ENV=production

8、启动gitlab服务,以及关闭防火墙等操作
  1. sudo gitlab-ctl start # 启动所有 gitlab 组件;并加入开机启动项里面:/etc/rc.local
  2. sudo gitlab-ctl stop # 停止所有 gitlab 组件;
  3. sudo gitlab-ctl restart # 重启所有 gitlab 组件;
  4. sudo gitlab-ctl status # 查看服务状态;
  5. sudo gitlab-ctl tail # 查看日志;
  6. #关闭防火墙:
  7. service iptables stop ###centos6关闭防火墙
  8. systemctl stop firewalld.service ###centos7关闭防火墙

9、访问gitlab
  1. http://192.168.1.157:82
  2. gitlab默认的root账号,首次进入需要修改root密码

10、全局配置
  1. git config --global user.email "user1@xxx.com"
  2. git config --global user.name "张三"