把本地的仓库推送到远程仓库 gitlab上

实现gitlab和本地之间进行推送仓库,首先要做ssh认证

 

配置 SSHKEY

前面我们已经在 GitLab 创建了仓库,并且授权用户可以使用仓库,我们所有的操作都是在 WEB 页面进行,下在我们介绍如何使用客户端来连接我们的仓库。

我们的仓库是私有的,只有授权的用户才可以访问到该仓库,那么只要将客户端的用户与我们 GitLab 的用户绑定,客户端即可访问到 GitLab 上的仓库,

我们建议使用 SSH 方式实现客户端与 Gitlab 用户的绑定,具体配置如下:

在客户端生成 ssh 密钥对(注 windows 客户端下只能使用 rsa 加密方式):

我们在本地客户端生成公钥,把公钥配到对应的gitlab用户的ssh key上

 

[root@ci-node1 ~]# ssh-keygen -t rsa
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:dKgV7ndo1haY4XeUseN/yjj7/2AWNH1wC2GpK/fLfuY root@ci-node1
The key's randomart image is:
+---[RSA 2048]----+
|        . .  +*o.|
|       . + +.oo=.|
|        = = + *.o|
|       = . = = o.|
|      . S = = o  |
|         = =   o |
|          o . + o|
|            o* =.|
|            +BOE+|
+----[SHA256]-----+

 

 

[root@ci-node1 ~]# ll .ssh/
total 8
-rw------- 1 root root 1679 Mar 29 17:55 id_rsa
-rw-r--r-- 1 root root  395 Mar 29 17:55 id_rsa.pub

 

[root@ci-node1 ~]# cat .ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCstYVepXE4/B85nKNU6gaVpPsjGXu6Ql2sKRgRDxsHGmuZVg+q0GD3c0Ml2XkzozswTRuKCcIhZjK/DEGbbgsBEH8q6HHrWCP6vAWb+7wIEvhtJVm6+5zOGA/AorFczEgnm9b2ekPkwAMV7qrOQ1KG9S94U+XRwAtjgNV0MuYx5S0liBr9KBEt8pQ7Fd2TMo95rF3328gGnWLZJWmETjtJaWpLKDgWnQ7BdlK89o6eyYBtdKJfZ5uYM3tgeX8GiRA+daR8fpEruOSpjuvRbyjRStf5ZXPIemL46lXcEDYurnQ4Uucv5F+paqTWnzYHziQOoKd3Jnxgqsh24TZeoEDP root@ci-node1

 

我们将该用户与 GitLab 的 root 用户绑定,复制用户的公钥,在 GitLab 主页面点击用户设置

gitlab 推出diff gitlab推送_gitlab 推出diff

 

 

进入到用户设置页面,点击左侧菜单栏的 SSH KEYS

gitlab 推出diff gitlab推送_gitlab 推出diff_02

 

 

进入到 SSH KEYS 管理页面

 

gitlab 推出diff gitlab推送_gitlab 推出diff_03

 

 

此处只能添加公钥,如果添加私钥系统将报错,此外一个公钥在整个 GitLab 系统中只能添加一次,但是一个 GitLab 用户可以添加多个公钥。

 

gitlab 推出diff gitlab推送_ci_04

 

 

 配置完了,本地客户端和gitlab认证完成了

 

推送本地客户端仓库到 GitLab

把git_test仓库推到远程仓库 gitlab 本地已经有git_test仓库

首先我们要将GitLab上的git_test仓库配置为ci-node1上git_test仓库的远程仓库,

 为他配置远程仓库

gitlab 推出diff gitlab推送_ci_05

 

 

 

[root@ci-node1 git_test]# git remote add gitlab git@192.168.31.11:test/git_test.git
[root@ci-node1 git_test]# git remote
gitlab

其次,使用 git push 命令直接推送本地仓库的 master 分支到远程仓库

[root@ci-node1 git_test]# git push -u gitlab master
The authenticity of host '192.168.31.11 (192.168.31.11)' can't be established.
ECDSA key fingerprint is SHA256:Eal3aUwcGcxH1ZD9X799taSiGiCMFgAE7rnoY74f25U.
ECDSA key fingerprint is MD5:27:41:0f:5a:4c:ba:8f:23:bd:23:44:8d:18:53:e6:30.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.31.11' (ECDSA) to the list of known hosts.
Counting objects: 30, done.
Compressing objects: 100% (18/18), done.
Writing objects: 100% (30/30), 2.50 KiB | 0 bytes/s, done.
Total 30 (delta 2), reused 0 (delta 0)
To 192.168.31.11:test/git_test.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from gitlab.

提示推送功能,我们在 GitLab 上的 git_test 仓库就可以看到我们推送上来的内容,

