====git简单介绍:版本控制====

版本控制系统基本概念

repository  存放所有文件及其历史信息

checkout  取出或切换到指定版本的文件

version  记录标识一个版本(编号或其他代码)

tag    记录标识一个主要版本(1.0)


本地化版本控制系统:RCS

集中化版本控制系统:CVS,SVN

优点:适合多人团队协作开发,代码集中化管理,所有开发人员代码提交到一台CVS服务器上集中化管理 

缺点: 单点故障(服务器数据库挂掉数据丢失);必须连网工作,无法单机本地工作

分布式版本控制系统:

除了集中式版本的优点,还能解决单点问题及可以离线工作,每个计算机都是一个完整仓库。

spacer.gif

wKiom1RYkzugKpi3AADehG0jziI535.jpg

wKioL1RYk5uhSa5FAAFVPEG6gbY786.jpg


====git安装====

最开始使用的是centos6.2系统安装,按照官网安装过程各种因为包版本不兼容问题刨下很多坑,各种填坑各种补丁浪费时间,快接近真相时最后还是失败。无奈下改用ubuntu系统,感谢Key. Posted博文的记录,但还是遇到了一些坑,如果按照以下笔记中一步步走下去基本可以保证一路顺风。


环境说明:

ubuntu系统

mysql数据库

nginx作为web服务器

redis 缓存

git     git-lab    gitlab-shell


安装笔记

为了解除依赖包带来的麻烦,建议同志们将apt源更新成中科大或者网易的源。
中科大的镜像源站点:

http://mirrors.ustc.edu.cn/

网易的镜像源站点:

http://mirrors.163.com/


以下操作采用root帐号或具有sudo权限的帐号执行

1.更新系统package

apt-get update

apt-get upgrade

2.安装必须的依赖包(否则接下去的安装过程中你会遇到很多坑)

apt-get install -y build-essential zlib1g-dev libyaml-dev libssl-dev libgdbm-dev libreadline-dev libncurses5-dev libffi-dev curl git-core openssh-server redis-server checkinstall libxml2-dev libxslt-dev libcurl4-openssl-dev libicu-dev vim ruby-dev


ubuntu系统安装好后默认自带的是python 2.7版本,同志们保持一致吧。如果不是2.7版本,尝试镜像源中找下:

apt-get install python2.7

安装邮件发送支持(默认):

apt-get install postfix

安装Ruby 1.9.3版本 :

apt-get install ruby


安装bundler ,也可以使用taobao镜像来缩短下载时间:

gem sources --remove http://rubygems.org

gem sources -a http://ruby.taobao.org/

gem install bundler


3. 添加git用户

adduser  --gecos 'GitLab' git

visudo       #更改sudo权限,赋予git以root身份去执行命令,添加下面这行(这里我为了后续方便将git的sudo权限放的很大)

git ALL=(ALL:ALL) ALL


4. 安装gitlab-shell

#login as git

su - git

cd /home/git/

#clone gitlab shell

git clone https://github.com/gitlabhq/gitlab-shell.git

cd gitlab-shell/

#switch to right  version

git checkout v1.4.0

cp config.yml.example config.yml

vi config.yml                         编辑config.yml配置文件:替换gitlab_url为自己的内部域名如git.abc.com

./bin/install


5.数据库安装

sudo apt-get install -y mysql-server mysql-client libmysqlclient-dev

创建gitlab用户并设置权限:

mysql -u root -p

mysql> CREATE USER 'gitlab'@'localhost' IDENTIFIED BY 'gitlab';

mysql> CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;

