代码管理平台介绍、安装svn、客户端使用svn、远程仓库、分支管理、标签管理、git别名、搭建git服务、安装gitlab、简单使用gitlab、备份和恢复

22.1 代码管理平台介绍

现在的开发工作都是由团队合作来完成开发,通常都是团队中的每个人或者每几个人完成一个模块的开发,最后再将模块拼凑起来,形成一个完整的项目,这就涉及到了协同开发。在各个模块的开发过程中,肯定会因为出现BUG或者需求更改,而进行代码的修改甚至重构的,代码每修改一次就相当于迭代了一次版本,一个完整的项目中通常会有多个模块,如果每个模块的开发过程中都会修改或重构代码,那么如果没有一个平台来管理、控制这些代码,肯定会造成代码混乱的局面。所以这时候就有了一个概念:版本控制,代码管理平台的主要功能就是进行版本的控制,以及记录代码修改、版本迭代的历史信息。

  • 版本控制,记录若干文件内容变化,以便将来查阅特定版本修订情况
  • 版本管理工具发展简史,cvs svn  git 参考http://luckypoem14.github.io/test/2012/04/24/scm-history/
  • svn全称subversion,是一个开源版本控制系统,始于2000年
  • git是linux创始人linus发起的,2005年发布,最初目的是更好管理linux内核代码
  • git和svn不同在于git不需要依赖服务端就可以工作,即git是分布式的
  • 关于git和svn的比较大家参考http://blog.lishiming.net/?p=305
  • github是基于git的在线web页面代码托管平台,可以选择付费服务
  • gitlab可以认为是一个开源的github,两者没有直接关系

22.2 安装svn

1、 yum install -y subversion
2、创建版本库 
 mkdir -p /data/svnroot/myproject #创建一个目录来存放项目的版本库
 svnadmin create /data/svnroot/myproject  #初始化,创建项目的版本库
[root@aming3 ~]# mkdir -p /data/svnroot/myproject
[root@aming3 ~]# svnadmin create /data/svnroot/myproject
[root@aming3 ~]# ls -la !$
ls -la /data/svnroot/myproject
总用量 8
drwxr-xr-x. 6 root root  86 8月  29 21:07 .
drwxr-xr-x. 3 root root  23 8月  29 21:04 ..
drwxr-xr-x. 2 root root  54 8月  29 21:07 conf
drwxr-sr-x. 6 root root 233 8月  29 21:07 db
-r--r--r--. 1 root root   2 8月  29 21:07 format
drwxr-xr-x. 2 root root 231 8月  29 21:07 hooks
drwxr-xr-x. 2 root root  41 8月  29 21:07 locks
-rw-r--r--. 1 root root 229 8月  29 21:07 README.txt
3、进入目录查看相关文件
 cd !$/conf     #配置文件所在的目录 #authz为权限配置文件,passwd为密码文件,svnserve.conf为仓库配置文件
[root@aming3 ~]# cd !$/conf 
cd /data/svnroot/myproject/conf 
[root@aming3 conf]# ls
authz  passwd  svnserve.conf
4、配置权限配置文件
 vim authz//配置文件改为如下
[groups]
admins = aming,user1
[/]
@admins = rw
*= r
[myproject:/]
user1 = rw
[root@aming3 conf]# vim authz
[groups] # 用户组
admins = user,user1 # 定义用户,可以定义多个 

[/] # 根目录
@admins = rw # 设置该用户组的权限是可读可写
*= r  # 设置所有的用户都可以读

# 可以指定对某个项目进行设置权限
#[myproject:/]
#user1 = rw  # 设置user1拥有可读可写权限
5、配置密码置文件
  vim passwd//加入如下内容
