考虑到目前SVN版本管理工具在模块化开发、代码审查及离线开发方面存在的不足,希望通过引入GIT版本管理工具,来解决这些不足,以促进项目的快速迭代、持续集成及代码质量改善。

与SVN相比,GIT存在如下比较显著的优势:

  • 分布式

    相比于SVN,GIT更倾向于分布式模式,也就是每个开发人员从服务器上clone代码后会在自己的机器上克隆一个自己的版本库,这样开发人员可以在本地创建分支,来解决临时的实务,而不耽误已有的工作,同时还可以在离线状态下查看历史版本信息。

  • 强化Branch

    我们知道SVN是通过复制整个工程代码来实现分支的创建,这样不仅耗时,而且不易合并,而GIT则是通过快照机制,可以非常快速地进行分支的创建、切换等。Branch非常有利用模块化开发,可以通过从主干分支新建一个Branch来进行新特性开发,并通过Merge来合并到主分支。

  • 审查

    不像SVN,任何人都可以随地随意地往主分支提交代码,SVN变得更像是一种代码备份工具,GIT增加了Merge Request来强调了代码审查的过程,通过审核机制可以保证代码的干净新鲜。

    若想全面了解GIT与SVN相比的优劣,可以参考此页面.

###选择 既然需要引入GIT作为版本管理工具,那就必须架设GIT服务器,关于GIT服务器,有Github、Gitlab、Gitcafe、BitBucket等,考虑到公司代码的安全性及访问速度,需要在内网构建一个GIT私有服务器,通过了解,Gitlab是构建GIT私有服务器的一个不错选择,首先是开源,公司可以自由使用,其次社区活跃,更新快,可以保证稳定性;下面我将对Gitlab的部署方法进行说明。

##Gitlab服务器部署

###Install Gitlab的部署大体有两种方式:官方installation文档Bitnami集成,关于这两种方法,本人都尝试过,推荐Bitnami集成,原因是问题少、安装方便、可以界面化配置,下面我介绍下Bitnami安装过程的几个要点:

  • 根据你linux系统位数到此下载相应的安装包.
  • 安装过程有GUI和命令行2种配置方式,可以根据个人习惯自由选择.
  • 填写Gitlab管理者帐号相关信息,并请务必记住,以保证后面Web登录.
  • 建议开启邮箱配置,这样方便邮件通知,具体邮箱名称和SMTP主机可根据你自己邮箱服务来设置.
  • 安装过程中可能会遇到端口被占用的情况,此情况有两种解决方案,其一,将占用端口的进程kill(可以通过lsof -i:xx来查看端口占用情况),其二,更换端口.
  • 运行后可能会出现HTTP 500等服务器类型错误,可以通过查看/opt/gitlab-xx.xx.xx-x/apps/gitlab/htdocs/log中的LOG日志来找出具体的原因,然后对症下药.
root@wenva-virtual-machine:/develop# ./bitnami-gitlab-7.3.2-0-linux-x64-installer.run 
----------------------------------------------------------------------------
Welcome to the Bitnami Gitlab Stack Setup Wizard.

----------------------------------------------------------------------------
Select the components you want to install; clear the components you do not want 
to install. Click Next when you are ready to continue.

GitLab : Y (Cannot be edited)

GitLab CI [Y/n] :

Is the selection above correct? [Y/n]: 

----------------------------------------------------------------------------
Installation folder

Please, choose a folder to install Bitnami Gitlab Stack

Select a folder [/opt/gitlab-7.3.2-0]: 

----------------------------------------------------------------------------
Create Admin account

Bitnami Gitlab Stack admin user creation

Email Address [user@example.com]: xuwenfa@star-net.cn

Login [user]: master

Password :
Please confirm your password :
----------------------------------------------------------------------------
Hostname that will be used to create internal URLs. If this value is incorrect, 
you may be unable to access your Gitlab installation from other computers. It is 
advisable to use a Domain instead of an IP address for compatibility with 
different browsers.

Domain [127.0.1.1]: 

Do you want to configure mail support? [y/N]: y

