如何搭建自己的git服务器

GitHub,Gitee 想来大家都用过,我们的代码就是托管在这些平台上的。因此,你可能好奇为什么我们不自己搭建一个 git 呢服务器?下面,就开始教大家如何一步步搭建自己的 git 服务器(试验成功的那一刻还是很让人激动的)。

我自己的虚拟机是 centOS7 的,首先肯定要安装 git 和 git-daemon,可以使用自带的 yum 进行安装。

yum install -y git
yum install -y git-daemon

复制

[root@master ~]# git --version
git version 2.28.0

[root@master ~]# yum install -y git-daemon
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Running transaction
  Installing : perl-Git-1.8.3.1-23.el7_8.noarch                                                                   

  ...

Installed:
  git-daemon.x86_64 0:1.8.3.1-23.el7_8                                                                                 

Dependency Installed:
  git.x86_64 0:1.8.3.1-23.el7_8                          perl-Git.noarch 0:1.8.3.1-23.el7_8                          

Complete!

复制

虚拟机服务端

创建 git 目录

[root@master ~]# mkdir git
[root@master ~]# cd git
[root@master git]# pwd
/root/git

复制

创建 git 仓库文件夹

[root@master git]# mkdir test-repo.git
[root@master git]# cd test-repo.git/
[root@master test-repo.git]# 

复制

初始化空目录仓库

[root@master test-repo.git]# git --bare init
Initialized empty Git repository in /root/git/test-repo.git/
[root@master test-repo.git]# ls -l
total 16
drwxr-xr-x. 2 root root    6 Sep 15 22:56 branches
-rw-r--r--. 1 root root   66 Sep 15 22:56 config
-rw-r--r--. 1 root root   73 Sep 15 22:56 description
-rw-r--r--. 1 root root   23 Sep 15 22:56 HEAD
drwxr-xr-x. 2 root root 4096 Sep 15 22:56 hooks
drwxr-xr-x. 2 root root   21 Sep 15 22:56 info
drwxr-xr-x. 4 root root   30 Sep 15 22:56 objects
drwxr-xr-x. 4 root root   31 Sep 15 22:56 refs

复制

修改仓库的 mod 权限

[root@master test-repo.git]# cd ..
[root@master git]# chmod 770 test-repo.git/ -R
[root@master git]# chmod 775 test-repo.git/ -R

复制

设置默认新建的文件和文件夹同属于其父目录的用户组

[root@master git]# chmod g+s test-repo.git -R
[root@master git]# set -m g:root:rwx test-repo.git
[root@master git]# 

复制

开启 git daemon 服务

[root@master git]# git daemon --verbose --export-all --base-path=/root/git/test-repo.git/
[3680] Ready to rumble

复制

本地机客户端

创建目录并初始化成仓库

Administrator@PC-20200713AJJH MINGW64 /d/MyProject
$ mkdir test-repo
Administrator@PC-20200713AJJH MINGW64 /d/MyProject
$ cd test-repo
Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo
$ git init
Initialized empty Git repository in D:/MyProject/test-repo/.git/

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo (master)
$

复制

查看 config 文件

文件在仓库的 .git 目录下。

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo (master)
$ cd .git/

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo/.git (GIT_DIR!)
$ vim config
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true

复制

关联远程仓库

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo/.git (GIT_DIR!)
$ git remote add origin ssh://192.168.128.139/root/git/test-repo.git

复制

修改 config 文件

我用的 root 账号登录的,所以 url 也改成 root@192.168.128.139 的形式:

