linux搭建git服务器并使用

Linux服务器配置之Git服务器搭建步骤:

一、配置环境

1、服务器:lCentOS 7.2(64位) + Git (version 1.8.3.1)
2、客户端:Windows 10 Pro(64位) + Git(version 2.11.0.windows.1)
3、服务器连接软件:Xshell 6

二、配置步骤

1、安装git 服务端
Linux作为服务器端系统,Windows作为客户端系统,分别安装Git
服务器端

[admin@ceontOS ~]$ su root                  #切换到root用户名

Password:                                   #输入root用户的密码

[root@ceontOS ~]# yum install -y git        #执行该命令进行Git安装

 

安装完后,查看Git版本

[root@ceontOS ~]# git --version

git version 1.8.3.1

   

客户端:
下载 Git for Windows,地址:https://git-for-windows.github.io/
安装完之后,可以使用 Git Bash 作为命令行客户端。
安装完之后,查看 Git 版本

$ git --version

git version 2.11.0.windows.1

 

Git客户端安装具体可参考:Git安装及SSH Key管理之Windows篇

 

2、服务器端创建 git 用户,用来管理 Git 服务,并为 git 用户设置密码

 

[root@ceontOS ~]# cd /home                                  #进入/home/目录

[root@ceontOS home]# id git                                 #查看git用户是否存在

id: git: no such user                                       #提示git用户不存在

[root@ceontOS home]# useradd git   #创建git用户 (名字任意取)

3、服务器端创建 Git 仓库
设置 mkdir -p home/git/fenlin88l.git 为 Git 仓库

注:git  fenlin88l.git是自建目录  fenlin88l.git为仓库名

[root@ceontOS home]# cd git   #进入git用户目录
[root@ceontOS git]# ls      #查看git用户目录下有哪些目录/文件
 fenlin88l.git 

[root@ceontOS git ]# git init  ./fenlin88l.git    #这步很重要,初始化项目测试目录 或者 cd fenlin88l.git 然后 git init

   git init 会有工作区目录

   git --bare init 只有裸仓库,没有实际代码,只记录提交了那些文件


Initialized empty Git repository in /home/git/fenlin88l.git/

[root@ceontOS git]#  ll      #查看fenlin88l.git项目文件夹的拥有者
total 4
drwxr-xr-x 3 root root 4096 Jan 13 13:08 fenlin88l.git    #拥有者是root用户名


[root@ceontOS git]# chown -R git:git fenlin88l.git/    #将拥有者改为git用户

        
[root@ceontOS git]# ll      #再次查看fenlin88l.git项目文件夹的拥有者
  #拥有者是git用户 如下

      

 注:

如果使用 git init初始化:

      

(1:在使用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 inconsisten

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:/home/git/fenlin88l.git/'

 

 

错误原因以及解决

 

这是由于git默认拒绝了push操作,需要进行设置,修改服务端的.git/config文件后面添加如下代码:(# 当前git项目下)

[receive]

denyCurrentBranch = ignore

(2:Linux Git代码提交自动更新到web目录

在git远端仓库的hooks目录下新建post-receive文件,输入如下内容:

#!/bin/sh

unset GIT_DIR

DeployPath="/home/git/fenlin88l.git/"  #web目录

cd $DeployPath

git reset --hard

echo  $DeployPath

保存后赋予可执行权限: chmod +x post-receive

 当客户端进行git push 操作时,代码将自动更新到web目录

4、客户端 clone 远程仓库

先在本地Windows系统上创建一个存放git项目的文件夹,例如我的设置在:【D:\git\fenlin88】

  

此处通过Git Bash来创建的,当然也可以手动创建该目录,然后进入该目录,启动Git Bash

 #在桌面打开的git bash


$ cd d:   #进入D盘

    
$ mkdir git  #创建git文件夹
   
$ cd git   / #进入git文件夹创建fenlin88文件夹

  
$ git clone  git@服务器公网IP地址:/home/git/fenlin88l.git    #IP地址后面跟冒号,冒号后面是刚才初始化的项目文件夹的绝对路径

![](http://upload-images.jianshu.io/upload_images/2267589-7bedba534bdc9f04.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

例:  git clone git@47.107.193.147:/home/git/fenlin88l.git

 

当第一次连接到目标 Git 服务器时会得到一个提示:

The authenticity of host '118.178.142.77 (118.178.142.77)' can't be established.
ECDSA key fingerprint is SHA256:JwC9NxLIjBGqtLC2NUk8MulSc3XH3mM5AWMcFz0a5/8.
Are you sure you want to continue connecting (yes/no)? yes

选择 yes:

Warning: Permanently added '118.178.142.77' (ECDSA) to the list of known hosts.

   

 

此时 C:\Users\用户名\.ssh 下会多出一个文件 known_hosts,以后在这台电脑上再次连接目标 Git 服务器时不会再提示上面的语句。

【说明】如果你的服务器没有配置SSH连接,那么按照正常情况会让你输入git用户的密码,输入正确后就能进行项目克隆了。

![](http://upload-images.jianshu.io/upload_images/2267589-776f44a088bba653.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值