[users]
aming = aming_!(*$123
user1 = user1_^^^123
user2 = user2-***123
6、配置仓库配置文件
vim svnserver.conf//更改或增加如下内容
[general]
anon-access = none
auth-access = write
password-db = passwd
authz-db = authz
realm = /data/svnroot/myproject
[root@aming3 conf]# vim svnserve.conf
[general]
anon-access = none  # 定义匿名用户的权限
auth-access = write  # 定义用户的权限
password-db = passwd  # 定义用户的密码文件
authz-db = authz  # 定义用户的权限文件
realm = /data/svnroot/myproject  # 定义对哪个项目生效
7、启动svn服务
svnserve -d -r /data/svnroot  # -d指定后台启动,-r指定库所在的目录
[root@aming3 conf]# svnserve -d -r /data/svnroot
[root@aming3 conf]# ps aux |grep svn
root      3334  0.0  0.0 162200   652 ?        Ss   22:28   0:00 svnserve -d -r /data/svnroot
root      3336  0.0  0.0 112676   984 pts/0    S+   22:28   0:00 grep --color=auto svn
[root@aming3 conf]# netstat -lntp |grep svn
tcp        0      0 0.0.0.0:3690            0.0.0.0:*               LISTEN      3334/svnserve

22.3 客户端上使用svn(linux)

  • 客户端安装subversion:
yum install -y subversion
  • 创建目录并连接服务端的myproject版本库(服务端需关闭防火墙)
[root@aming4 ~]# cd /home/
[root@aming4 home]# mkdir svntest
[root@aming4 home]# cd svntest/
[root@aming4 svntest]# svn checkout svn://192.168.222.112/myproject --username=user1
认证领域: <svn://192.168.222.112:3690> /data/svnroot/myproject
“user1”的密码: 

-----------------------------------------------------------------------
注意!  你的密码,对于认证域:

   <svn://192.168.222.112:3690> /data/svnroot/myproject

只能明文保存在磁盘上!  如果可能的话,请考虑配置你的系统,让 Subversion
可以保存加密后的密码。请参阅文档以获得详细信息。

你可以通过在“/root/.subversion/servers”中设置选项“store-plaintext-passwords”为“yes”或“no”,
来避免再次出现此警告。
-----------------------------------------------------------------------
保存未加密的密码(yes/no)?y
取出版本 0。
  • 进入版本库,上传文件到服务端;
[root@aming4 svntest]# cd myproject
[root@aming4 myproject]# ll -a
总用量 0
drwxr-xr-x. 3 root root 18 8月  30 09:59 .
drwxr-xr-x. 3 root root 23 8月  30 09:59 ..
drwxr-xr-x. 4 root root 75 8月  30 09:59 .svn
[root@aming4 myproject]# cp /etc/fstab .
[root@aming4 myproject]# ls
fstab
[root@aming4 myproject]# svn add  ./fstab    # 添加到版本控制中心
A         fstab
[root@aming4 myproject]# svn commit -m "add fstab"   # 把文件上传到服务器
正在增加       fstab
传输文件数据.
提交后的版本为 1。
[root@aming4 myproject]#
  • 删除本地文件,删除服务端的文件,查看变更日志
[root@aming4 myproject]# svn delete fstab 
D         fstab
[root@aming4 myproject]# svn commit -m "delete fstap"
正在删除       fstab

提交后的版本为 2。
[root@aming4 myproject]# svn log

22.4 客户端上使用svn(windows)

22.5/22.6 单机上使用git

  • 安装:
yum install -y git
  • 创建git仓库
[root@aming3 ~]# mkdir /data/gitroot
[root@aming3 ~]# cd /data/gitroot
[root@aming3 gitroot]# git init
初始化空的 Git 版本库于 /data/gitroot/.git/
[root@aming3 gitroot]# ll -a
总用量 0
drwxr-xr-x. 3 root root  18 8月  30 10:43 .
drwxr-xr-x. 4 root root  36 8月  30 10:43 ..
drwxr-xr-x. 7 root root 119 8月  30 10:43 .git
  • 创建测试文件,添加到git仓库,上传
[root@aming3 gitroot]# vim test.java
[root@aming3 gitroot]# git add test.java
[root@aming3 gitroot]# git commit -m "add new file test.java"

*** Please tell me who you are.

Run

  git config --global user.email "[email protected]"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'root@aming3.(none)')
出现提示,把引号里的改成自己的邮箱和账号
 git config --global user.email "[email protected]"
 git config --global user.name "Your Name"
再次提交;
[root@aming3 gitroot]# git commit -m "add new file test.java"
[master(根提交) cc95c57] add new file test.java
 1 file changed, 2 insertions(+)
 create mode 100644 test.java
  • 测试修改文件上传