mysql> GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlabhq_production`.* TO 'gitlab'@'localhost';


6. 安装gitlab主程序

su - git            #切换至git帐号下执行cd /home/git/
# Clone GitLab repository
git clone https://github.com/gitlabhq/gitlabhq.git gitlab
cd /home/git/gitlab

# Checkout to stable release
git checkout 5-2-stable

# Make sure GitLab can write to the log/ and tmp/ directories
# 修改账户权限(注意保持/home/git目录及子目录属主、组为git用户)
chmod -R u+rwX log/
chmod -R u+rwX tmp/

# Create directory for satellites

mkdir /home/git/gitlab-satellites

# Create directories for sockets/pids and make sure GitLab can write to them

mkdir tmp/pids

mkdir tmp/sockets

# Create public/uploads directory otherwise backup will fail

mkdir public/uploads

chmod -R u+rwX  tmp/pids

chmod -R u+rwX  tmp/sockets

chmod -R u+rwX  public/uploads


# Copy the example Puma config

cp config/puma.rb.example config/puma.rb       # 该配置文件默认即可


# Configure Git global settings for git user, useful when editing via web

# Edit user.email according to what is set in gitlab.yml

git config --global user.name "GitLab"

git config --global user.email "gitlab@localhost"


cp config/database.yml.mysql config/database.yml

vi config/database.yml   # 修改数据库账号密码, 刚才添加过gitlab这个数据库用户 直接修改成该账号即可

cp config/gitlab.yml.example  config/gitlab.yml 

vi config/gitlab.yml    #修改host:localhost为自己的域名 git.olymtech.com


7. 安装 Gems

cd /home/git/gitlab

sudo gem install charlock_holmes --version '0.6.9.4'

#修改Bundle源地址为taobao, 首行改成 source 'http://ruby.taobao.org/'

vi Gemfile

#使用mysql数据库,排除 postgres等

cd /home/git/gitlab

bundle install --deployment --without development test postgres

注:这里在安装过程中发现有个坑,无论是ruby官网还是淘宝都找不到modernizr-2.6.2版本。

错误如下:

git@gitserver:~/gitlab$ bundle install --deployment --without development test postgres 
Fetching source index from http://ruby.taobao.org/ 
Could not find modernizr-2.6.2 in any of the sources

官网跟淘宝 ruby源 都没了这个包 解决办法是先手动去下载 然后安装


git@gitserver:~/gitlab$ wget http://rubygems.org/gems/modernizr-2.6.2.gem

git@gitserver:~/gitlab$ sudo  gem install modernizr

sudo  gem install modernizr

 然后修改 gitlab源码包里面的 Gemfile 跟 Gemfile.lock文件 ,把里面 modernizr包名换成 modernizr-rails 

版本全部换成 2.7.1 然后再运行 bundle install --deployment --without development test postgres 即可


8.初始化数据库并启用高级功能

bundle exec rake gitlab:setup RAILS_ENV=production

整个gitlab安装完成后默认创建的帐号密码:

Administrator account created: 

login.........admin@local.host 

password......5iveL!fe


9.检测一下程序状态

bundle exec rake gitlab:check RAILS_ENV=production

检测结果:

Checking Environment ...

Git configured for git user? ... yes

Has python2? ... yes

python2 is supported version? ... yes

Checking Environment ... Finished

Checking GitLab Shell ...

GitLab Shell version >= 1.4.0 ? ... OK (1.4.0)

Repo base directory exists? ... yes

Repo base directory is a symlink? ... no

Repo base owned by git:git? ... yes

Repo base access is drwxrws---? ... yes

post-receive hook up-to-date? ... yes

post-receive hooks in repos are links: ...

Administrator / olymtechtest ... repository is empty

Checking GitLab Shell ... Finished

Checking Sidekiq ...

Running? ... yes

Checking Sidekiq ... Finished

Checking GitLab ...

Database config exists? ... yes

Database is SQLite ... no

All migrations up? ... yes

GitLab config exists? ... yes

GitLab config outdated? ... no

Log directory writable? ... yes

Tmp directory writable? ... yes

Init script exists? ... yes

Init script up-to-date? ... no

  Try fixing it:

  Redownload the init script

  For more information see:

  doc/install/installation.md in section "Install Init Script"

  Please fix the error above and rerun the checks.

Projects have satellites? ...

Administrator / olymtechtest ... can't create, repository is empty

Redis version >= 2.0.0? ... yes

Your git bin path is "/usr/bin/git"

Git version >= 1.7.10 ? ... yes (1.9.1)

Checking GitLab ... Finished

10.安装init脚本(以root身份,否则可能没权限)

wget https://raw.github.com/gitlabhq/gitlabhq/5-2-stable/lib/support/init.d/gitlab

mv gitlab /etc/init.d/

service gitlab start             启动gitlab服务。


至此:上面的git+gitlab+mysql+redis 环境安装完成


====安装nginx作为web服务器====

nginx安装(以root身份):

apt-get install nginx


增加nginx配置文件:

wget https://raw.github.com/gitlabhq/gitlabhq/5-2-stable/lib/support/nginx/gitlab -P /tmp

mv /tmp/gitlab  /etc/nginx/sites-enabled/

vi /etc/nginx/sites-enabled/gitlab  编辑配置文件,修改配置文件中直接监听80端口, 域名为自定义的访问域名即可:

server {

  listen 80;

  server_name git.abc.com;

.....

}

需注意的是nginx主配置文件/etc/nginx/nginx.conf 中开启include /etc/nginx/sites-enabled/*;

使其子配置文件生效。


启动服务:

service nginx restart


接下来手动hosts指向下,期待已久的站点页面就可以访问了:

wKioL1RYmH3wn2N7AAFd1hOP8z4805.jpg


参考文档:

https://www.uloli.com/p/6sh26/

http://www.showerlee.com/archives/1285

http://www.cnblogs.com/scue/p/3663546.html

http://www.tuicool.com/articles/m22AVv


遇到的坑一:

git@gitserver:~/gitlab$ sudo gem install charlock_holmes --version '0.6.9.4' 
Building native extensions. This could take a while... 
ERROR: Error installing charlock_holmes: 
ERROR: Failed to build gem native extension. 

/usr/bin/ruby1.9.1 extconf.rb 
/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- mkmf (LoadError) 
from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require' 
from extconf.rb:1:in `<main>'

