1.部署Git服务器
1.1git服务端搭建
Git是分布式的版本控制系统,我们只要有了一个原始Git版本仓库,就可以让其他主机克隆走这个原始版本仓库,从而使得一个Git版本仓库可以被同时分布到不同的主机之上,并且每台主机的版本库都是一样的,没有主次之分,极大的保证了数据安全性,并使得用户能够自主选择向那个Git服务器推送文件了,其实部署一个git服务器是非常简单的事情,我们需要用到两台主机,分别是:
主机名称 | 操作系统 | IP地址 |
Git服务器 | Centos7操作系统 | 192.168.56.32 |
Git客户端 | Centos7操作系统 | 192.168.56.33 |
1.首先我们分别在Git服务器和客户机中安装Git服务程序(刚刚实验安装过就不用安装了):
[root@gitserver ~]# yum install git Loaded plugins: fastestmirror Package git-1.8.3.1-12.el7_4.x86_64 already installed and latest version Nothing to do
2.然后创建Git版本仓库并修改所有者与所有组,一般规范的方式要以.git为后缀
[root@gitserver ~]# mkdir linuxprobe.git [root@gitserver ~]# chown -Rf git:git linuxprobe.git/
3.初始化Git版本仓库
[root@gitserver linuxprobe.git]# git --bare init Initialized empty Git repository in /root/linuxprobe.git/
1.2.客户端创建密钥
其实此时你的Git服务器就已经部署好了,但用户还不能向你推送数据,也不能克隆你的Git版本仓库,因为我们要在服务器上开放至少一种支持Git的协议,比如HTTP/HTTPS/SSH等,现在用的最多的就是HTTPS和SSH,我们切换至Git客户机来生成SSH密钥:
[root@gitclient ~]# 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:
5f:39:80:27:a2:cf:92:b7:74:60:d1:e6:d6:bc:c7:3f root@gitclient
The key's randomart image is:
+--[ RSA 2048]----+
| |
| . . |
| o = o |
| . = = . . |
| . o S o + |
| = o . + . |
| o = . o o |
| + o . .E |
| . .. |
+-----------------+
1.将客户机的公钥传递给Git服务器
[root@gitclient ~]# ssh-copy-id 192.168.56.32 The authenticity of host '192.168.56.32 (192.168.56.32)' can't be established. ECDSA key fingerprint is 97:a2:46:3f:6c:cd:3a:11:eb:27:03:40:b7:28:7f:af. Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@192.168.56.32's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '192.168.56.32'" and check to make sure that only the key(s) you wanted were added.
2.此时就已经可以从Git服务器中克隆版本仓库了(此时目录内没有文件是正常的):
[root@gitclient ~]# git clone root@192.168.56.32:/root/linuxprobe.git Cloning into 'linuxprobe'... warning: You appear to have cloned an empty repository. [root@gitclient ~]# cd linuxprobe/ [root@gitclient linuxprobe]#
3.初始化git
工作环境
[root@gitclient linuxprobe]# git config --global user.name "xie jc" [root@gitclient linuxprobe]# git config --global user.email "root@linuxprobe.com" [root@gitclient linuxprobe]# git config --global core.editor vim [root@gitclient linuxprobe]# git config --global color.ui
4.向Git版本仓库中提交一个新文件
[root@gitclient linuxprobe]# echo "I successfully cloned the Git repository" > readme.txt [root@gitclient linuxprobe]# git add readme.txt [root@gitclient linuxprobe]# git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: readme.txt # [root@gitclient linuxprobe]# git commit -m "Clone the Git repository" [master (root-commit) a114b26] Clone the Git repository 1 file changed, 1 insertion(+) create mode 100644 readme.txt [root@gitclient linuxprobe]# git status # On branch master nothing to commit, working directory clean
1.3远程git服务器
1.定义远程git服务器
前次的操作还是只将文件提交到了本地的Git版本仓库,并没有推送到远程Git服务器,所以我们来定义下远程的Git服务器
[root@gitclient linuxprobe]# git remote add server root@192.168.56.32:/root/linuxprobe.git
2.将文件提交到远程Git服务器
[root@gitclient linuxprobe]# git push -u server master Counting objects: 3, done. Writing objects: 100% (3/3), 262 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To root@192.168.56.32:/root/linuxprobe.git * [new branch] master -> master Branch master set up to track remote branch master from server.
为了验证真的是推送到了远程的Git服务,你可以换个目录再克隆一份版本仓库(虽然在工作中毫无意义)
[root@gitclient linuxprobe]# cd ../Disktop/ [root@gitclient Disktop]# git clone root@192.168.56.32:/root/linuxprobe.git Cloning into 'linuxprobe'... remote: Counting objects: 3, done. remote: Total 3 (delta 0), reused 0 (delta 0) Receiving objects: 100% (3/3), done. [root@gitclient Disktop]# cd linuxprobe/ [root@gitclient linuxprobe]# cat readme.txt I successfully cloned the Git repository
1.4命令小结
git --bare init #初始化git版本仓库--bare将存储库视为一个空库。如果没有设置git_dir环境,它被设置为当前工作目录。 ssh-keygen #创建公钥 ssh-copy-id IP #将客户机的公钥传递给Git服务器 git clone root@server-IP:/root/linuxprobe.git #从Git服务器中克隆版本仓库 git config --global user.name "xiejc" #创建版本用户名 git config --global user.email "root@linuxprobe.com" #创建版本邮箱 git config --global core.editor vim #使用vim命令 git config --global color.ui #语法高亮 git add [file1] ... #提交至缓存区 git status #查看git数据版本状态 git remote add server root@server-IP:/root/linuxprobe.git #定义远程的Git服务器 git push -u server master #将文件提交到远程Git服务器 git remote -v ##查看git版本仓库名称 git pull ##经过认证之后可以用来把代码更新下来已经git clone之后,合并到当前分支 git fetch ##跟pull的区别就是git fetch会把代码下来,不会合并到当前分支,单建一个分支存起来 git push gitlab news ##git push到远程gitlab这个库里面news分支
Get busy living or get busy dying. 努力活出精彩的人生,否则便如行尸走肉
如无特殊说明,文章均为本站原创,转载请注明出处
- 转载请注明来源:Git系列之五部署Git服务器
- 本文永久链接地址:https://www.xionghaier.cn/archives/33.html