zoukankan      html  css  js  c++  java
  • Git服务器仓库自建

    前言:

    自己的项目即不想让人知道,也不想花钱买会员,最好就是自建一台git服务器。

    PS:在下面操作命令操作时,git有些命令要在目录内执行,

          比如,你想设置 /home/gitrepo/ 是你的仓库目录,创建新仓库时候就要进入到 /home/gitrepo/ 目录下执行。

          如果,你在 /home/tomcat 下执行,那么tomcat就是你的仓库了,切记。

    服务器端: 

    安装git服务: 

    [root@Cent0S7 ~]# yum -y  install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel git 

     创建用户:

    [root@Cent0S7 ]# $ groupadd git
    [root@Cent0S7 ]# $ useradd git -g git

    创建目录:

    [root@Cent0S7 ]# mkdri -p /home/git/.ssh/
    [root@Cent0S7 ]# mkdri -p /home/gitrepo/
    /home/gitrepo/是我们以后放各种项目的总仓库,新建项目就放在gitrepo目录里。

    创建文件保存客户端(Client)公钥文件:一行一个公钥

    [root@Cent0S7 ]# touch /home/git/.ssh/authorized_keys
    

    修改文件夹和文件的所属用户和访问权限

    [root@Cent0S7 ]#  chmod 755  /home/git/.ssh/
    [root@Cent0S7 ]#  chmod 644  /home/git/.ssh/authorized_keys
    [root@Cent0S7 ]#  chown git:git /home/gitrepo/
    

    在服务器上新建一个项目,用来测试,以上命令Git 创建一个空仓库,服务器上的Git仓库通常都以.git结尾。然后,把仓库所属用户改为git

    [root@Cent0S7 ~]# cd /home/gitrepo/
    [root@Cent0S7 gitrepo]# git init --bare hello.git
    Initialized empty Git repository in /home/gitrepo/hello.git/
    [root@Cent0S7 gitrepo]# chown -R git:git hello.git
    

      PS:提示创建了一个空仓库,新仓库肯定是没有东西,等会客户机提交就有东西了,

           PS:注意这里的东西不要随意修改,会造成git错误的。

     为了安全,禁止用户git登录shell,修改/etc/passwd文件

    [root@Cent0S7 g~]# sed -i 's/git:/bin/bash/git:/bin/git-shell/g' /etc/passwd

    在客户端远程SSH连接服务器创建项目仓库,看root@后面的主机名从客户机Centos7-2,连接到了CentOS7服务器里了。

    # ssh name (name:远程服务器地址)
    [root@CentOs7-2 ~]# ssh 192.168.200.132
    root@192.168.200.132's password: 
    Last login: Wed Dec  2 09:55:36 2020 from 192.168.200.1
    [root@Cent0S7 ~]# 
    

     创建新项目test

    # git init --bare name (name:仓库的名称)
    [root@Cent0S7 ~]# cd /home/gitrepo/
    [root@Cent0S7 gitrepo]# git init --bare test.git
    初始化空的 Git 版本库于 /home/gitrepo/test.git/
    #exit 退出
    [root@Cent0S7 gitrepo]# exit
    #回到本地
    [root@Cent0S7-2 ~]# 
    

      PS:git init 是创建私有仓库;git init是创建公共仓库。

    客户端:

    安装git

    [root@Cent0S7 ~]# yum -y  install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel git 

    克隆仓库要访问服务器,可以用证书的方式来登录去克隆,告诉服务器有权限访问,在客户端创建证书,直接一路回车即可

    [root@CentOs7-2 ~]# ssh-keygen -t rsa
    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa): 
    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:6YANhNQ13h11PNUu******KsQ1TnkJtykfiN1aUZJt0 root@CentOs7-2
    The key's randomart image is:
    +---[RSA 2048]----+
    | oo**+*+ooo.....o|
    |  +o**o=o=E...= .|
    | . o.Bo.+ .. . = |
    |  + *+. ...   . .|
    |   +. o.S.     . |
    |       o.        |
    |        .        |
    |                 |
    |                 |
    +----[SHA256]-----+
    

    证书创建完成。保存在用户主目录下.ssh文件下,"点" 开头的文件夹是隐藏文件夹,可以用 ls -a 可以看到。

    复制公钥证书到服务器

    将客户端root创建的证书公钥id_rsa.pub 复制到远程服务器192.168.200.132的/home/git目录下,输入服务器root的密码,因为git我们设置不能登录。

    [root@CentOs7-2 etcd315]# scp /root/.ssh/id_rsa.pub root@192.168.200.132:/home/git/
    root@192.168.200.132's password: 
    

       PS:注意主目录路径,root的主目录在/root/  普通用户在/home/

    现在离开客户机client 回到服务器里server,将客户机的公钥添加到git里

    [root@Cent0S7 ]# cat /home/git/id_rsa.pub >> /home/git/.ssh/authorized_keys
    

      

    到此,服务器和客户机互访已经建立,来克隆上面建好的项目仓库 hello,从服务器上克隆一个仓库到本地,本地目录随意,这里选择用户主目录

    [root@Cent0S7 ~]# git clone git@192.168.200.132:/home/gitrepo/hello
    Cloning into 'hello'...
    warning: You appear to have cloned an empty repository.
    git@192.168.200.132 git是你git服务器上的用户,192.168.200.132填你服务器IP或域名
    warning: You appear to have cloned an empty repository. 提示克隆一个空仓库,首次克隆没有文件,正常,忽略
    /home/gitrepo/hello 是的你没看错,我也没少写,服务器创建是hello.git 这里就写hello就可以

    这样看看你的主目录里是不是多了个hello的文件夹,进入hello 并创建 hellogit.txt文件,内容是hello git
    [root@Cent0S7 ~]# ll
    drwxr-xr-x  3 root root      18 Dec  1 23:15 hello/
    [root@Cent0S7 ~]# cd hello
    [root@Cent0S7 hello]# echo "hello git" >>hellogit.txt
    

      将新文件,用add先提交到“暂存区(缓存区)”,用git commit 提交到本地仓库,-m 'first file' 是备注,备注内容是 first file ,备注内容随意写

    [root@Cent0S7 hello]# git add .
    [root@Cent0S7 hello]# git commit -m 'first file'
    [master (root-commit) 2cd8575] first file
     1 file changed, 1 insertion(+)
     create mode 100644 hellogit.txt
    

      看看“暂存区(缓存区)”状态,提示“没有需要提交的,目录很干净”

    [root@Cent0S7 hello]# git status
    # On branch master
    nothing to commit, working directory clean
    

      提交本地文件到远程git服务器,将本地master分支推送到origin主机的master分支上

    [root@Cent0S7 hello]# git push origin master
    Counting objects: 3, done.
    Writing objects: 100% (3/3), 207 bytes | 0 bytes/s, done.
    Total 3 (delta 0), reused 0 (delta 0)
    To git@192.168.200.132:/home/gitrepo/hello
     * [new branch]      master -> master
    

      从服务器取回最新数据,注意,第一次向服务器取数据是克隆git clone ,以后取数据是更新 git pull

    [root@Cent0S7 hello]# git pull origin master
    From 192.168.200.132:/home/gitrepo/hello
     * branch            master     -> FETCH_HEAD
    Already up-to-date.
    

      好了,数据提交成功,也可以取回,只不过现在是实验,文件没变动,如果项目里有很多人,取回来时会提示文件变化内容。

    常用的Git命令

    git init  //初始化本地git仓库
    git add<file>  //添加文件
    git add *.html  //添加一类文件
    git add .  //添加所有文件
    git status //查看状态
    git commit //提交
    git commit -m ''提交并注释 (这样就可以不用进入到编辑页面了)
    git push  //从远程仓库考取数据
    git clone  //从远程仓库拷贝数据
    git config --global user.name'张宜成'  配置用户名
    git config -- global user.email 'chengchengzhang123@qq.com' 配置邮箱
    git rm --cached 文件名  //从add工作区中删除
    git branch 分支名 (创建分支)
    git checkout 分支名 (切换到你的分支中)
    git checkout master(切换到主分支)
    git merge 分支名(在master主分支下使用 将分支合并到主分支)
    git remote 查看连接
    git remote add origin 链接  (创建链接)
    git remote remove 名称(删除remote)
    git clone 将服务器端的项目克隆岛本地仓库
    touch .gitignore 忽略需要上传的文件(将要忽略的文件放到该文件夹)
    touch.gitignore 忽略文件(/文件名)
    README.md文档 git对项目进行描述
    $ git push origin master //推送到远程仓库
    

      

      

     参考文章:

    https://www.runoob.com/git/git-server.html

    http://cnblogs.com/shijiaoyun/p/9663396.html

    http://developer.51cto.com/art/201507/483448.htm

    http://git-scm.com/book/zh/v2/服务器上的-Git-在服务器上搭建-Git

    http://blog.csdn.net/qq_33759448/article/details/82984450

  • 相关阅读:
    xlrd doc
    安装Python package
    Python处理Excel(转载)
    OPENSSL简介
    sublime text2教程
    使用SQL 从表中取记录
    SQL基础
    shell脚本之grep的使用方法
    (转载)(收藏)OceanBase深度解析
    (转载)线程池的使用
  • 原文地址:https://www.cnblogs.com/wutou/p/14071563.html
Copyright © 2011-2022 走看看