[root@aming3 gitroot]# echo "95279527{}" >> test.java
[root@aming3 gitroot]# git add test.java
[root@aming3 gitroot]# git commit -m "add test.java agin"
[master 8601540] add test.java agin
 1 file changed, 1 insertion(+)
  • git status命令可以查看当前仓库中的状态
[root@aming3 gitroot]# git status
# 位于分支 master
无文件要提交,干净的工作区
  • git diff命令可以对比某个文件本次修改了什么内容,相比较仓库里面的版本:
[root@aming3 gitroot]# echo "423430{}" >> test.java
[root@aming3 gitroot]# git diff test.java
diff --git a/test.java b/test.java
index 21a455c..a03c92a 100644
--- a/test.java
+++ b/test.java
@@ -1,3 +1,4 @@
 123457890
 abcdefg
 95279527{}
+423430{}
  • git log 查看修改记录
[root@aming3 gitroot]# git log
commit 86015403fc33b58a1e2d41f39e8a5fa3246bfa01    //# 这个是版本的id,进行回退操作时需要使用
Author: zero <[email protected]>
Date:   Thu Aug 30 11:04:05 2018 +0800

    add test.java agin

commit cc95c57a132ec42a0f5be2c0b464b9672314731f
Author: zero <[email protected]>
Date:   Thu Aug 30 10:58:55 2018 +0800

    add new file test.java
  • 一行显示:git log --pretty=oneline
  • 回退版本:git reset --hard +版本id
  • 查看回退版本:git log --pretty=oneline
  • 查看所有历史版本:git reflog
  • 恢复删除的文件:git checkout -- +文件名(前提是文件已经存储在仓库中)
  • 删除仓库中的文件:
git rm +文件
git commit -m "delete +文件"
  • 删除了仓库中的文件,可以通过版本id来恢复
查看ID:git log --pretty=oneline
恢复:git reset --hard +ID

22.7 建立远程仓库

建立远程的GitHub仓库

1.首先到 https://github.com 注册一个账号

2.登录之后,点击右上角,头像旁边的 + 图标,创建一个自己的repository(仓库)

3.填写仓库的相关信息

4、仓库创建成功,记录仓库URL地址;

5.在本地机器上创建密钥对

[root@aming3 gitroot]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:2/7sj8izBzw7HAT0bgNdrSpcDvh+M24yEKXxjQ+E3No root@aming3
The key's randomart image is:
+---[RSA 2048]----+
|     . +.   ..   |
|      + =o .  .  |
|       X.+o  .   |
|      = Eo+ .    |
|       +SO+.     |
|      . ++O.     |
|       o.o.=     |
|        +oXo..   |
|         **X=..  |
+----[SHA256]-----+
[root@aming3 gitroot]# cd 
[root@aming3 ~]# cat .ssh/id_rsa.pub //复制公钥到远程GitHub仓库

6.远程的仓库添加密钥认证,保证访问的安全性:

连接远程仓库

1.创建一个目录,用于存放和上传仓库文件,也相当于是一个本地仓库

[root@aming3 ~]# mkdir /tmp/example
[root@aming3 ~]# cd !$
cd /tmp/example

2、初始化仓库

[root@aming3 example]#  echo "# example" >> README.md  //生成文件
[root@aming3 example]# git init 
初始化空的 Git 版本库于 /tmp/example/.git/
[root@aming3 example]# ll -a
总用量 8
drwxr-xr-x.  3 root root   35 8月  30 12:09 .
drwxrwxrwt. 11 root root 4096 8月  30 12:08 ..
drwxr-xr-x.  7 root root  119 8月  30 12:09 .git
-rw-r--r--.  1 root root   10 8月  30 12:09 README.md
[root@aming3 example]# git add README.md
[root@aming3 example]# git commit -m "first commit"
[master(根提交) cdbba4c] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
## 将本地文件推送到远程仓库上
[root@aming3 example]# git remote add origin https://github.com/xou6363/example.git
[root@aming3 example]# git push -u origin master
输入github用户名,密码

最后到GitHub的仓库上查看;

22.8 克隆远程仓库

1.复制远程仓库的URL链接:

2、本地机器上执行命令进行克隆

