• Linux Ubuntu搭建git服务器


    1. 安装 openssh-server ,用于创建SSH服务。

    sudo apt-get install openssl-server

    使用命令ps -e|grep ssh,查看ssh服务是否启动。

    如果正常启动,则会显示类似信息:1966 ?    00:00:00 ssh-agent 

    2. 创建用户名为git的用户,用来管理和运行git服务。

    sudo user del -r git // 删除已经存在的叫git的用户;
    sudo adducer git // 添加用户名叫git的用户;

    进入git用户目录下,创建目录.ssh,然后在.ssh目录下创建名为authorized_keys的文件;

    cd /home/git
    sudo mkdir .ssh
    cd .ssh
    touch authorized_keys

    将客户端的公钥(生成方法见后文)拷贝到authorized_keys文件中;

    cat path/id_rsa.pub >> /home/git/.ssh/authorized_keys

    安装 git-core

    sudo apt-get install git-core

    3. 初始化服务端仓库

    git —bare init /home/git/test1.git

    注:该命令会生成一个空仓库,此时test1.git对应的地址则为git@hostIp:/home/git/test1.git

    或者:

    mkdir test1.git
    cd test1.git
    git —bare init

    4. 客户端运行ssh-keygen -t rsa生成密钥;

    生成密钥后,在.ssh目录下,会有两个文件id_rsa和id_rsa.pub文件,id_rsa.pub为公钥,通过命令scp /home/git/.ssh/id_rsa.pub gitServer:/home/git将client上生成的公钥拷贝到gitServer上;

    服务端把公钥添加到authorized_keys文件中后,客户端就能通过路径访问到git仓库了;

    生成密钥时,会要求你确认保存公钥的位置(默认为~/.ssh/id_rsa),然后会让重复一个密码两次,如果不想在使用公钥的时候输入密码,则留空;

    假定hostIp为192.168.1.100

    git clone git@192.168.1.100:/home/git/test1.git

     

    5. 常用git命令

    >1. git clone

      语法:git clone 版本库网址 本地库名称(可省略)

    >2. git remote

      此命令用于管理远程主机名,在没有参数的情况下可以列出所有主机名;

      git remote
      origin

      显示origin是在使用clone命令,克隆远程版本库时git自动为远程主机命令;

      通过git remote -v 查看版本库的地址;

    >3. git fetch

      语法:git fetch origin(git fetch origin master)

      默认情况下,git fetch origin将会更新远程主机origin上所有分支,如果只想更新某个分支则在主机名后加分支名

    >4. git push

      语法:git push 远程主机名 本地分支名:远程分支名

      如果省略远程分支名,则表示将本地分支推送与存在最终关系的远程分支,如果远程分支不存在,则会被创建;

      git push origin master,表示将本地master分支推送到origin主机的master分支;

      如果省略本地分支名,则表示要删除远程主机中的分支,如git push origin : master,则表示删除origin主机中的master分支;

    >5. git pull

      语法:git pull 远程主机 远程分支:本地分支 如:git pull origin master:master,表示将远程主机origin中的master分支更新到本地分支master;

     

    6. 每次添加一个新项目都需要通过shell登入主机并创建一个纯仓库,我们不妨以gitServer作为git用户和仓库所在的主机名,如果你在网络内部运行该主机,并且在DNS中设定gitServer指向该主机,则以下这些命令都是可用的:

    cd myproject
    git init
    git add .
    git commit -m “initial commit”
    git remote add origin git@gitServer:test1.git
    git push origin master

    创建一个版本库仓库

    这样,其它人的克隆和推送也一样

    git clone git@gitServer:/test1.git
    vim README
    git commit -am ‘fix for the README file’
    git push origin master

    7. 作为一个额外的防护措施,可以用git自带的git-shell工具来把git用户的活动限制在仅与git相关。把它设为git用户登入的shell,那么该用户就不能拥有主机正常的shell访问权,为了实现这一点,需要指明用户的登入shell为git-shell,而不是 bash 或者 csh,可以通过编辑/etc/passwd文件

    sudo vim /etc/passwd

    在文件末尾,找到类似此行

    git:x:1000:1000::/home/git:/bin/sh

    将bin/sh改为/usr/bin/git-shell

    git:x:1000:1000::/home/git:/usr/bin/git-shell

    现在git用户只能用ssh连接来推送和获取git仓库,而不能直接使用主机shell。

     

    8. Q&A

    (1)

    Q:

    xiongmc@xiongmc-desktop:~/myproject2$ git push origin master

    Agent admitted failure to sign using the key.

    git@localhost's password: 

    error: src refspec master does not match any.

    error: failed to push some refs to 'git@localhost:/opt/git/project.git/'

    A:如果初始的代码仓库为空,git push origin master提交代码的时候会出现以上异常

    http://www.linuxidc.com/Linux/2013-03/81022.htm 

    (2)

    Q:

    xiongmc@xiongmc-desktop:~/myproject2$ git push origin master

    Agent admitted failure to sign using the key.

    git@localhost's password: 

    Permission denied, please try again.

    git@localhost's password: 

    Counting objects: 3, done.

    Writing objects: 100% (3/3), 213 bytes, done.

    Total 3 (delta 0), reused 0 (delta 0)

    error: insufficient permission for adding an object to repository database ./objects

    fatal: failed to write object

    error: unpack failed: unpack-objects abnormal exit

    To git@localhost:/opt/git/project.git/

     ! [remote rejected] master -> master (n/a (unpacker error))

    error: failed to push some refs to 'git@localhost:/opt/git/project.git/'

    A: 服务器无权限。

    http://blog.sina.com.cn/s/blog_53e449530101349s.html

    http://stackoverflow.com/questions/1918524/error-pushing-to-github-insufficient-permission-for-adding-an-object-to-reposi 

    sudo chown -R git:gitgroup path/to/repo.git/
    // or
    sudo chown -R git:git path/to/repo.git/

    (3)

    http://www.linuxidc.com/Linux/2013-03/81022.htm

    Q:

    xiongmc@xiongmc-desktop:~/myproject2$ git push origin master

    Agent admitted failure to sign using the key.

    git@localhost's password: 

    Counting objects: 3, done.

    Writing objects: 100% (3/3), 213 bytes, done.

    Total 3 (delta 0), reused 0 (delta 0)

    remote: error: refusing to update checked out branch: refs/heads/master

    remote: error: By default, updating the current branch in a non-bare repository

    remote: error: is denied, because it will make the index and work tree inconsistent

    remote: error: with what you pushed, and will require 'git reset --hard' to match

    remote: error: the work tree to HEAD.

    remote: error: 

    remote: error: You can set 'receive.denyCurrentBranch' configuration variable to

    remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into

    remote: error: its current branch; however, this is not recommended unless you

    remote: error: arranged to update its work tree to match what you pushed in some

    remote: error: other way.

    remote: error: 

    remote: error: To squelch this message and still keep the default behaviour, set

    remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.

    To git@localhost:/opt/git/project.git/

     ! [remote rejected] master -> master (branch is currently checked out)

    error: failed to push some refs to 'git@localhost:/opt/git/project.git/'

    A:

    $cd .git

    $vim config

    该配置文件的原始内容为:

    [core]

            repositoryformatversion = 0

            filemode = true

            bare = false

            logallrefupdates = true

    在该配置文件中加入以下内容:

    [receive]

    denyCurrentBranch = ignore

    (4)

    Linux服务器:Ubuntu配置git服务 - 逸云沙鸥

    http://www.xue5.com/Server/Linux/667461.html

    (5)为了集成到SCM,我们在Linxu上安装GIT

    http://www.examw.com/linux/all/182529/index-2.html

    LINUX上创建GIT服务器

    http://lionest.iteye.com/blog/1447310

    http://blog.csdn.net/andy_android/article/details/6996134

    Receiving objects:  26% (5668/21560), 8.06 MiB | 183 KiB/s      21560)   

    (6)

    Q:

    xiongmc@xiongmc-desktop:~/myproject.git$ git push origin master ssh: connect to host xiongmc-desktop port 22: Connection refused

    fatal: The remote end hung up unexpectedly

    xiongmc@xiongmc-desktop:~/myproject.git$ git push origin master 

    ssh: connect to host xiongmc-desktop port 22: Connection refused

    fatal: The remote end hung up unexpectedly

    A:

    http://blog.csdn.net/zlm_250/article/details/7979221

    sudo apt-get install openssh-server

    sudo net start sshd  

    sudo ufw disable 

    ssh localhost  

    (7)

    Q:

    ubuntu系统下关于'xx'用户不在 sudoers文件中,此事将被报告。的解决方法 

    A:

    http://blog.sina.com.cn/s/blog_bede36550101b0av.html

    git ALL=(ALL:ALL) ALL 

    (8)

    Q:

    xiongmc@xiongmc-desktop:~/myproject.git$ git push origin master 

    git@xiongmc-desktop's password: 

    fatal: '/opt/git/project.git' does not appear to be a git repository

    fatal: The remote end hung up unexpectedly

    A:

    http://www.dotkam.com/2010/08/22/gitolite-does-not-appear-to-be-a-git-repository/

     

  • 相关阅读:
    【一句日历】2020年4月
    【2020-03-31】思维永远只有一种
    【2020-03-30】事情就怕太容易而看不起
    【2020-03-29】人生有趣的地方在于不断升级自己
    【2020-03-27】球场还在,换了地方而已
    【2020-03-26】人其实是一个系统
    【2020-03-25】没工作是个伪命题
    day 74 vue 2 axios数据请求 以及组件的学习
    day 73 初学vue (1)
    day 72 crm(9) 客户关系系统,整体实现,以及自定制路由内容,客户关系梳理
  • 原文地址:https://www.cnblogs.com/a284628487/p/3729246.html
Copyright © 2020-2023  润新知