gitlab 推出diff gitlab推送_git_06

 

 认证是正确的,现在实现了本地的仓库和远程gitlab的集成,能把仓库推上去

 

克隆 GitLab 仓库到本地客户端

我们要把gitlab 的仓库 克隆到另外一台机器上

我们在另外一台机器上 ci-node2 安装git服务

参考这个帖子  源码安装git

 

安装好git后  配置git全局配置 邮箱 name


 

ci-node2客户端生成ssh 公钥 做ssh认证

[root@ci-node2 ~]# ssh-keygen -t rsa
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:pCb3ozG5sS3ZrnkDUsLTD5RTSule2QxE6oCcmAlzKTU root@ci-node2
The key's randomart image is:
+---[RSA 2048]----+
|o.E.  .*+        |
|oB.+ .=o.        |
|+.+..+o..=       |
|    +o=oo o      |
|    .==+S        |
|    .+oo.        |
|     .=+o        |
|      oX+.       |
|      *=o.       |
+----[SHA256]-----+

 

把公钥配置到gitlab

 

 

[root@ci-node2 ~]# ll .ssh/
total 8
-rw------- 1 root root 1675 Mar 29 23:39 id_rsa
-rw-r--r-- 1 root root  395 Mar 29 23:39 id_rsa.pub

 

 

 

首先,我们配置 ci-node2 客户端与 GitLab 上 dev 用户绑定,

 把ci-node2 客户端的公钥配置到 gitlab 上的dev用户绑定

 

登录dev用户

gitlab 推出diff gitlab推送_客户端_07

 

 

 gitlab要求第一次登录dev用户需要重新修改默认密码

gitlab 推出diff gitlab推送_客户端_08

 

 

 把ci-node2 客户端的公钥配置到 gitlab 上的dev用户绑定

gitlab 推出diff gitlab推送_gitlab 推出diff_09

 

 

 

gitlab 推出diff gitlab推送_git_10

 

 

 现在认证已经做完了

第二,使用 git clone 命令克隆仓库到 ci-node2 本地

[root@ci-node2 data]# git clone git@192.168.31.11:test/git_test.git
Cloning into 'git_test'...
The authenticity of host '192.168.31.11 (192.168.31.11)' can't be established.
ECDSA key fingerprint is SHA256:Eal3aUwcGcxH1ZD9X799taSiGiCMFgAE7rnoY74f25U.
ECDSA key fingerprint is MD5:27:41:0f:5a:4c:ba:8f:23:bd:23:44:8d:18:53:e6:30.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.31.11' (ECDSA) to the list of known hosts.
remote: Counting objects: 30, done.
remote: Compressing objects: 100% (18/18), done.
remote: Total 30 (delta 2), reused 0 (delta 0)
Receiving objects: 100% (30/30), done.
Resolving deltas: 100% (2/2), done.
Checking connectivity... done.

我们可以看到已经将 GitLab 上的 git_test 仓库克隆到了 ci-node2 本地,同时为本地仓库添加了一个指向 GitLab 上 git_test 仓库的远程仓库。

执行git clone时候,会默认会仓库添加一个默认的远程仓库 就是gitlab

[root@ci-node2 data]# ll git_test/
total 4
-rw-r--r-- 1 root root 21 Mar 29 23:55 a
-rw-r--r-- 1 root root  0 Mar 29 23:55 master
-rw-r--r-- 1 root root  0 Mar 29 23:55 test

[root@ci-node2 data]# cd git_test/
[root@ci-node2 git_test]# 
[root@ci-node2 git_test]# git remote
origin

 

我们 ci-node2 的 git_test 上创建一个 dev 分支,并将 dev 分支,推送到 GitLab 上:

验证是否默认远程仓库 是否是gitlab 

 

建立dev分支

// 建立dev分支
[root@ci-node2 git_test]# git branch dev
// 切换到dev分支
[root@ci-node2 git_test]# git checkout dev
Switched to branch 'dev'

在dev分支,创建一个文件

[root@ci-node2 git_test]# touch dev

提交commit,需要先配置git全局配置

 

[root@ci-node2 git_test]# git add .
[root@ci-node2 git_test]# git commit -m "commit dev on dev branch"
[dev a0e7b8d] commit dev on dev branch
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 dev

 

把dev分支推到gitlab

[root@ci-node2 git_test]# git status
On branch dev
nothing to commit, working tree clean


[root@ci-node2 git_test]# git push -u origin dev
Counting objects: 2, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 227 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: 
remote: To create a merge request for dev, visit:
remote:   http://192.168.31.11/test/git_test/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote: 
To 192.168.31.11:test/git_test.git
 * [new branch]      dev -> dev
Branch dev set up to track remote branch dev from origin.

完成后,我们在 GitLab 上可以看到我们刚推上来的 dev 分支

 

 

gitlab 推出diff gitlab推送_gitlab 推出diff_11