[root@aminglinux example]# cd /home/
[root@aminglinux home]# ls
mysql
[root@aminglinux home]# git clone https://github.com/xou6363/example.git
正克隆到 'example'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
[root@aminglinux home]# ls
example  mysql

注:公开的仓库是任何人都可以进行克隆的,但是只能克隆不可以对仓库进行写操作。

3.对克隆的文件进行更改,然后再推送到远程的仓库

[root@aminglinux home]# cd example/
[root@aminglinux example]# echo "423420" >> example.txt 
[root@aminglinux example]# git add example.txt
[root@aminglinux example]# git commit -m "change example.txt"
[master 486c5ea] change example.txt
 1 file changed, 1 insertion(+)
 create mode 100644 example.txt
[root@aminglinux example]# git config --global push.default simple
[root@aminglinux example]# git push
Username for 'https://github.com': xou6363
Password for 'https://[email protected]': 
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 281 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/xou6363/example.git
   463ffd6..486c5ea  master -> master

3.然后到GitHub上看看是否有更改的内容

4.在GitHub上更改文件的内容,更改之后同样可以在本地把新内容拉下来

[root@aminglinux example]# git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
来自 https://github.com/xou6363/example
   486c5ea..4997d11  master     -> origin/master
更新 486c5ea..4997d11
Fast-forward
 example.txt | 1 +
 1 file changed, 1 insertion(+)
[root@aminglinux example]# cat example.txt 
423420
95279527

22.9 分支管理

查看当前分支:

[root@aming3 ~]# cd /data/gitroot/
[root@aming3 gitroot]# git branch
* master
[root@aming3 gitroot]# ls
test.java

创建分支:

[root@aminglinux gitroot]# git branch example
[root@aminglinux gitroot]# git branch
  example
* master
[root@aminglinux gitroot]# ls
test.java

切换分支,创建新文件

[root@aminglinux gitroot]# git checkout example
切换到分支 'example'
[root@aminglinux gitroot]# git branch
* example
  master
[root@aminglinux gitroot]# ls
test.java
[root@aminglinux gitroot]# echo "abcdefg" > test.txt
[root@aminglinux gitroot]# git add test.txt
[root@aminglinux gitroot]# git commit -m "add test.txt"
[example 4b79e84] add test.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
[root@aminglinux gitroot]# ls
test.java  test.txt

切换到master分支后,可以发现该分支下没有刚创建的文件

[root@aminglinux gitroot]# git checkout master 
切换到分支 'master'
[root@aminglinux gitroot]# ls
test.java

这说明分支跟分支之间是相互隔离开的,在当前分支下进行的操作不会影响到其他分支。

分支的合并

合并example分支:git merge example

[root@aminglinux gitroot]# git checkout master
已经位于 'master'
[root@aminglinux gitroot]# git branch
  example
* master
[root@aminglinux gitroot]# ls
test.java
[root@aminglinux gitroot]# git merge example
更新 0b31f31..4b79e84
Fast-forward
 test.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
[root@aminglinux gitroot]# ls
test.java  test.txt
  • 删除分支:git branch -d +分支
  • 强制删除:git branch -D +分支

对于分支的应用,建议大家以这样的原则来:

  • master分支是非常重要的,线上发布代码用这个分支,平时我们开发代码不要在这个分支上。
  • 创建一个dev分支,专门用作开发,只有当发布到线上之前,才会把dev分支合并到master
  • 开发人员应该在dev的基础上再分支成个人分支,个人分支(在自己pc上)里面开发代码,然后合并到dev分支

22.10 远程分支管理

  • 在GitHub上创建一个dev分支;

  • 克隆远程的仓库

[root@aminglinux gitroot]# cd /tmp/
[root@aminglinux tmp]# git clone https://github.com/xou6363/example.git
正克隆到 'example'...
remote: Counting objects: 9, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 9 (delta 0), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (9/9), done.
  • 查看远程仓库所有分支的命令:git ls-remote origin
[root@aminglinux tmp]# cd example
[root@aminglinux example]# git branch
* master
[root@aminglinux example]# ls
example.txt  README.md
[root@aminglinux example]# git ls-remote origin
4997d116341b9cb0f66dcc79a2373c388e5ff3f0	HEAD
4997d116341b9cb0f66dcc79a2373c388e5ff3f0	refs/heads/dev
4997d116341b9cb0f66dcc79a2373c388e5ff3f0	refs/heads/master
  • 本地创建和远程分支对应的分支并添加文件推送
