环境:
序号 | 角色 | IP | 主机名 | 主要软件 |
---|---|---|---|---|
@1 | git-server | 192.168.11.11 | local-1 | git |
@2 | git-client | 192.168.11.12 | local-2 | git |
@3 | gitlab | 192.168.11.13 | gitlab.example.com | gitlab |
SVN 与 GIT 对比:对比连接点这里<<<<
Git是分布式的,SVN是集中式的
Git复杂概念多,SVN简单易上手
Git分支廉价,SVN分支昂贵
Git 分支是指针指向某次提交,而 SVN 分支是拷贝的目录。这个特性使 Git 的分支切换非常迅速,且创建成本非常低。
而且 Git 有本地分支,SVN 无本地分支。
@1 11.11
##安装git
[root@local-1 ~]# yum -y install git
###创建版本库,一般规范的方式要以.git 为后缀
[root@local-1 ~]# mkdir my.git
[root@local-1 ~]# cd my.git
###初始化 Git 版本仓库。
[root@local-1 my.git]# git --bare init
初始化空的 Git 版本库于 /root/my.git/
###--bare 创建裸库
###当前目录会出现一些文件
###如果不加此选项,则会在当前目录创建一个 .git 目录,目录里同样是这些文件。
###需要在服务器上开放至少一种支持 Git 的协议,比如 HTTP/HTTPS/SSH 等。
###现在用的最多的就是 HTTPS 和 SSH,本案例中使用的是 SSH 协议。
@2 11.12
[root@local-2 ~]# yum -y install git
###配置免密(为了后面提交版本时候不用输密码)
[root@local-2 ~]# ssh-keygen
[root@local-2 ~]# ssh-copy-id 192.168.11.11
###从11.11上下载版本仓库目录
[root@local-2 ~]# git clone root@192.168.11.11:/root/my.git
正克隆到 'my'...
warning: 您似乎克隆了一个空版本库。
###配置用户名称和电子邮件地址。每次 Git 提交时都会引用这两条信息,记录提交了文件的用户,并且会随更新内容一起被永久纳入历史记录。
[root@local-2 ~]# git config --global user.name "baymax"
[root@local-2 ~]# git config --global user.email "baymax@baymax.com"
###设置 vim 为默认的文本编辑器
[root@local-2 ~]# git config --global core.editor vim
###设置 git 颜色支持
[root@local-2 ~]# git config --global color.ui true
###查看 git 配置
[root@local-2 ~]# git config --list
user.name=baymax
user.email=baymax@baymax.com
core.editor=vim
color.ui=true
###模拟代码提交
[root@local-2 ~]# cd my
[root@local-2 my]# echo "init git" > readme.txt
###将该文件添加到暂存区
[root@local-2 my]# git add readme.txt
或用 git add .
###添加到暂存区后再次修改文件的内容
[root@local-2 my]# echo "v1" >> readme.txt
###将暂存区的文件提交到本地 Git 版本仓库
[root@local-2 my]# git commit -m "add readme"
###查看当前工作目录的状态
[root@local-2 my]# git status
1)第一次修改提交代码:git 将代码文件提交到本地 Git 版本数据库,此时会在暂存区
生产一个快照版本
2)第二次修改提交代码:当再次修改代码时,需要重新提交到暂存区,此时还会生产
一个快照版本
3)提交代码:只有将暂存区的代码提交到 git 版本数据库才能算直正提交。
###查看当前文件内容与 Git 版本数据库中的差别
[root@local-2 ~]# git diff readme.txt
###再次提交
[root@local-2 ~]# git commit -a -m "all"
[root@local-2 ~]# git status
这次的操作还是只将文件提交到了本地的 Git 版本仓库,并没有推送到远程 Git 服务器,所以需要定义远程的 Git 服务器。
###查看远程库
[root@local-2 ~]# git remote
[root@local-2 ~]# git remote -v //详细显示
origin root@192.168.11.11:/root/my.git (fetch)
origin root@192.168.11.11:/root/my.git (push)
此时可以直接向远程库 push
###添加远程库
git remote add origin git@github.com:gitname/learngit.git #//远程库名(可自由命名)@github账户/github库名称
例如:
[root@local-2 ~]# git remote add baymax root@192.168.11.11:/root/my.git
[root@local-2 ~]# git config --list
[root@local-2 ~]# git remote -v
baymax root@192.168.11.11:/root/my.git (fetch) 远程下载的url
baymax root@192.168.11.11:/root/my.git (push) 上传到 远程的url
origin root@192.168.11.11:/root/my.git (fetch)
origin root@192.168.11.11:/root/my.git (push)
###将文件提交到远程 Git 服务器
[root@local-2 ~]# git push baymax master
Counting objects: 6, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (6/6), 420 bytes | 0 bytes/s, done.
Total 6 (delta 0), reused 0 (delta 0)
To root@192.168.11.11:/root/my.git
* [new branch] master -> master
###为了验证是否推送到了远程的 Git 服务,可以换个目录再克隆一份版本仓库。
[root@local-2 my]# mkdir test
[root@local-2 my]# cd test/
[root@local-2 test]# git clone root@192.168.11.11:/root/my.git
###验证查看克隆文件
[root@local-2 test]# cat my/readme.txt
@3 11.13
搭建部署 GitLab 服务器
GitLab 是一个用于仓库管理系统的开源项目,使用 Git 作为代码管理工具,并在此基础上搭建起来的 Web 服务。建议服务器内存 3G 以上。
###配置yum源
[root@gitlab ~]# vim /etc/yum.repos.d/gitlab-ce.repo
[gitlab-ce]
name=gitlab-ce
baseurl=http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7
repo_gpgcheck=0
gpgcheck=0
enabled=1
gpgkey=https://packages.gitlab.com/gpg.key
[root@gitlab ~]# yum -y install gitlab-ce
[root@gitlab ~]# mkdir -p /etc/gitlab/ssl
[root@gitlab ~]# cd /etc/gitlab/ssl
###创建本地私有秘钥
[root@gitlab ssl]# openssl genrsa -out "gitlab.example.com.key" 2048
###根据私有秘钥创建csr证书
[root@gitlab ssl]# openssl req -new -key "gitlab.example.com.key" -out "gitlab.example.com.csr"
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn 国家
State or Province Name (full name) []:henan 州/省
Locality Name (eg, city) [Default City]:zz 城市
Organization Name (eg, company) [Default Company Ltd]:baymax 组织
Organizational Unit Name (eg, section) []:baymax 单位
Common Name (eg, your name or your server's hostname) []:gitlab.example.com 域名/主机名
Email Address []:gitlab@example.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456 修改口令
An optional company name []: 可选公司名
[root@gitlab ssl]# ll /etc/gitlab/ssl/
总用量 8
-rw-r--r-- 1 root root 1082 12月 29 22:25 gitlab.example.com.csr
-rw-r--r-- 1 root root 1675 12月 29 22:23 gitlab.example.com.key
###使用私有秘钥和csr证书创建签署证书
[root@gitlab ssl]# openssl x509 -req -days 365 -in "gitlab.example.com.csr" -signkey "gitlab.example.com.key" -out "gitlab.example.com.crt"
###制作pem证书,可不做
[root@gitlab ssl]# openssl dhparam -out dhparams.pem 2048
##需要一点时间
[root@gitlab ssl]# ll /etc/gitlab/ssl/
总用量 16
-rw-r--r-- 1 root root 424 12月 29 22:46 dhparams.pem
-rw-r--r-- 1 root root 1298 12月 29 22:39 gitlab.example.com.crt
-rw-r--r-- 1 root root 1082 12月 29 22:38 gitlab.example.com.csr
-rw-r--r-- 1 root root 1679 12月 29 22:37 gitlab.example.com.key
[root@gitlab ssl]# chmod 600 /etc/gitlab/ssl/*
###将证书加入到gitlab配置文件中
[root@gitlab ssl]# vim /etc/gitlab/gitlab.rb
29 external_url 'https://gitlab.example.com' //http改成https
1102 nginx['redirect_http_to_https'] = true //false改成true
###加载配置文件并启动,前提是 80 端口没有被占用
[root@gitlab ssl]# gitlab-ctl reconfigure
[root@gitlab ssl]# gitlab-ctl restart
gitlab-ctl 其他命令:
启动:gitlib-ctl start
关闭:gitlab-ctl stop
重启:gitlab-ctl restart
重载配置:gitlab-ctl reconfigure
查看状态:gitlab-ctl status
[root@gitlab ssl]# ss -tnl
GitLab 配置文件路径为/etc/gitlab/gitlab.rb,生产环境下可以跟据需求修改。
访问 https://gitlab.example.com
需要物理机有静态解析
设置密码:12345678
登陆
用户:root
密码:12345678
创建一个项目:
在客户机验证。
#需要静态解析
[root@gitlab ssl]# echo "192.168.11.13 gitlab.example.com" >> /etc/hosts
[root@gitlab ssl]# git -c http.sslverify=false clone https://gitlab.example.com/root/example.git
#设置忽略证书错误 SSL证书未经过第三方机构认证
设置全局配置
[root@gitlab ssl]# git config --global http.sslverify false
[root@gitlab ssl]# cat example/README.TXT
baymax