流程:

  1. 在linux上安装git
  2. 在linux中新建一个git用户
  3. 创建ssh证书
  4. 导入ssh证书
  5. 选定一个空目录作为代码仓库
  6. 更改git命令的所有者
  7. 禁止ssh登录服务器

在linux上安装git

使用命令:yum install git

创建一个git用户

输入:adduser git 或者 useradd git

然后输入id git 查看是否成功

 

创建ssh证书

*这里创建ssh证书,是在本地创建,而不是在服务器端创建的。这个需要注意。以win为例,需要先安装git客服端,之后打开git命令面板,

1、先配置用户名和邮箱:(如果已经配置了则跳过)
  git config --global user.name '你的名称'
  git config --global user.email '你的邮箱地址'

2、然后生成公钥和私钥

 ssh-keygen -t rsa -C "你的邮箱地址"

回车后会看到以下界面。这里是选择ssh文件保存的路径(直接回车),我选择默认路径,即最后一行显示的、 保存在了c盘的这个位置中(c/user/wei/.ssh/id_rsa)。

紧接着会让你输入密码(会有2次确认,简短的英语很好理解的)。默认保持不输入(也是直接回车,避免每次clone代码都需要输入密码的麻烦)

成功执行后会看到这样的界面:

然后我们到刚才提示的c盘的目录中,找到我们需要的ssh文件。(之前一直找不到对应的文件夹,是因为C盘中的user的中文别名叫“用户”,所以C:/user/  其实就是 C:/用户/)。打开文件夹后,里面会有2份文件,分别为公钥和私钥。


当然可以使用文本编辑器打开ssh公钥查看里面的内容(不推荐使用记事本直接打开,原因不多作解释)

这时候我们就需要把id_rsa.pub文件放到服务器上,准备下一步的工作。这里我使用的是Winscp工具。把文件放在服务器任意地方都可以,只要自己找得到就行。

导入自己的SSH证书

以上生成SSH证书的步骤弄懂后,就需要把自己的ssh公钥证书导入服务器的公钥管理的地方

在服务器中找到  /home/git/.ssh/authorized_keys    文件、如果没有就自己创建一个authorized_keys文件,注意路径不要弄错。

找到刚才放在服务器上的 id_rsa.pub文件。然后使用命令:cat id_rsa.pub >> authorized_keys

注意路径不要弄错,因为我这是在 /home/git/.ssh/ 的目录中了

(如果linux命令用的不熟的,其实可以直接使用winscp编辑文件,把id_rsa.pub文件的内容复制粘贴到authorized_keys文件中。方法不是唯一)

最重要的一点,也是最容易出错的一点!linux一切皆文件,所以要给对应的文件设置好组和权限!!

*注:这里的/home/git/下的文件所有者必须都为git  .ssh的权限为最少700或者755  authorized_keys权限最少为600

创建一个仓库目录

选定一个空目录作为仓库  这里选择  /srv 目录。切换到 /srv 目录下

执行命令 :  git init --bare test.git   (这样就创建了一个名叫 “test”的git仓库了),bare的意思是光秃秃的,也就是空的

更改仓库所有者和权限

chown -R git:git test.git  (“因为我的仓库名称叫test,所以可以根据自己的仓库名称进行修改”,仓库的所有者应该为git。 )即步骤2创建的git用户

当然/srv文件也需要修改为git用户才行chown -R git:git /srv。不然push的时候会报错没权限。。。。。

禁止linux使用git的ssh登录

这里禁止的意义是为了服务器的安全性,避免从git的ssh可以直接登录服务器

编辑/etc/passwd文件
找到 : git:x:1001:1001:,,,:/home/git:/bin/bash

改为 : git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell

*可能用到的命令:修改ssh后,有可能需要重启ssh才能生效
SSH重启: service sshd restart
SSH启动: service sshd start


以上步骤都完成后,就可以试下把服务器的代码clone下来了。由于是在自己服务器搭建的git环境,所以git文件的url如下:

git clone git@ip地址:/srv/test.git

ip地址很好理解,而url后面的  /srv/test.git  则是git文件在服务器上的位置,如果仓库在其他目录,那么/srv/test.git换成你对应的文件目录即可。

确认项目路径没错后,回车进行clone。这时候会看到一句警告

warning: You appear to have cloned an empty repository.

不要慌张,其实仔细一看,只是git提示我们:你似乎clone了一个空的项目。打开克隆下来的文件夹,查看是否有一个.git的隐藏文件。如果有的话,那就是已经clone成功,那也代表着linux搭建自己的git服务成功了!

后续的一些错误:

如果创建初始仓库时没有使用--bare参数,那么在push的时候可能会报错:

在使用Git Push代码到数据仓库时,提示如下错误:

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

错误:

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@192.168.1.X:/var/git.server/.../web

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

error: failed to push some refs to 'git@192.168.1.X:/var/git.server/.../web'

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

[receive]
denyCurrentBranch = ignore

无法查看push后的git中文件的原因与解决方法

所以在初始化远程仓库时最好使用

git --bare init

而不要使用:git init

git init 和git --bare init 的具体区别:

如果使用了git init初始化,则远程仓库的目录下,也包含work tree,当本地仓库向远程仓库push时, 如果远程仓库正在push的分支上(如果当时不在push的分支,就没有问题), 那么push后的结果不会反应在work tree上,  也即在远程仓库的目录下对应的文件还是之前的内容。

此时需要 git reset --hard 后才能看到push后的内容.

不知道问题所在,只找到以下方法:

登录到远程文件夹,使用

git config --bool core.bare true

就可以使用了,下面是找到的信息:

Create a bare GIT repository

A small rant: git is unable to create a normal bare repository by itself. Stupid git indeed.

To be precise, it is not possible to clone empty repositories. So an empty repository is a useless repository. Indeed, you normally create an empty repository and immediately fill it:

git init git add .

However, git add is not possible when you create a bare repository:

git --bare init git add .

gives an error "fatal: This operation must be run in a work tree".

You can't check it out either:

Initialized empty Git repository in /home/user/myrepos/.git/ fatal: http://repository.example.org/projects/myrepos.git/info/refs not found: did you run git update-server-info on the server? git --bare init git update-server-info # this creates the info/refs file chown -R <user>:<group> . # make sure others can update the repository

The solution is to create another repository elsewhere, add a file in that repository and, push it to the bare repository.

mkdir temp; cd temp git init touch .gitignore git add .gitignore git commit -m "Initial commit" git push <url or path of bare repository> master cd ..; rm -rf temp

Logo

更多推荐