[root@aminglinux example]# git checkout -b dev origin/dev
分支 dev 设置为跟踪来自 origin 的远程分支 dev。
切换到一个新分支 'dev'
[root@aminglinux example]# git branch
* dev
  master
[root@aminglinux example]# ls
example.txt  README.md
[root@aminglinux example]# echo "123456abc" > test.txt
[root@aminglinux example]# git add test.txt
[root@aminglinux example]# git commit -m "add test.txt"
[dev 7371227] add test.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt

关于git push分支的两种情况

1.当本地分支和远程分支一致时,git push默认会把所有本地分支的变更一同推送到远程(matching模式下),如果想只推送某一个分支,可以使用git push origin branch-name命令:

[root@aminglinux example]# git push origin dev
Username for 'https://github.com': xou6363
Password for 'https://[email protected]': 
Everything up-to-date

2.当本地分支比远程分支多,默认git push 只推送本地和远程一致的分支,想要把多出来的本地分支推送到远程时,使用git push origin branch-name 命令, 如果推送失败,先用git pull抓取远程的新提交:

[root@aminglinux example]# git branch dev2 //创建一个新的本地分支
[root@aminglinux example]# git branch
* dev
  dev2
  master
[root@aminglinux example]# git checkout dev2
切换到分支 'dev2'
[root@aminglinux example]# ls
example.txt  README.md  test.txt
[root@aminglinux example]# echo "123" > dev2.txt
[root@aminglinux example]# git add dev2.txt
[root@aminglinux example]# git commit -m "add dev2.txt"
[dev2 a958326] add dev2.txt
 1 file changed, 1 insertion(+)
 create mode 100644 dev2.txt
[root@aminglinux example]# git push origin dev2   //指定分支推送到远程
Username for 'https://github.com': xou6363
Password for 'https://[email protected]': 
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 333 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/xou6363/example.git
 * [new branch]      dev2 -> dev2

查看远程仓库的分支,可以看到多了一个dev2分支;

22.11 标签管理

标签类似于虚拟机的快照功能,可以给版本库打一个标签,记录某个时刻库的状态,也可以随时恢复到该标签标记的状态;

  • 通常情况下我们都是对master分支打标签(其他分支也可以),切换到master分支上,给master打一个标签v1.0;
[root@aminglinux example]# git checkout master
切换到分支 'master'
[root@aminglinux example]# git tag v1.0  //给master打一个标签v1.0
[root@aminglinux example]# git show v1.0  //查看标签的信息
commit 4997d116341b9cb0f66dcc79a2373c388e5ff3f0
Author: xou6363 <[email protected]>
Date:   Thu Aug 30 14:17:17 2018 +0800

    Update example.txt

diff --git a/example.txt b/example.txt
index a4029ba..ee9b693 100644
--- a/example.txt
+++ b/example.txt
@@ -1 +1,2 @@
 423420
+95279527
[root@aminglinux example]# git tag   //查看当前分支下的所有标签
v1.0
  • tag是针对commit来打标签的,可以针对历史的commit来打tag

  • 查看历史的commit:git log --pretty=oneline --abbrev-commit

[root@aminglinux example]# git log --pretty=oneline --abbrev-commit
4997d11 Update example.txt
486c5ea change example.txt
463ffd6 first commit
[root@aminglinux example]# git tag v2.0 463ffd6 //针对历史commit打标签
[root@aminglinux example]# git tag
v1.0
v2.0
  • 标签描述
[root@aminglinux example]# git tag -a v0.1 -m "first tag" 463ffd6  //-m指定描述信息
 [root@aminglinux example]# git tag
 v0.1
 v1.0
 v2.0
 [root@aminglinux example]# git show v0.1
 tag v0.1
 Tagger: xou6363 <[email protected]>
 Date:   Thu Aug 30 21:26:22 2018 +0800
 
 first tag
 
 commit 463ffd6028848d64ddda5648d667565c471c0c5a
 Author: xou6363 <[email protected]>

-+0800

first commit
 
 diff --git a/README.md b/README.md
 new file mode 100644
 index 0000000..02dc8ac
 --- /dev/null
 +++ b/README.md
 @@ -0,0 +1 @@
 +# example
  • 删除标签
