首页 > 技术文章 > 搭建git服务器

catinsky 2018-06-04 16:13 原文

自己的服务器到期,转移自己博客内容至此。

环境:
服务器:Cent OS 7.3  10.8.8.50
客户端01:Cent OS 7.3 10.8.8.51
客户端02: Windows 10

一、安装git、创建git用户,禁止git用户直接登陆

[root@git ~]# yum install git -y
Loaded plugins: fastestmirror
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast


base | 3.6 kB 00:00:00 
extras | 3.4 kB 00:00:00 
updates | 3.4 kB 00:00:00                                           
(1/2): extras/7/x86_64/primary_db | 145 kB 00:00:00 
(2/2): updates/7/x86_64/primary_db | 4.5 MB 00:00:02 
Determining fastest mirrors
* base: centos.ustc.edu.cn
* extras: centos.ustc.edu.cn
* updates: centos.ustc.edu.cn
Package git-1.8.3.1-12.el7_4.x86_64 already installed and latest version
Nothing to do
[root@git ~]# git --version
git version 1.8.3.1
#默认已经安装,git版本为1.8.3.1

[root@git ~]# adduser --system --shell /bin/sh --create-home --home-dir /home/git git
#创建git用户,禁止登陆。
[root@git ~]# su git
sh-4.2$ mkdir /home/git/gitrepo
sh-4.2$ cd /home/git/gitrepo
sh-4.2$ git init --bare liuanhuaming.git
Initialized empty Git repository in /home/git/gitrepo/liuanhuaming.git/
sh-4.2$ chmod 700 liuanhuaming.git/
#创建仓库目录,创建liuanhuaming.git仓库,并更改权限。
sh-4.2$ mkdir /home/git/.ssh
sh-4.2$ touch /home/git/.ssh/authorized_keys
#创建存放客户端ssh公钥文件目录、公钥文件。

二、客户端创建公钥、并传送至服务器指定目录

[root@gitclient ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory ‘/root/.ssh’.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
4c:35:86:e6:6c:7c:fa:99:fb:88:a7:82:0e:a8:ec:a9 root@gitclient
The key’s randomart image is:
[root@gitclient ~]# scp -P 958 /root/.ssh/id_rsa.pub root@10.8.8.50:/home/git/.ssh/

三、服务器端授权

[root@git gitrepo]# cd /home/git/.ssh/
[root@git .ssh]# mv id_rsa.pub gitclient01.pub
[root@git .ssh]# cat gitclient01.pub >> authorized_keys

四、客户端克隆项目,并推送文件

[root@gitclient wwwroot]# git clone ssh://git@10.8.8.50:61958/home/git/gitrepo/liuanhuaming.git
Cloning into ‘liuanhuaming’…
warning: You appear to have cloned an empty repository.
[root@gitclient wwwroot]# cd liuanhuaming/
[root@gitclient liuanhuaming]# touch 1.txt
[root@gitclient liuanhuaming]# git add 1.txt
[root@gitclient liuanhuaming]# git commit -m “add 1.txt”

*** Please tell me who you are.

Run

git config --global user.email “you@example.com”
git config --global user.name “Your Name”

to set your account’s default identity.
Omit –global to set the identity only in this repository.

fatal: unable to auto-detect email address (got ‘root@gitclient.(none)’)
[root@gitclient liuanhuaming]# git config --global user.email “client01@gmail.com”
[root@gitclient liuanhuaming]# git config --global user.name “client01”
[root@gitclient liuanhuaming]# git commit -m “add 1.txt”
[master (root-commit) a529a32] add 1.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 1.txt
[root@gitclient liuanhuaming]# git push

warning: push.default is unset; its implicit value is changing in
Git 2.0 from ‘matching’ to ‘simple’. To squelch this message
and maintain the current behavior after the default changes, use:

git config –global push.default matching

To squelch this message and adopt the new behavior now, use:

git config –global push.default simple

See ‘git help config’ and search for ‘push.defaultfor further information.
(the ‘simple’ mode was introduced in Git 1.7.11. Use the similar mode
‘current’ instead of ‘simple’ if you sometimes use older versions of Git)

No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as ‘master’.
fatal: The remote end hung up unexpectedly
error: failed to push some refs to ‘ssh://git@10.8.8.50:958/home/git/gitrepo/liuanhuaming.git’
[root@gitclient liuanhuaming]# git config --global push.default simple
[root@gitclient liuanhuaming]# git push
Counting objects: 3, done.
Writing objects: 100% (3/3), 201 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://git@10.8.8.50:958/home/git/gitrepo/liuanhuaming.git
* [new branch] master -> master
#客户端克隆了liuanhuaming.git,并推送了1.txt文件。

五、windows客户端

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

推荐阅读