[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
[remote "origin"]
        url = ssh://root@192.168.128.139/root/git/test-repo.git
        fetch = +refs/heads/*:refs/remotes/origin/*

复制

git commit 一些东西

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo/.git (GIT_DIR!)
$ cd ..

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo (master)
$ touch test.txt

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo (master)
$ vim test.txt
hello world

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo (master)
$ git add test.txt
warning: LF will be replaced by CRLF in test.txt.
The file will have its original line endings in your working directory.

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo (master)
$ git commit -m "first commit :)"
[master (root-commit) a1e4f83] first commit :)
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt

复制

关联分支并推送

Administrator@PC-20200713AJJH MINGW64 ~/Desktop/test-repo (master)
$ git remote -v
origin  ssh://root@192.168.128.139/root/git/test-repo.git (fetch)
origin  ssh://root@192.168.128.139/root/git/test-repo.git (push)

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo (master)
$ git push -u origin master
root@192.168.128.139's password:
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 217 bytes | 217.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://192.168.128.139/root/git/test-repo.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

复制

虚拟机服务端

新建一个终端查看 push 记录

因为刚才那个终端还跑着 git-daemon 服务,所以先不要关掉(后来发现好像关掉了也不影响,不知道是为什么)。

[root@master git]# cd test-repo.git/
[root@master test-repo.git]# pwd
/root/git/test-repo.git
[root@master test-repo.git]# git log
commit a1e4f83292ac8d9128c94a402ce2ada752fb14aa (HEAD -> master)
Author: 2392863668 <2392863668@qq.com>
Date:   Tue Sep 15 23:16:34 2020 +0800

    first commit :)

复制

可以看到,服务端已经成功接收到了。

当然,客户端可以多推送一些上来,服务端都是可以接收到的。

本地机客户端

再次推送

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo (master)
$ vim test.txt

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo (master)
$ git add test.txt
warning: LF will be replaced by CRLF in test.txt.
The file will have its original line endings in your working directory.

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo (master)
$ git commit -m "second commit"
[master ec56e9e] second commit
 1 file changed, 1 insertion(+)

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo (master)
$ git push
root@192.168.128.139's password:
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 261 bytes | 261.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://192.168.128.139/root/git/test-repo.git
   a1e4f83..ec56e9e  master -> master

复制

虚拟机服务端

[root@master test-repo.git]# git log
commit ec56e9ee09edd5b4ab9ea5fe46927e91d4e09fd5 (HEAD -> master)
Author: 2392863668 <2392863668@qq.com>
Date:   Tue Sep 15 23:21:26 2020 +0800

    second commit

commit a1e4f83292ac8d9128c94a402ce2ada752fb14aa
Author: 2392863668 <2392863668@qq.com>
Date:   Tue Sep 15 23:16:34 2020 +0800

    first commit :)

复制

从服务端克隆仓库

我们甚至还可以从服务端克隆仓库下来:

Administrator@PC-20200713AJJH MINGW64 ~/Desktop
$ git clone ssh://root@192.168.128.139/root/git/test-repo.git
Cloning into 'test-repo'...
root@192.168.128.139's password:
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (6/6), done.

Administrator@PC-20200713AJJH MINGW64 ~/Desktop
$ cd test-repo/

Administrator@PC-20200713AJJH MINGW64 ~/Desktop/test-repo (master)
$ git log
commit ec56e9ee09edd5b4ab9ea5fe46927e91d4e09fd5 (HEAD -> master, origin/master, origin/HEAD)
Author: 2392863668 <2392863668@qq.com>
Date:   Tue Sep 15 23:21:26 2020 +0800

    second commit

commit a1e4f83292ac8d9128c94a402ce2ada752fb14aa
Author: 2392863668 <2392863668@qq.com>
Date:   Tue Sep 15 23:16:34 2020 +0800

    first commit :)

Administrator@PC-20200713AJJH MINGW64 ~/Desktop/test-repo (master)
$

复制

SSH 免密登录

如果你不想每次远程操作都输入密码的话,就略微看一下这一节吧!免密登录已经不是什么稀奇事儿了,我们稍微过一下!

先用 ssh-keygen -t rsa 命令在本地机客户端生成密钥:

把 id_rsa.pub 上传到虚拟机,并将 id_rsa.pub 内容追加(这儿的 >> 表示追加的意思,不然很可能就把文件里边原有的东西给覆盖掉了)到 authorized_keys 里边去:

[root@master ~]# pwd
/root
[root@master ~]# cat id_rsa.pub >> .ssh/authorized_keys

复制

然后?然后就没了!这个时候你在本地机客户端再次克隆的时候,就不需要输入虚拟机服务端的密码了。(这个操作使用 ssh-copy-id 来弄也行的)

Administrator@PC-20200713AJJH MINGW64 ~/Desktop
$ git clone ssh://root@192.168.128.139/root/git/test-repo.git
Cloning into 'test-repo'...
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (6/6), done.

复制

后记

细心的你可能会发现,服务端目录结构一直是这样的:

[root@master test-repo.git]# ll
total 16
drwxrwsr-x.  2 root root    6 Sep 15 22:56 branches
-rwxrwsr-x.  1 root root   66 Sep 15 22:56 config
-rwxrwsr-x.  1 root root   73 Sep 15 22:56 description
-rwxrwsr-x.  1 root root   23 Sep 15 22:56 HEAD
drwxrwsr-x.  2 root root 4096 Sep 15 22:56 hooks
drwxrwsr-x.  2 root root   21 Sep 15 22:56 info
drwxrwsr-x. 10 root root   90 Sep 15 23:21 objects
drwxrwsr-x.  4 root root   31 Sep 15 22:56 refs

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 要搭建一个私有的 Git 服务器,基本步骤如下: 1. 安装 Git:在 Linux 服务器上执行命令 sudo apt-get install git 即可安装 Git。 2. 创建 Git 用户:执行命令 sudo adduser git 创建用户名为 git 的新用户。 3. 初始化 Git 仓库:在 git 用户的 home 目录下创建存放 Git 仓库的目录,并在此目录下执行 git init --bare <仓库名>.git 命令来创建一个裸仓库。其中,<仓库名> 可以自己定义。 4. 配置 SSH:在 Git 用户的 home 目录下,创建 .ssh 目录并将 SSH 公钥文件 id_rsa.pub 添加到 authorized_keys 文件中。这样就可以使用 SSH 协议来访问 Git 服务。 5. 设置访问权限:在 Git 仓库所在的目录下,执行命令 chown -R git:git <仓库名>.git 将目录的所有权赋予 git 用户,并执行命令 chmod -R 755 <仓库名>.git 修改权限。 6. 打开端口:如果使用 SSH 协议访问 Git 服务,则需要打开服务器端口。默认情况下,SSH 协议使用的端口为 22。 7. 测试:在客户端使用 Git 工具测试访问 Git 服务是否正常工作。 需要注意的是,搭建私有 Git 服务器需要具备一定的 Linux 和 Git 知识,建议在了解清楚操作流程和安全风险后再进行相关配置。 ### 回答2: 在Linux系统中,可以通过搭建私有Git服务器来进行代码的版本控制和团队协作。下面是一些搭建私有Git服务器的具体步骤和注意事项: 1. 安装Git 首先需要在Linux系统中安装Git。可以通过命令行输入sudo apt-get install git来安装Git。 2. 创建Git用户 为了保证数据的安全,最好创建一个专门的Git用户来管理Git服务器的安全。可以通过命令sudo adduser git来创建Git用户,并设置密码。 3. 初始化Git仓库 在Git用户下,通过命令行输入sudo mkdir /git,创建一个文件夹用于存放Git仓库,可以通过sudo chown git:git /git来给Git用户设定相应的权限。接下来,在/git文件夹下创建具体的Git仓库,可以通过命令sudo git init --bare /git/repo.git来创建一个名为repo.gitGit仓库。 4. 配置Git用户 为了保证Git服务器的安全,需要为Git用户配置SSH密钥。可以通过命令行输入sudo su git,然后通过ssh-keygen来生成相应的SSH公私钥,将公钥添加到到~/.ssh/authorized_keys文件中。这样,在使用Git客户端时,就可以使用SSH私钥来访问Git服务器了。 5. 完成 搭建私有Git服务器的过程非常简单,只需要几个简单的步骤就可以完成。最后,在Git客户端中,可以通过命令git remote add origin git@服务器IP地址:/git/repo.git来在Git客户端中添加一个指向Git服务器的远程仓库,然后,就可以使用Git工具进行版本管理和团队协作了。 需要注意的是,在搭建私有Git服务器时,需要考虑到数据的安全和权限的设置,避免不必要的数据丢失和权限泄露。同时,也需要注重Git客户端的配置和安全,避免恶意攻击和数据泄露。 ### 回答3: 搭建私有Git服务器是非常有必要的,特别是对于一些敏感的代码或是重要的开发项目。如果您想要搭建自己的Git服务器,那么Linux系统是您最佳的选择,因为它是开源的、免费的,而且以稳定和安全而著称。下面是一个简单介绍如何在Linux平台上搭建Git服务器的步骤。 第一步,安装Git。在Linux系统中,您可以使用Package Manager(包管理器)来安装Git。例如,在Debian和Ubuntu上,您可以使用以下命令安装Git: sudo apt-get install git-core 在安装完成后,您可以通过输入以下命令来检查Git是否安装成功:git --version 第二步,创建Git用户。在Linux系统中,您需要为Git服务创建一个专门的用户。首先,您需要使用以下命令来创建一个名为git的用户: sudo adduser git 接下来,您需要登录到git用户: sudo su git 然后创建一个名为.gitolite的目录: mkdir .gitolite cd .gitolite 接着,在该目录下执行以下命令以克隆Gitolite代码库: git clone git://github.com/sitaramc/gitolite 第三步,安装Gitolite。运行以下命令以安装Gitolite: gitolite/install -symlink 这将在/home/git/bin目录下安装Git服务,并创建一个.gitolite.rc配置文件。 第四步,管理Git仓库。您可以将Git仓库放到/home/git/repositories目录下,例如在/home/git/repositories下创建一个名为test.gitGit仓库: git init --bare /home/git/repositories/test.git 然后,您可以将Git仓库命名为任何您想要的名称。 第五步,配置Gitolite。在git用户的主目录下,创建一个名为.gitolite.rc的文件,其中指定您的Git仓库和访问权限等。 使用以下命令来添加一个新的协作者: git clone git@localhost:gitolite-admin.git cd gitolite-admin 编辑conf/gitolite.conf文件以添加新用户及其权限: repo test RW+ = user1 RW = user2 git commit -m "Added users and their permissions" git push origin master 第六步,测试Git服务器。使用以下命令来检查您的Git服务是否运行正常: ssh git@localhost info 现在,您就成功地在Linux系统中搭建了自己的Git服务器。如果您想要添加更多的Git仓库,您可以重复以上步骤即可。有了一个私有的Git服务器,您就可以安全地管理您的代码库了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

知一NN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值