----------------------------------------------------------------------------
Configure SMTP Settings

This is required so your application can send notifications via email.

Default email provider:

[1] GMail
[2] Custom
Please choose an option [1] : 2

----------------------------------------------------------------------------
Configure SMTP Settings

This data is stored in the application configuration files and may be visible to 
others. For this reason, it is recommended that you do not use your personal 
account credentials.

Username []: xuwenfa@star-net.cn

Password :
Re-enter :
SMTP Host []: mail.star-net.cn

SMTP Port []: 25

Secure connection

[1] None
[2] SSL
[3] TLS
Please choose an option [3] : 1

----------------------------------------------------------------------------
Setup is now ready to begin installing Bitnami Gitlab Stack on your computer.

Do you want to continue? [Y/n]: 

----------------------------------------------------------------------------
Please wait while Setup installs Bitnami Gitlab Stack on your computer.

 Installing
 0% ______________ 50% ______________ 100%
 #########################################



----------------------------------------------------------------------------
Setup has finished installing Bitnami Gitlab Stack on your computer.

Info: To access the Bitnami Gitlab Stack, go to
http://127.0.1.1:80 from your browser.
Press [Enter] to continue:

###Config 安装并运行,在提交代码到远程服务器(即git push origin <branch)的时候会出现如下错误:

xuwenfadeMacBook-Pro-2:example wenva$ git push origin master
/opt/gitlab-7.3.2-0/ruby/lib/ruby/2.0.0/net/http.rb:878:in `initialize': Connection refused - connect(2) (Errno::ECONNREFUSED)
	from /opt/gitlab-7.3.2-0/ruby/lib/ruby/2.0.0/net/http.rb:878:in `open'
	from /opt/gitlab-7.3.2-0/ruby/lib/ruby/2.0.0/net/http.rb:878:in `block in connect'
	from /opt/gitlab-7.3.2-0/ruby/lib/ruby/2.0.0/timeout.rb:52:in `timeout'
	from /opt/gitlab-7.3.2-0/ruby/lib/ruby/2.0.0/net/http.rb:877:in `connect'
	from /opt/gitlab-7.3.2-0/ruby/lib/ruby/2.0.0/net/http.rb:862:in `do_start'
	from /opt/gitlab-7.3.2-0/ruby/lib/ruby/2.0.0/net/http.rb:851:in `start'
	from /opt/gitlab-7.3.2-0/apps/gitlab/gitlab-shell/lib/gitlab_net.rb:97:in `post'
	from /opt/gitlab-7.3.2-0/apps/gitlab/gitlab-shell/lib/gitlab_net.rb:27:in `allowed?'
	from /opt/gitlab-7.3.2-0/apps/gitlab/gitlab-shell/lib/gitlab_shell.rb:64:in `validate_access'
	from /opt/gitlab-7.3.2-0/apps/gitlab/gitlab-shell/lib/gitlab_shell.rb:25:in `exec'
	from /opt/gitlab-7.3.2-0/apps/gitlab/gitlab-shell/bin/gitlab-shell:16:in `
' fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. </pre> * **解决方案** 编辑/home/git/gitlab-shell/config.yml,即gitlab_url改为你的IP+端口(默认是http://localhost:8080)
...
gitlab_url: "http://192.168.60.151:80/"
...
![image](http://127.0.0.1:8080/2014-10-23_20-54-18.png =800x500) ##Gitlab使用 ###更换IP * 设置/home/git/gitlab-shell/config.yml的gitlab_url
...
gitlab_url: "http://192.168.60.151:80/"
...
* 设置/opt/gitlab-7.3.2-0/apps/gitlab/htdocs/gitlab_url.yml
...
  gitlab:
    ## Web server settings (note: host is the FQDN, do not include http://)
    host: 192.168.60.151
    port: 80 # Set to 443 if using HTTPS, see installation.md#using-https for additional HTTPS configuration details
    https: false # Set to true if using HTTPS, see installation.md#using-https for additional HTTPS configuration details
...
</font>
###服务启动和停止 Gitlab服务可以通过/opt/gitlab-xx.xx.xx-x/ctlscript.sh来控制,可以通过help来查看帮助说明
root@wenva-desktop:/opt/gitlab-7.3.2-0# ./ctlscript.sh help
usage: ./ctlscript.sh help
       ./ctlscript.sh (start|stop|restart|status)
       ./ctlscript.sh (start|stop|restart|status) mysql
       ./ctlscript.sh (start|stop|restart|status) apache
       ./ctlscript.sh (start|stop|restart|status) redis
       ./ctlscript.sh (start|stop|restart|status) gitlab_sidekiq
       ./ctlscript.sh (start|stop|restart|status) gitlabci_sidekiq
       ./ctlscript.sh (start|stop|restart|status) gitlabci_runner

help       - this screen
start      - start the service(s)
stop       - stop  the service(s)
restart    - restart or start the service(s)
status     - show the status of the service(s)
###备份和恢复 考虑到服务器迁移、代码安全等需求,需要对代码进行实时的备份,Gitlab提供的备份命令backup,下面将具体阐述如何进行备份: * 设置 可以通过编辑vim /opt/gitlab-7.3.2-0/apps/gitlab/htdocs/config/gitlab.yml文件来设定备份路径,找出"Backup settings"
## Backup settings
  backup:
    path: "tmp/backups"   # Relative paths are relative to Rails.root (default: tmp/backups/)
    # keep_time: 604800   # default: 0 (forever) (in seconds)
    
* 备份(Backup) 通过Gitlab自带的备份命令"bundle exec bin/rake gitlab:backup:create RAILS_ENV=production",可以很容易地实现备份.
root@wenva-desktop:/opt/gitlab-7.3.2-0# pwd
/opt/gitlab-7.3.2-0
root@wenva-desktop:/opt/gitlab-7.3.2-0# ./use_gitlab 
sh-4.1# cd apps/gitlab/htdocs/
sh-4.1# bundle exec bin/rake gitlab:backup:create RAILS_ENV=production
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
 Warning 
  You are running as user root, we hope you know what you are doing.
  Things may work/fail for the wrong reasons.
  For correct results you should run this as user git.

Dumping database ... 
Dumping MySQL database bitnami_gitlab ... [DONE]
done
Dumping repositories ...
 * master/ecommunity ... [SKIPPED]
 * wenva/mytest ... [SKIPPED]
 * wenva/xtool ... [SKIPPED]
 * wenva/helloworld ... [SKIPPED]
done
Dumping uploads ... 
done
Creating backup archive: 1413189434_gitlab_backup.tar ... done
Deleting tmp directories ... done
Deleting old backups ... skipping
sh-4.1# ls -al tmp/backups/
total 192
drwxr-xr-x 2 root root  4096 Oct 13 16:37 .
drwxrwxr-x 6 git  git   4096 Oct 11 16:57 ..
-rw-r--r-- 1 root root 40960 Oct 11 16:58 1413008522_gitlab_backup.tar
-rw-r--r-- 1 root root 40960 Oct 11 16:57 1413017852_gitlab_backup.tar
-rw-r--r-- 1 root root 51200 Oct 13 16:36 1413189369_gitlab_backup.tar
-rw-r--r-- 1 root root 51200 Oct 13 16:37 1413189434_gitlab_backup.tar
* 恢复(Restore) 通过Gitlab自带的备份命令"bundle exec bin/rake gitlab:backup:restore RAILS_ENV=production BACKUP=xxxxxx",可以很容易地回复到时间戳xxxxxx对应的状态. ###用户管理 当Gitlab启动后,通过浏览器输入服务器IP,就可以看到如下登录界面,可以利用配置的管理者进行登录,管理者登入Gitlab系统后,点击右上角的"Admin area"可以进入Projects、Users、Groups管理界面.可以对User和Group进行增删改操作. ![image](http://127.0.0.1:8080/markdown/2014-10-13_15-34-40.png) ##开发模型构建 ##GIT命令及客户端