准备工作:以root用户登陆自己的Linux服务器。
第一步安装依赖库
[root@localhost ~]# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel [root@localhost ~]# yum install gcc perl-ExtUtils-MakeMaker
第二步卸载旧版git
加入原先有用yum安装过git,则需要先卸载一下
[root@localhost ~]# yum remove git
第三步下载源码
下载git-2.10.0.tar.gz 到 /usr/local/src
(查找git版本可以到https://www.kernel.org/pub/software/scm/git/下查看git的版本号自行选择下载)
查看版本方法:
[root@iZbp1ap7v4yegqdgzrh7cuZ ~]# wget -v https://www.kernel.org/pub/software/scm/git/
[root@iZbp1ap7v4yegqdgzrh7cuZ ~]# vi index.html
复制想下载的版本 --> Esc --> :q! --> 回车!
这里我选择下载git-2.10.0.tar.gz
[root@localhost ~]# cd /usr/local/src [root@localhost ~]# wget https://www.kernel.org/pub/software/scm/git/git-2.10.0.tar.gz
第四步解压、编译和安装
[root@localhost ~]# tar -zvxf git-2.10.0.tar.gz [root@localhost ~]# cd git-2.10.0 [root@localhost ~]# make prefix=/usr/local/git all [root@localhost ~]# make prefix=/usr/local/git install
报错处理:
make prefix=/usr/local/git all编译的时候出现了错误:
LINK git-credential-store
libgit.a(utf8.o): In function `reencode_string_iconv':
/usr/src/git-2.8.3/utf8.c:463: undefined reference to `libiconv'
libgit.a(utf8.o): In function `reencode_string_len':
/usr/src/git-2.8.3/utf8.c:502: undefined reference to `libiconv_open'
/usr/src/git-2.8.3/utf8.c:521: undefined reference to `libiconv_close'
/usr/src/git-2.8.3/utf8.c:515: undefined reference to `libiconv_open'
collect2: ld returned 1 exit status
make: *** [git-credential-store] Error 1
解决如下:
一.安装libiconv
1、# cd /usr/local/src
2、# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
3、# tar -zxvf libiconv-1.14.tar.gz
4、# cd libiconv-1.14
5、# ./configure --prefix=/usr/local/libiconv && make && make install
二.创建一个软链接到/usr/lib
1、# ln -s /usr/local/lib/libiconv.so /usr/lib
2、# ln -s /usr/local/lib/libiconv.so.2 /usr/lib
三.然后回到git目录继续编译
1、# cd /usr/local/scr/git-2.10.0
2、# make configure
3、# ./configure --prefix=/usr/local --with-iconv=/usr/local/libiconv
4、# make
5、# make install
第五步将git目录加入PATH
[root@localhost ~]# echo 'export PATH=$PATH:/usr/local/git/bin' >> /etc/bashrc [root@localhost ~]# source /etc/bashrc
安装成功后就可以查看到git版本了。
[root@localhost ~]# git --version git version 2.10.0
第六步创建git账号并设置密码
[root@localhost ~]# useradd -m git [root@localhost ~]# passwd git Changing password for user git. New password: BAD PASSWORD: is too simple Retype new password: passwd: all authentication tokens updated successfully.
第七步创建git仓库并初始化
[root@localhost ~]# mkdir -p /home/git/repositories/test.git [root@localhost ~]# cd /home/git/repositories/test.git [root@localhost test.git]# git --bare init Initialized empty Git repository in /home/git/repositories/test.git/
第八步给git仓库目录设置用户和用户组并设置权限
[root@localhost test.git]# chown -R git:git /home/git/repositories [root@localhost test.git]# chmod 755 /home/git/repositories
第九步限制git账号的ssh连接
查找git-shell所在目录
[root@localhost ~]# whereis git-shell git-shell: /usr/src/git-2.10.0/git-shell
编辑passwd文件
[root@localhost ~]# vi /etc/passwd
找到这一行
git:x:500:500::/home/git:/bin/bash
将最后的/bin/bash改为:git-shell的目录 /usr/src/git-2.10.0/git-shell 如下:
git:x:500:500::/home/git:/usr/src/git-2.10.0/git-shell
Esc --> :wq! --> 回车!
完成搭建,去克隆提交试试吧!
拉取项目报错:
[root@iZ2ze5zuc8m9608lrl3u3jZ test]# git clone git@gitee.com:wuyajing168/test.git
Cloning into 'test'...
Permission denied (publickey).
fatal: Could not read from remote repository.
解决办法:(在服务器上操作)
生成 sshkey:
ssh-keygen -t rsa -C "xxxxx@xxxxx.com"
# Generating public/private rsa key pair...
# 三次回车即可生成 ssh key
#查看你的 public key,并把他添加到码云(Gitee.com) SSH key添加地址
cat ~/.ssh/id_rsa.pub
# ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6eNtGpNGwstc....
#添加后,在终端(Terminal)中输入
ssh -T git@gitee.com
#若返回
#Welcome to Gitee.com, yourname!
#则证明添加成功。
clone地址: git ssh地址
附加:以后每次新建仓库时,只需执行上面第七、八步即可!