搭建Git私有仓库

在自己电脑上搭建Git私有服务器
一开始肯定是百度,因为整个问题维度很低,大概一篇文章就够用,筛选后应该就是这篇: git 服务器搭建,在自己服务器上搭建私有仓库

步骤开始:

  1. 在服务器上(我用的是Ubuntu)安装git,简单,不说了。

  2. 在客户机上(Win10)安装Git,简单,不说了。

  3. 在服务器上添加一个用户,一般都叫git,这个不重要。

     # useradd git -d /home/git -m -s /bin/bash
     -d:指定用户目录
     -m:如果目录不存在则创建
     -s:可以指定用户使用的命令
    
  4. 创建git仓库

     $ su git
     $ cd ~
     $ git init --bare myserver.git
     --bare选项(bare汉语意思是:裸,裸的)初始化的版本库(暂且称为bare repository)只会生成一类文件:用于记录版本库历史记录的。
    

摘自git init 与 git init --bare 的区别,大概的意思就是生成了一些初始化的系统配置文件。

  1. 服务端设置从现在看就差不多了,一会儿还有别的,先配置客户机

  2. 生成密钥对

     ssh-keygen -t rsa   // 会在 ~/.ssh/,生成 'id_rsa' 和 'id_rsa.pub' 2个文件 
    

生成这俩东西的作用仅仅是以后免密登录,摘自ssh-kengen的使用说明

  1. id_rsa.pub传到服务器上,执行cat id_rsa.pub >> ~/.ssh/authorized_keys,此语句作用大家都懂。

  2. 命令行下输入git clone git@IP:/home/git/myserver.git,常用命令如下,含义顾名思义。

     $ git add .
     $ git commit -m "first commit"
     $ git push
    
  3. 这样会报一个错:

     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 t
     remote: error: 'ignore' or 'warn' in the remote repository to allow pushing int
     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 som
     remote: error: other way.
     remote: error:
     remote: error: To squelch this message and still keep the default behaviour, se
     remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
     To root@101.200.159.138:/data0/htdocs/www/test/study/.git
      ! [remote rejected] master -> master (branch is currently checked out)
     ...
    

解决方法参照《git push错误 remote: error: refusing to update checked out branch: refs/heads/master解决方式》:

    这是由于git默认拒绝了push操作,需要进行设置,修改当前仓库目录下.git/config文件后面添加如下代码:

    [receive]

    denyCurrentBranch = ignore

    重新git push即可
  1. 其实就能正常用了,在WebStorm中配置是这样的:

    • VCS > Checkout from Version Control > Git
    • URL填入ssh://git@IP:PORT/home/git/myserver.git,OK。
  2. 我把本地目录传上去是个很绕的过程,先将WebStorm生成的工程备份(因为会重名),myserver改名myserver2,在WebStrom当前工程目录clone服务器上的仓库,生成myserver目录,然后将myserver2中的文件复制到myserver目录中,然后执行addcommitpush操作,myserver2就可以删掉了,这样讲需要的东西全部传到服务器上,然后删除myserver,因为一会儿在WebStorm中clone时会路径冲突,最后在WebStorm中引入仓库路径就结束了。

  3. 安全问题:

    为安全考虑Git账号只允许使用git-shell。在passwd文件中找到git用户,把/bin/bash直接修改成/usr/bin/git-shell 登录root账号,并修改git的用户权限。

    $ su
    # vim /etc/passwd
    

    这样git用户只能git-shell命令不能登录了。

    使用 su git 命令就会出现下面提示,git用户就无法登录到shell,这样就OK了。

    # su git
    fatal: Interactive git shell is not enabled.
    hint: ~/git-shell-commands should exist and have read and execute access.
    

搭建私有Git仓库过程大概就是这个样子,以后在使用过程中发现什么问题,再说。##

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容