解决方案:

sudo apt-get install ruby-dev


遇到的坑二:


一切安装完成后,发现客户端通过http方式push可以,但每次都需要密码。

采用gitlab 的ssh-key认证方式,本地客户端生成一个ssh key后将公钥上传至gitlab站点页面,发现ssh key并未成功,git push时每次都要输入密码。

从日志中找不出有效信息,百度了很久一直没有找到原因,核对本地客户端的key,上传到gitlab站点的key,和服务端生成的key对比发现,问题根本原因在于上传到gitlab站点上的key和服务器生成的key不一致。

为什么么不一致,谷歌上找了很久,gitlab与gitlab-shell网上查的原因有配置文件中url不一致,用户或权限不对,.ssh目录及authorized_keys权限必须分别为700和600。 这些反复查看配置都是正确的,无解。

尝试重新生成key:

git@gitserver:~/gitlab$ rake gitlab:shell:setup RAILS_ENV=production

重启gitlab:

root@gitserver:/home/git/gitlab# /etc/init.d/gitlab restart


git push测试还是需要输入密码,这一大坑直到第二天在一国外论坛上找到某一人:just forgot "RAILS_ENV=production"。提醒了我下,解决方案:

把RAILS_ENV=production直接加入到/etc/profile全局环境变量中,source /etc/profile 使其生效。

root@gitserver:~# echo "RAILS_ENV=production" >>/etc/profile

root@gitserver:~#source /etc/profile 

把git用户下的原来的authorized_keys文件删除。再将gitlab服务停止再启动。

root@gitserver:~#rm  /home/git/.ssh/authorized_keys

root@gitserver:~# /etc/init.d/gitlab stop

root@gitserver:~# /etc/init.d/gitlab start


这时客户端测试删除原来生成的key,重新生成并上传到gitlab站点,这次再push测试认证通过。

这时查看服务端生成的authorized_keys内容跟上传的公钥key是完全一致的。