git的工作环境
1 2 3 4 5 6 工作区 暂存区 git add * 版本库 git commit -m “版本描述信息” HEAD 版本号 版本日志
1 2 3 4 git clone git@IP地址:/自建的目录/自建的库/ git add . git commit -m "描述信息" git push origin master
每个版本都会有一个id号,也就是commit id
1 2 3 [root@vm20 ~] commit fbecfa3d04ae5038aa11bf55942e46c840077ace
部署git
1 2 3 4 5 6 7 8 9 10 11 12 环境: git-server 192.168.246.214 充当服务器 client 192.168.246.213 安装:所有机器都安装 [root@git-server ~] [root@git-server ~] git version 1.8.3.1 `所有的机器都添加,只要邮箱和用户不一样就可以`
1、git使用
1 2 3 4 5 6 7 8 9 1.创建一个空目录:在中心服务器上创建[root@git-server ~] [root@git-server ~] [root@git-server ~] [root@git-server ~] 2.通过git init命令把这个目录变成Git可以管理的仓库: 第1种情况:可以改代码,还能上传到别人的机器,别人也能从你这里下载但是别人不能上传代码到你的机器上。 第2种情况:只是为了上传代码用,别人从这台机器上下载代码也可以上传代码到这台机器上,经常用于核心代码库。
创建—-裸库:
1 2 3 4 5 6 7 8 9 10 `语法:git init --bare 库名字[root@git-server git-test] Initialized empty Git repository in /git-test/testgit/[root@git-server ~] 2.仓库创建完成后查看库目录:[root@git-server git-test] [root@git-server testgit] branches config description HEAD hooks info objects refs
客户端
1 2 3 4 5 6 7 8 9 1.配置免密登录[root@client ~] [root@client ~] 2.克隆git仓库[root@client ~] Cloning into 'testgit'... warning: You appear to have cloned an empty repository.[root@client ~] anaconda-ks.cfg testgit
创建文件模拟代码提交到仓库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 1.在testgit目录下创建一个测试文件test.txt[root@client ~] [root@client testgit] 2.把文件添加到暂存区:使用 "git add" 建立跟踪[root@client testgit] 注: 这里可以使用 git add * 或者 git add -A 3.提交文件到本地仓库分支:[root@client testgit] [master (root-commit) 2b51ff9] test1 1 file changed, 2 insertions(+) create mode 100644 test.txt -m:描述 4.查看git状态: [root@client testgit] 5.修改文件后再此查看状态:[root@client testgit] [root@client testgit] 修改尚未加入提交(使用 "git add" 和/或 "git commit " 6.先add[root@client testgit] 8.再次提交commit:[root@client testgit] [master 73bf688] add2 1 file changed, 1 insertion(+) [root@client testgit] nothing to commit, working directory clean
版本回退
1 2 3 4 5 6 7 8 9 10 11 12 13 14 查看现在的版本[root@client testgit] 回到上一个版本[root@client testgit] HEAD is now at 0126755 test1 2.回到指定的版本(根据版本号): [root@client testgit] HEAD is now at dd66ff9 add2 ========================================================== 注:消失的ID号:可以查看之前的所有的版本[root@vm20 gittest]
删除文件
从工作区删除test.txt,并且从版本库一起删除
1 2 3 4 5 6 7 8 9 从工作区删除[root@client testgit] 从版本库删除:[root@client testgit] rm 'test.txt'[root@client testgit] [master cebc081] 删除文件test.txt 1 file changed, 3 deletions(-) delete mode 100644 test.txt
将代码上传到仓库的master分支
1 2 3 4 5 6 7 8 9 10 [root@client testgit] [root@client testgit] [root@client testgit] [root@client testgit] Counting objects: 11, done. Compressing objects: 100% (4/4), done. Writing objects: 100% (11/11), 828 bytes | 0 bytes/s, done. Total 11 (delta 0), reused 0 (delta 0) To [email protected] :/git-test/testgit/ * [new branch] master -> master
测试:
在客户端将仓库删除掉然后在克隆下来查看仓库中是否有文件
1 2 3 4 5 6 7 8 9 10 11 12 [root@git-client ~] [root@client ~] Cloning into 'testgit'... remote: Counting objects: 11, done. remote: Compressing objects: 100% (4/4), done. remote: Total 11 (delta 0), reused 0 (delta 0) Receiving objects: 100% (11/11), done.[root@client ~] [root@client testgit] a.txt[root@client testgit] hello world
创建分支并合并分支
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 [root@client testgit] [root@client ~] [root@client testgit] nothing to commit (create/copy files and use "git add" to track) 创建分支:[root@client testgit] [root@client testgit] dev * master 切换分支:[root@client testgit] Switched to branch 'dev'[root@client testgit] * dev master 在dev分支创建一个文件;[root@client testgit] [root@client testgit] [root@client testgit] [dev f855bdf] add dev 1 file changed, 1 insertion(+) create mode 100644 test.txt 现在,dev分支的工作完成,我们就可以切换回master分支: [root@client testgit] Switched to branch 'master' 切换回`master`分支后,再查看一个`test.txt`文件,刚才添加的内容不见了!因为那个提交是在`dev`分支上,而`master`分支此刻的提交点并没有变:[root@client testgit] a.txt 现在,我们把`dev`分支的工作成果合并到`master`分支上:[root@client testgit] Updating 40833e0..f855bdf Fast-forward test.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 test.txt[root@client testgit] a.txt test.txt 现在已经将dev分支的内容合并到master上。确认没有问题上传到远程仓库:[root@client testgit] 合并完成后,就可以放心地删除`dev`分支了:[root@client testgit] Deleted branch dev (was f855bdf). 删除后,查看`branch`,就只剩下`master`分支了:[root@client testgit] * master
2、部署gitlab服务
官网下载gitlab地址
1 2 3 百度搜索gitlab--点击https://gitlab.com/users/sign_in 拉到最下面--点击 关于 GitLab 再拉到最下面--有个Resources--点击Install--往下拉点击有个cenos 7版本的下载--安装命令步骤操作即可
配置yum源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 [root@git-server ~] [root@git-server yum.repos.d] [gitlab-ce] name =Gitlab CE Repositorybaseurl =https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever gpgcheck =0 enabled =1 [root@git-server yum.repos.d] [root@git-server yum.repos.d] [root@git-server yum.repos.d] [root@git-server yum.repos.d] [root@git-server yum.repos.d] [root@git-server yum.repos.d] [root@git-server yum.repos.d] [root@git-server yum.repos.d]
配置gitlab
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 [root@git-server ~] 1. external_url 'http://192.168.246.214' 2.设置地区 gitlab_rails['time_zone'] = 'Asia/Shanghai' 将数据路径的注释去掉,可以更改 git_data_dirs({ "default" => { "path" => "/mnt/nfs-01/git-data" } }) 开启ssh服务: gitlab_rails['gitlab_shell_ssh_port'] = 22 开启邮箱服务 gitlab_rails['smtp_enable'] = true gitlab_rails['smtp_address'] = "smtp.163.com" gitlab_rails['smtp_port'] = 465 gitlab_rails['smtp_user_name'] = "[email protected] " gitlab_rails['smtp_password'] = "邮箱授权密码" gitlab_rails['smtp_domain'] = "163.com" gitlab_rails['smtp_authentication'] = "login" gitlab_rails['smtp_enable_starttls_auto'] = true gitlab_rails['smtp_tls'] = true gitlab_rails['smtp_openssl_verify_mode'] = 'none' gitlab_rails['gitlab_email_from'] = '[email protected] ' gitlab_rails['gitlab_email_display_name'] = 'Admin'
重置并启动GitLab执行:
启动
邮箱测试
1 2 3 4 [root@git-server ~] irb(main):001:0> Notify.test_email('[email protected] ', 'Message Subject', 'Message Body').deliver_now
测试web页面访问
1 输入IP地址,直接访问,会出来一个界面,它默认用户是root,直接输入密码就可以(设置密码)
在服务器通过在git客户端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 [root@client ~] Cloning into 'testapp'... remote: Enumerating objects: 6, done. remote: Counting objects: 100% (6/6), done. remote: Compressing objects: 100% (4/4), done. remote: Total 6 (delta 0), reused 0 (delta 0) Receiving objects: 100% (6/6), done.[root@client ~] testapp[root@client ~] [root@client testapp] test.txt 同步时间.txt[root@client testapp] 注意:如果克隆不下来可以重新使用命令生成私钥[root@client ~] 使用http的[root@client ~] [root@client ~] Cloning into 'testapp'... Username for 'http://192.168.246.214': root Password for 'http://[email protected] ':12345678 remote: Enumerating objects: 6, done. remote: Counting objects: 100% (6/6), done. remote: Compressing objects: 100% (4/4), done. remote: Total 6 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (6/6), done.[root@client ~] testapp 提交到远程gitlab仓库[root@client ~] [root@client testapp] 1000phone[root@client testapp] [root@client testapp] [root@client testapp] Counting objects: 4, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 266 bytes | 0 bytes/s, done. Total 3 (delta 1), reused 0 (delta 0) To [email protected] :root/testapp.git 4f35d4b..a0067ea master -> master
Gitlab 备份与恢复
1、查看系统版本和软件版本
1 2 3 4 5 [root@git-server ~] CentOS Linux release 7.4.1708 (Core)[root@git-server ~] 13.1.1
2、数据备份
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 打开/etc/gitlab/gitlab.rb配置文件,查看一个和备份相关的配置项: vim /etc/gitlab/gitlab.rb gitlab_rails['backup_path'] = "/var/opt/gitlab/backups" gitlab_rails['backup_archive_permissions'] = 0644 gitlab_rails['backup_keep_time'] = 604800 [root@git-server ~] 或者[root@git-server ~] [root@git-server ~] 也可以添加到 crontab -e 中定时执行: 0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create 备份完成,会在备份目录中生成一个当天日期的tar包。[root@git-server ~] 1588774133_2020_05_06_12.10.3_gitlab_backup.tar
数据恢复
特别注意:
备份目录和gitlab.rb中定义的备份目录必须一致GitLab的版本和备份文件中的版本必须一致,否则还原时会报错
在恢复之前,可以删除一个文件,以便查看效果
执行恢复操作:
1 2 3 4 5 6 7 8 [root@git-server ~] [root@git-server backups] 注意恢复文件的名称 恢复完成后,或者重启服务,再打开浏览器进行访问,发现数据和之前的一致:[root@git-server backups]