一、git介绍
git是一个分布是的版本控制,每个人都有自己的版本库,每次提交的版本号是sha-1算法产生的全球唯一的编号。
SVN是集中式的版本控制,只有一个版本库。版本号从1,2,3顺序编号。
几个概念:
index 暂存区-加入跟踪但没提交的
work area 工作区
local repository 本地仓库
remote repository 远程仓库
一个文件夹夹 ,在执行git init后,就初始化成本地仓库。产生一个.git隐藏文件夹
.git |本地仓库
abc.html |工作区
de.html |工作区
.git隐藏文件夹的内部:
logs |文件夹
objects |文件夹 提交后的数据
refs |文件夹
COMMIT_EDITMSG |
config |
FETCH_HEAD |
HEAD |
index |文件 暂存区
ORIG_HEAD |
packed-refs
sourcetreeconfig
gitlab 版本控制 CE版
下载:https://about.gitlab.com/installation/#centos-7
详细说明:https://gitlab.com/gitlab-org/omnibus-gitlab/tree/master/doc/settings
客户端用: Git-2.10.2-64-bit.exe sourcetree
1.下载gitlab安装包
官网下载速度较慢 建议先行下载
(1).https://about.gitlab.com/downloads/
(2).国内的源 里面可以找到最新的版本https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu/pool/trusty/main/g/gitlab-ce/
(3).yum 源安装 curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
2.安装依赖
yum install curl policycoreutils openssh-server openssh-clients postfix -y
systemctl enable postfix
systemctl start postfix
firewall-cmd --permanent --add-service=http
systemctl reload firewalld
3.添加yum 源,安装包
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
sudo yum install gitlab-ce -y
yum --installroot 指定安装路径
或者
curl -LJO https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-XXX.rpm/download
rpm -i gitlab-ce-XXX.rpm
4.配置启动GitLab
gitlab-ctl reconfigure
配置文件: /etc/gitlab/gitlab.rb
external_url 'http://tqsrv131-git' ##!这里是域名或IP
5.EMail设置:
(1)/etc/gitlab/gitlab.rb
external_url 'http://60.205.222.133'
(2)启用postfix,(本例子中不启用,使用smtp)
postfix 配置文件:
vi /etc/postfix/main.cf
vi /var/log/mail.log
gitlab_rails['gitlab_email_enabled'] = true
gitlab_rails['gitlab_email_from'] = 'huasaixiu@tianqiantek.cn'
gitlab_rails['gitlab_email_display_name'] = 'GitLab'
gitlab_rails['gitlab_email_reply_to'] = 'noreply@tianqiantek.cn'
(3)配置SMTP 代替SendMail/postfix发送邮件
gedit /etc/gitlab/gitlab.rb
external_url 'http://xx.xx.xx.xx'
# see https://gitlab.com/gitlab-org/omnibus-gitlab/tree/master/doc/settings
# Use smtp insttead of sendmail/postfix.
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.126.com"
gitlab_rails['smtp_port'] = 25
gitlab_rails['smtp_user_name'] = "system_mail_ul@126.com"
gitlab_rails['smtp_password'] = "xxxxx"
gitlab_rails['smtp_domain'] = "smtp.126.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = false
gitlab_rails['smtp_openssl_verify_mode'] = 'peer'
重新配置
gitlab-ctl reconfigure
测试
gitlab-rails console
irb(main):003:0> Notify.test_email('huasaixiu@tianqiantek.cn', 'Message Subject--test', 'Message Body').deliver_now
注意:如果是阿里
云服务器,需要申请25端口解封,否则不通
6.gitlab-ctl管理gitlab
1)查看状态
[root@tqsrv131-git ~]# gitlab-ctl status
run: gitaly: (pid 25445) 440241s; run: log: (pid 24853) 440351s
run: gitlab-monitor: (pid 25453) 440240s; run: log: (pid 25130) 440327s
run: gitlab-workhorse: (pid 25457) 440240s; run: log: (pid 24873) 440349s
run: logrotate: (pid 4445) 1038s; run: log: (pid 24953) 440345s
run: nginx: (pid 25474) 440239s; run: log: (pid 24919) 440347s
run: node-exporter: (pid 25482) 440239s; run: log: (pid 25058) 440337s
run: postgres-exporter: (pid 25489) 440238s; run: log: (pid 25111) 440329s
run: postgresql: (pid 25499) 440237s; run: log: (pid 24677) 440390s
run: prometheus: (pid 25507) 440237s; run: log: (pid 25035) 440339s
run:
redis: (pid 25517) 440237s; run: log: (pid 24617) 440397s
run: redis-exporter: (pid 25534) 440236s; run: log: (pid 25088) 440335s
run: sidekiq: (pid 25542) 440234s; run: log: (pid 24834) 440357s
run: unicorn: (pid 25566) 440233s; run: log: (pid 24795) 440359s
提示: 我们要保证80端口不被占用
2)查看一下端口
[root@tqsrv131-git ~]# gitlab-ctl restart
ok: run: gitaly: (pid 6461) 1s
ok: run: gitlab-monitor: (pid 6474) 0s
ok: run: gitlab-workhorse: (pid 6477) 1s
ok: run: logrotate: (pid 6487) 0s
ok: run: nginx: (pid 6494) 0s
ok: run: node-exporter: (pid 6502) 1s
ok: run: postgres-exporter: (pid 6509) 0s
ok: run: postgresql: (pid 6523) 1s
ok: run: prometheus: (pid 6532) 0s
ok: run: redis: (pid 6542) 0s
ok: run: redis-exporter: (pid 6546) 1s
ok: run: sidekiq: (pid 6553) 1s
ok: run: unicorn: (pid 6563) 0s
[root@tqsrv131-git ~]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 6494 root 7u IPv4 9813609 0t0 TCP *:http (LISTEN)
nginx 6495 gitlab-www 7u IPv4 9813609 0t0 TCP *:http (LISTEN)
nginx 6496 gitlab-www 7u IPv4 9813609 0t0 TCP *:http (LISTEN)
nginx 6497 gitlab-www 7u IPv4 9813609 0t0 TCP *:http (LISTEN)
nginx 6498 gitlab-www 7u IPv4 9813609 0t0 TCP *:http (LISTEN)
AliYunDun 10327 root 18u IPv4 20373 0t0 TCP tqsrv131-git:60838->140.205.140.205:http (CLOSE_WAIT)
AliYunDun 10327 root 20u IPv4 585797 0t0 TCP tqsrv131-git:52398->106.11.68.13:http (CLOSE_WAIT)
AliYunDun 10371 root 18u IPv4 20373 0t0 TCP tqsrv131-git:60838->140.205.140.205:http (CLOSE_WAIT)
AliYunDun 10371 root 20u IPv4 585797 0t0 TCP tqsrv131-git:52398->106.11.68.13:http (CLOSE_WAIT)
AliYunDun 10371 root 22u IPv4 864184 0t0 TCP tqsrv131-git:44820->106.11.68.13:http (ESTABLISHED)
3)Web:访问
http://xxx.xxx.xxx.xxx/
按照上面设置的external_url访问,第一次登陆默认管理员密码和用户名:
Username: root
Password: 根据邮件给出的连接去设置初始密码。
gitlab关闭开放注册
Admin-->settings --> Sign-in Restrictions
Sign-upenbaled 关闭注册功能
Sign-inenbaled 关闭注册登录功能
设置内部使用,不公开
4)使用
先创建组,再用组创建项目,最后创建用户,把用放组里
设置 项目、snippet、group 权限等级分三种:
Private 私有的,只有你自己或者组内的成员能访问
Internal 所有登录的用户
Public 公开的,所有人都可以访问
对项目进行设置
5)Git的使用
Git Bash 下载地址:https://git-scm.com/downloads
(1)添加sshkey
生成sshkey
ssh-keygen -t rsa -C "$your_email"
cat ~/.ssh/id_rsa.pub
$your_emai是你的邮箱地址,一直回车即可,此命令在C:\Users\<你的用户名>.ssh目录下生成一对公私密钥,拷贝公钥(.pub结尾的文件)的内容
(2).保存sshkey到gitlab
登陆你的GitLab账号之后,点击右上角的”Profile Setting” -> “SSH Keys”,输入SSH Key标题(可自定义),将拷贝的id_rsa.pub内容拷贝到Key中,”Add Key”即可。
(3).开始上传代码
进入工程目录 cd $project_root
初始化git仓库 git init
添加文件到仓库 git add .
提交代码到仓库git commit -m ‘init commit’
链接到git server
git remote add origin git@example.com:namespace/projectname.git
push代码到
服务器 git push origin master
GitLab一些常用指令
gitlab-ctl start/stop/restart/
Gitlab 默认的日志文件存放在/var/log/gitlab 目录下
gitlab-ctl tail
gitlab-ctl tail nginx/gitlab_acces.log
gitlab-ctl tail postgresql
1.GitLab 仓库
搜索 /etc/gitlab/gitlab.rb 中的 git_data_dir
缺省仓库路径: git_data_dir “/var/opt/gitlab/git-data”
备份路径: /var/opt/gitlab/backups
备份保存时间: gitlab_rails['backup_keep_time'] = 604800
2.GitLab 备份
参考文档:https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/doc/settings/backups.md
http://shaonian.blog.51cto.com/2975261/1891124/
2.1方式1 直接备份配置文件和数据文件
-
备份目录:
-
/etc/gitlab
-
/var/opt/gitlab/git-data/repositories
-
-
备份方法
-
cd /u01/gitlab/backups/
tar -zPcf $(date "+etc-gitlab_%Y%_m_%d_%H%M%S.tar.gz") /etc/gitlab
tar -zPcf $(date "+gitlab_data_%Y_%m_%d_%H%M%S.tar.gz") /var/opt/gitlab/git-data/repositories
-
定时:
-
sudo sh -c 'umask 0077; tar -cf $(date "+etc-gitlab-%s.tar") -C / etc/gitlab'
-
2.2方式2 Omnibus 方式安装使用以下命令备份
-
指令 : /opt/gitlab/bin/gitlab-rake gitlab:backup:create
-
-
-
[root@tqsrv131-git repositories]# gitlab-rake gitlab:backup:create
-
Dumping database ...
-
Dumping PostgreSQL database gitlabhq_production ... [DONE]
-
...
-
done
-
Deleting old backups ... skipping
-
-
[root@tqsrv131-git backups]# pwd
-
/var/opt/gitlab/backups
-
[root@tqsrv131-git backups]# ll
-
total 64
-
-rw------- 1 git git 61440 Aug 15 13:39 1502775554_2017_08_15_9.4.3_gitlab_backup.tar
-
-
定时配置备份:
15 04 * * 2-6 umask 0077; tar cfz /secret/gitlab/backups/$(date "+etc-gitlab-\%s.tgz") -C / etc/gitlab
定时备份数据仓库:
0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1
3.恢复
-
cp 1493107454_2017_04_25_9.1.0_gitlab_backup.tar /var/opt/gitlab/backups/
-
gitlab-ctl stop unicorn
-
gitlab-ctl stop sidekiq
-
gitlab-ctl status
-
gitlab-rake gitlab:backup:restore BACKUP=1493107454_2017_04_25_9.1.0
-
gitlab-ctl start
-
gitlab-rake gitlab:check SANITIZE=true
Git 分支 - 分支的新建与合并
master
|->C2->C1->C0
要解决你的公司使用的问题追踪系统中的 #53 问题
$ git checkout -b iss53
Switched to a new branch "iss53"
也就是下面两天命令的缩写
$ git branch iss53
$ git checkout iss53
$ git commit -a -m 'added a new footer [issue 53]'
$ git checkout master
Switched to branch 'master'
$ git checkout -b hotfix
Switched to a new branch 'hotfix'
$ vim index.html
$ git commit -a -m 'fixed the broken email address'
[hotfix 1fb7853] fixed the broken email address
1 file changed, 2 insertions(+)
ssh-keygen -t rsa -C "huasaixiu@tianqiantek.cn" -b 4096