您的位置:首页 > 其它

Ubuntu14.04环境中搭建属于自己的Git服务器

2017-06-27 16:57 513 查看
最近要开始源码环境搭建了。没有现成的环境只好自己搭建了。基于Ubuntu14.04搭建。理论上通用其他版本的Ubuntu。后续在整理一遍关于Git服务器下搭建Android源码版本管理。 

在这里git版本管理的好处就不过多介绍了。可以自行百度。这里就更细致的讲解一下搭建的流程。方便大家更快速的搭建属于自己的Git版本管理服务器。配置完成后Ubuntu登陆界面会多一个git用户登录。觉得碍眼的话可以参考我的前一篇文章Ubuntu14.10登录界面隐藏其他用户登录窗口。话不多说,开始我们的搭建之旅。 

一、创建Git用户。 

1、安装git-core、Python-setuptools、openssh-server、openssh-client(python-setuptools主要用于安装gitosis。后两个软件主要是为了方便使用Putty直接登录到服务器)
sudo apt-get install git-core python-setuptools openssh-server openssh-client
1
1

2、创建git用户并初git用户的始化密码
sudo useradd -m git
sudo passwd git
1
2
1
2

3、建立git用户使用的目录
sudo mkdir /home/repo
sudo chown git:git /home/repo
1
2
1
2

二、安装Gitosis软件。 

1、安装Gitosis(为了统一管理,下边我会把几个重要的文件放在我建立的Tools目录中进行安装配置。)
mkdir Tools
cd Tools
git clone git://eagain.net/gitosis
(如果克隆失败用下面的地址)
git clone https://github.com/res0nat0r/gitosis.git 
cd gitosis/
sudo python setup.py install
(切换到git用户)
su git
(软链接目录)
ln -s /home/repo /home/git/repositories
1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12

2、生成管理员ssh公钥 ,并拷贝到服务器。 

(在这里我是直接使用的服务器作为管理员,使用其他服务器原理一样)
ssh-keygen -t rsa
(直接回车即可)
cp /home/XXX/.ssh/id_rsa.pub /tmp
(尽量使用git用户拷贝,防止下一步初始化时读取不了公钥。)
1
2
3
4
1
2
3
4



其他用户作为管理员可以直接用优盘拷贝公钥到服务器或者直接通过命令拷贝
scp .ssh/id_rsa.pub git@<ServerIP>:/tmp
(此方法直接是使用的git用户拷贝。ServerIP为你的Git服务器ip。)
(如:scp .ssh/id_rsa.pub git@192.168.1.102:/tmp)
1
2
3
1
2
3

3、初始化Gitosis,让你的管理员公钥生效(在Git服务器上进行)。
sudo -H -u git gitosis-init < /tmp/id_rsa.pub
1
1



三、配置Gitosis、建立第一个属于自己的Git版本库。 

1、使用管理员机器克隆Gitosis配置库(前边上传的谁的公钥谁就是管理员,后续也可以添加。)
git clone git@localhost:gitosis-admin.git
1
1



2、服务器中建立“test.git”版本库 

(GIt版本库统一在/home/git/repositories目录下,文件夹一定要以“.git”结尾)
(切换到git用户)
su git
(建立版本库“test.git”)
cd ~/repositories
mkdir test.git
(初始化test.git版本库)
cd test.git
git init --bare
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8

3、管理员机器中配置“test.git”版本库。
cd gitosis-admin/
(gitosis.conf为配置文件、keydir为公钥文件夹)
vim gitosis.conf
1
2
3
1
2
3



配置完毕提交配置信息 
(公钥的命令一定要和公钥的名字一样,如下:后边的就是你的公钥名字,此时公钥的命名就为XXX.pub) 



git add gitosis.conf
git commit -am "添加“test.git”版本库。"
1
2
1
2


第一次提交会让你填写你的身份



git config --global user.email "你的邮箱@XXX.com"
git config --global user.name "你的名字"
(再次填写commit信息)
git commit -am "添加“test.git”版本库。"
git push origin master
1
2
3
4
5
1
2
3
4
5

四、测试第一个属于自己的Git版本库。
git clone git@localhost:test.git
cd test
echo "HelloWorld" > HelloWorld
git add HelloWorld
git commit -am "第一个提交记录"
git push origin master
1
2
3
4
5
6
1
2
3
4
5
6



参考文章ubuntu上配置git服务器关于Git的读写权限配置可以参考此文章。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: