搭建git服务器
1、安装git
服务器端是腾讯云的Linux(CentOS7),客户端是自己笔记本的Windows,分别安装git。
服务器端
yum源安装git安装命令
# yum install -y git
也可以编译安装git,可参考Linux(CentOS7)安装git。
客户端
官网(https://git-for-windows.github.io/)下载安装包进行安装。
安装成功,使用Git Bash作为命令行客户端。
查看git版本
$ git --version
2、服务器端创建git用户,管理git服务
# id git
# useradd git
# passwd xxxx
# id git
Linux id命令用于显示用户的ID,以及所属群组的ID。
3、服务器端创建git仓库
进入目录/home/data/git,创建名为estA.git的空git仓库,将git仓库的owner修改为git。
[root@VM_0_10_centos git]# mkdir -p testA.git
[root@VM_0_10_centos git]# git init --bare testA.git
Initialized empty Git repository in /home/data/git/testA.git/
[root@VM_0_10_centos git]# chown -R git:git testA.git/
在这里,git init --bare生成的是一个裸仓库,是没有工作区的,只会记录git提交的历史信息,git log是可以看到各个版本信息的,但是没办法进行版本回退或者切换分支的操作。
4、客户端clone服务器端远程仓库
在Git Bash命令行客户端,创建项目地址(d/GitItems/gittest_gitbash),并进入
shaotiliu@SH5002306701 MINGW64 /d/GitItems/gittest_gitbash
$
从Linux Git服务器上clone项目
shaotiliu@SH5002306701 MINGW64 /d/GitItems/gittest_gitbash
$ git clone git@132.232.19.205:/home/data/git/testA.git
Cloning into 'testA'...
warning: You appear to have cloned an empty repository.
当第一次连接目标git服务器时会得到一个提示:
The authenticity of host '192.168.56.101 (192.168.56.101)' can't be established.
RSA key fingerprint is SHA256:Ve6WV/SCA059EqoUOzbFoZdfmMh3B259nigfmvdadqQ.
Are you sure you want to continue connecting (yes/no)?
输入yes
完成后,目录C:\Users\用户名.ssh下生成了文件known_hosts。
5、客户端创建SSH公匙和私匙
$ ssh-keygen -t rsa -C "邮件地址"
将邮件地址替换成自己的邮件地址,一路回车。
此时C:\Users\用户名.ssh下会多出两个文件id_rsa(私匙)和id_rsa.pub(公匙)。
服务器端进入/etc/ssh目录,编辑sshd_config文件,这个文件是ssh服务器的配置文件,打开注释了的属性,设置为
StrictModes No
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
重启sshd服务,命令
# systemctl restart sshd.service
从sshd_config文件中的AuthorizedKeysFile属性可知公匙的存放路径为.ssh/authorized_keys,实际上是 $Home/.ssh/authorized_keys,由于管理 Git 服务的用户是 git,所以实际存放公钥的路径是 /home/git/.ssh/authorized_keys
在/home/git下创建目录.ssh,在目录.ssh下创建文件authorized_keys
并将客户端得到的id_rsa.pub(公匙)中的内容复制到服务器端的文件authorized_keys中去。
然后将.ssh文件夹的owner修改为git
[root@VM_0_10_centos git]# chown -R git:git .ssh
[root@VM_0_10_centos git]# ll -a
total 16
drwxr-xr-x 4 root root 4096 Nov 22 16:11 .
drwxr-xr-x 3 root root 4096 Nov 16 09:53 ..
drwxr-xr-x 7 git git 4096 Nov 22 16:11 testA.git
修改.ssh目录的权限为700,修改.ssh/authorized_keys文件的权限为600
[root@VM_0_10_centos git]# chmod 700 .ssh
[root@VM_0_10_centos git]# cd .ssh
[root@VM_0_10_centos git]# chmod 600 authorized_keys
6、客户端再次clone服务器端远程仓库
shaotiliu@SH5002306701 MINGW64 /d/GitItems/gittest_gitbash
$ git clone git@132.232.19.205:/home/data/git/testA.git
Cloning into 'testA'...
warning: You appear to have cloned an empty repository.
在客户端项目目录下d/GitItems/gittest_gitbash已经有着clone下来的项目了
也可以使用TortoiseGit客户端来管理项目,安装TortoiseGit后右键git clone
7、客户端提交版本库
$ git add . //添加文件到版本库(只是添加到缓存区),.代表添加文件夹下所有文件,值单独添加某个文件时命令为git add 文件名
$ git commit -m "first commit" //把添加的文件提交到版本库,并填写提交备注
此时提交的代码提交到本地的版本库。
8、客户端代码提交到远程服务端
$ git remote add origin 远程库地址 //把本地库与远程库关联
$ git push -u origin master //第一次推送时
$ git push origin master //第一次推送后,直接使用该命令即可推送修改
使用push命令,实际上是把当前分支master推送到远程。
客户端
shaotiliu@SH5002306701 MINGW64 /d/GitItems/gittest_gitbash/gittest (master)
$ git remote add origin git@132.232.19.205
fatal: remote origin already exists.
shaotiliu@SH5002306701 MINGW64 /d/GitItems/gittest_gitbash/gittest (master)
$ git push -u origin master
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 4 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (7/7), 1.58 KiB | 231.00 KiB/s, done.
Total 7 (delta 0), reused 0 (delta 0)
To 132.232.19.205:/home/data/git/gittest.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
此时,已把客户端本地版本库的代码推送到服务端版本库。
服务端
[root@VM_0_10_centos gittest.git]# git log
commit 0664aa71a09c8b86884f59210a5c6955340be372
Author: XXXXX
Date: Thu Nov 22 15:38:43 2018 +0800
second commit based on updating book.txt
commit 66ce5ea322297c2ca5475d1cc3bdd2ae0a27f6aa
Author: XXXXX
Date: Thu Nov 22 14:31:37 2018 +0800
first commit based on blank
命令git log查看提交历史。
参考网址:
https://www.cnblogs.com/dee0912/p/5815267.html
https://blog.csdn.net/qq_32685139/article/details/52426155