[root@aminglinux example]# git tag -d v0.1
已删除 tag 'v0.1'(曾为 ccf4a6e)
[root@aminglinux example]# git tag
v1.0
v2.0
  • 标签推送到远程仓库
[root@aminglinux example]#  git push origin v1.0
Username for 'https://github.com': xou6363
Password for 'https://[email protected]': 
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/xou6363/example.git
 * [new tag]         v1.0 -> v1.0
  • 推送所有标签到远程仓库
[root@aminglinux example]# git push --tag origin
Username for 'https://github.com': xou6363
Password for 'https://[email protected]': 
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/xou6363/example.git
 * [new tag]         v2.0 -> v2.0

远程仓库上查看可以刚推送的标签;

如果本地删除了一个标签,远程也想要删除需要先删除本地标签,后再删除远程标签

[root@aminglinux example]# git push origin :refs/tags/v1.0
Username for 'https://github.com': xou6363
Password for 'https://[email protected]': 
To https://github.com/xou6363/example.git
 - [deleted]         v1.0

22.12 git别名

用别名可以缩短命令的长度提高工作效率。

  • git config --global alias.ci commit //把commit的别名设置为ci

  • git config --global alias.co checkout //把checkout的别名设置为co

  • git config --global alias.br branch //把branch的别名设置为br

  • 查看git命令的别名:git config --list |grep alias

  • 取消别名:git config --global --unset alias.别名

配置log命令的别名,可以让log查询的结果更易读

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

22.13 搭建git服务器

GitHub只有公开库是免费的,而私有仓库是需要花钱买的。所以我们可以想办法自己搭建一个私有的。Gitlab是个不错的选择。在介绍它之前,先讲述一下如何搭建命令行的git服务器。

准备环境:

服务器IP:192.168.222.111 客户端IP:192.168.222.110

1、首先在服务器上安装git

yum -y install git

2、添加git用户,并且设置shell为/usr/bin/git-shell,目的是为了不让git用户远程登陆,并且在该用户的家目录下创建authorized_keys文件,并更改属主、属组和权限,用来存客户端机器上的公钥:

[root@aming2 ~]# useradd -s /usr/bin/git-shell git
 [root@aming2 ~]# cd /home/git/
 [root@aming2 git]# mkdir .ssh/
 [root@aming2 git]# touch .ssh/authorized_keys
 [root@aming2 git]# chmod 600 .ssh/authorized_keys
 [root@aming2 git]# chown -R git:git .ssh
 [root@aming2 git]# passwd git 
 更改用户 git 的密码 。
 新的 密码:
 重新输入新的 密码:
 passwd:所有的身份验证令牌已经成功更新。

3、然后把客户端上的公钥复制到服务器的authorized_keys文件中,如果没有密钥则使用ssh-keygen命令生成

客户端:复制密钥
[root@aminglinux ~]# cat .ssh/id_rsa.pub
服务端:添加密钥
[root@aming2 git]# vim .ssh/authorized_keys

4、到客户端上使用ssh连接git用户,输出结果如下代表没问题,因为我们设置了不让git用户远程登陆

[root@aminglinux ~]# ssh [email protected]
Enter passphrase for key '/root/.ssh/id_rsa': 
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.
Connection to 192.168.222.111 closed.

5、在服务端创建git仓库的目录;在该目录下创建一个裸仓库,裸仓库没有工作区,因为服务器上的Git仓库纯粹是为了共享,所以不让用户直接登录到服务器上去改工作区,并且服务器上的Git仓库通常都以.git结尾:

[root@aming2 git]# mkdir /data/gitroot
[root@aming2 git]# cd /data/gitroot
[root@aming2 gitroot]# git init --bare sample.git
初始化空的 Git 版本库于 /data/gitroot/sample.git/
[root@aming2 gitroot]# ls
sample.git
[root@aming2 gitroot]# chown -R git.git sample.git

6、在客户端上克隆远程仓库,添加代码并推送到远程

