git服务器搭建
2020-02-20Linux90root429°c
A+ A-github毕竟是公开的,创建私有仓库又得花钱买。所以我们可以想办法搭建一个私有的,只自己公司使用的。
安装git
[[email protected] ~]# yum install git [[email protected] ~]# yum install -y epel-release
添加git用户,并且设置shell为/usr/bin/git-shell,目的是为了不让git用户远程登陆
[[email protected] ~]# useradd -s /usr/bin/git-shell git [[email protected] ~]# cd /home/git
创建authorized_keys文件,并更改属主、属组和权限,用来存客户端机器上的公钥
[[email protected] git]# mkdir .ssh [[email protected] git]# touch .ssh/authorized_keys [[email protected] git]# chown -R git.git .ssh [[email protected] git]# chmod 700 .ssh
把客户端的公钥添加到服务器里
[[email protected] ~]# cat /root/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAvIcb2P49rw4XU6xTMnM6mw6a2+8Km21S2TXAf976XRCAh3VAPofacXhhmBleJzTNbt4xyQYV5P36acWdL8ru4T9S9zFpx7l5LCu3Rc+v/xf8+wEdTTCiZ8VzR9O86Valjae0ZpIFb9JtVyLFJWrlV+YGXo6PFCUAkiOkXFn/99DlaKZTwJOC8KODW+KLZeETKp/fUyLh61WwYUYFA95uwworfgD8KhtKu6x7OKMG/L36Xr53H5xtnXw6EQYU3qJdDoOysPffjh+Bpe6yd0ThvDiX5XAV6IsQcY+NrQA0eRZ2Herh3Ajgz4bcH2XhnyZBXVdaNJ8FYdCYNc8lKEInOQ== [email protected]
添加到服务器
[[email protected] git]# vim .ssh/authorized_keys [[email protected] git]# usermod -s /bin/bash git
在客户端登陆服务器
[[email protected] ~]# ssh [email protected] Welcome to aliyun Elastic Compute Service!
可以登陆了就改回去(目的是不让他登陆,为了安全)
[[email protected] git]# usermod -s /usr/bin/git-shell git
搭建仓库存储目录
[[email protected] git]# mkdir -p /data/gitroot [[email protected] git]# cd /data/gitroot/
创建一个裸仓库,裸仓库没有工作区,因为服务器上的Git仓库纯粹是为了共享,所以不让用户直接登录到服务器上去改工作区,并且服务器上的Git仓库通常都以.git结尾
[[email protected] gitroot]# git init --bare sample.git Initialized empty Git repository in /data/gitroot/sample.git/ [[email protected] gitroot]# ls -la total 12 drwxr-xr-x 3 root root 4096 Sep 17 22:37 . drwxr-xr-x 5 root root 4096 Sep 17 22:35 .. drwxr-xr-x 7 root root 4096 Sep 17 22:37 sample.git [[email protected] gitroot]# ls sample.git/ branches config description HEAD hooks info objects refs [[email protected] gitroot]# chown -R git.git sample.git
以上所有的操作都是在Git服务器上进行
在客户端上(自己pc)克隆远程仓库
平时git服务器是不需要开发人员登录修改代码的,它仅仅是充当着一个服务器的角色,就像github一样,平时操作都是在我们自己的pc上做的。 首先要把客户端上的公钥放到git服务器上/home/git/.ssh/authorized_keys文件里
[[email protected] ~]# cd /home/ gitroot/ lanmp/ mysql/ php-fpm/ yunweigit-/ [[email protected] ~]# cd /home/ [[email protected] home]# ls gitroot lanmp mysql php-fpm yunweigit- [[email protected] home]# git clone [email protected]:/data/gitroot/sample.git Initialized empty Git repository in /home/sample/.git/ warning: You appear to have cloned an empty repository. [[email protected] home]# ls gitroot lanmp mysql php-fpm sample yunweigit- [[email protected] home]# cd sample/ [[email protected] sample]# cd .git/ [[email protected] .git]# ls branches config description HEAD hooks info objects refs [[email protected] .git]# cat config [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true [remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/* url = [email protected]:/data/gitroot/sample.git [branch "master"] remote = origin merge = refs/heads/master
创建文件并上传
[[email protected] sample]# git add azhen.txt [[email protected] sample]# git commit -m "new file azhen.txt" [master (root-commit) 5f2139b] new file azhen.txt 1 files changed, 2 insertions(+), 0 deletions(-) create mode 100644 azhen.txt [[email protected] sample]# git status # On branch master nothing to commit (working directory clean)
推送到服务器或者远程仓库去
[[email protected] sample]# git push -u origin master Counting objects: 3, done. Writing objects: 100% (3/3), 222 bytes, done. Total 3 (delta 0), reused 0 (delta 0) To [email protected]:/data/gitroot/sample.git * [new branch] master -> master Branch master set up to track remote branch master from origin.
第二次推送用这个命令即可
[[email protected] sample]# git pushgithub