[root@aminglinux ~]# git clone [email protected]:/data/gitroot/sample.git
正克隆到 'sample'...
Enter passphrase for key '/root/.ssh/id_rsa': 
warning: 您似乎克隆了一个空版本库。
[root@aminglinux ~]# cd sample/
[root@aminglinux sample]# ll -a
总用量 4
drwxr-xr-x  3 root root   18 8月  30 22:24 .
dr-xr-x---. 6 root root 4096 8月  30 22:24 ..
drwxr-xr-x  7 root root  119 8月  30 22:24 .git
[root@aminglinux sample]# echo "0987654321" > test.txt
[root@aminglinux sample]# git add test.txt
[root@aminglinux sample]# git commit -m "add test.txt"
[master(根提交) cdf8d20] add test.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
[root@aminglinux sample]# git push origin master   //因为是裸仓库,所以需要指定分支进行提交
Enter passphrase for key '/root/.ssh/id_rsa': 
Counting objects: 3, done.
Writing objects: 100% (3/3), 215 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:/data/gitroot/sample.git
 * [new branch]      master -> master

7、测试:到另一个目录下进行克隆,看看是否能从服务端上克隆

[root@aminglinux sample]# cd /tmp
[root@aminglinux tmp]# git clone [email protected]:/data/gitroot/sample.git
正克隆到 'sample'...
Enter passphrase for key '/root/.ssh/id_rsa': 
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
接收对象中: 100% (3/3), done.
[root@aminglinux tmp]# cd sample/
[root@aminglinux sample]# ll -a
总用量 8
drwxr-xr-x   3 root root   34 8月  30 22:32 .
drwxrwxrwt. 14 root root 4096 8月  30 22:32 ..
drwxr-xr-x   8 root root  163 8月  30 22:32 .git
-rw-r--r--   1 root root   11 8月  30 22:32 test.txt
[root@aminglinux sample]# cat test.txt
0987654321

成功克隆则代表该git服务器已经能够正常提供服务。

22.14/22.15 安装gitlab

注:官方说安装gitlab要求服务器内存最好不少于4g ,gitlab的社区版是免费的,企业版则是收费的。

  • 配置镜像源:
[root@aming2 ~]# vim /etc/yum.repos.d/gitlab.repo
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1
  • 安装:yum install -y gitlab-ce

  • 配置:gitlab-ctl reconfigure

  • 检查进程和端口:

[root@aming2 ~]# ps aux |grep git 
[root@aming2 ~]# netstat -lntp
  • 重启、启动、停止gitlab服务以及查看服务状态:gitlab-ctl {stop | restart | star | status}

网页登陆,设置密码。报502错误,检查下端口。

22.16 简单使用gitlab

  • 配置gitlab内置的nginx服务器,配置文件所在的路径:
[root@aming2 ~]# ls /var/opt/gitlab/nginx/conf/
gitlab-http.conf  nginx.conf  nginx-status.conf
[root@aming2 ~]# vim /var/opt/gitlab/nginx/conf/gitlab-http.conf
  • 域名和监听端口在gitlab-http.conf文件中配置,如果机器上只跑一个gitlab服务就不用配置保持默认。

  • 在gitlab上新建一个用户组(New group),在新建组里创建一个项目(New project),在设置添加密钥;

22.17gitlab备份和恢复

netstat -lnpt  //查看监听端口
 gitlab-ctl stop/restart/start/status
 浏览器访问gitlab,输入ip即可
 默认管理员root,无密码,它会让我们去定义一个密码
 gitlab常用命令  https://www.cnyunwei.cc/archives/1204
 gitlab备份  gitlab-rake gitlab:backup:create
 备份目录在/var/opt/gitlab/backups
 gitlab 恢复  先停服务 gitlab-ctl stop unicorn ; gitlab-ctl stop sidekiq
 gitlab-rake gitlab:backup:restore BACKUP=xxxxx (这里是一个编号,即备份文件的前缀)
 再启动服务 gitlab-ctl start
备份:gitlab-rake gitlab:backup:create
备份目录:/var/opt/gitlab/backups
恢复数据需要停掉以下两个服务:
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
恢复数据:gitlab-rake gitlab:backup:restore BACKUP=(备份的目录,格式:1535953489_2018_09_03_11.2.3)
启动服务:gitlab-ctl start

猜你喜欢

转载自blog.csdn.net/xou6363/article/details/82320356
今日推荐