如何搭建自己的git服务器

这篇具有很好参考价值的文章主要介绍了如何搭建自己的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 命令在本地机客户端生成密钥:

git服务器搭建,git,服务器,github

把 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

 文章来源地址https://www.toymoban.com/news/detail-754609.html

到了这里,关于如何搭建自己的git服务器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 使用Gogs搭建自己的github服务器

    Gogs官方网址:https://gogs.io/ 在官网首页可以找到二进制运行,点击进去 让咱们去 github 咱们就去 github 看看好了。https://github.com/gogs/gogs/releases 找到自己需要的版本。 使用命令 可以查看自己的 Linux 服务器是 64位的还是32位的。 我这里显示的是 x86_64 ,因此是 64位的。 下载完成

    2024年02月09日
    浏览(62)
  • 搭建自己的Git服务器

    环境 服务端 :Ubuntu 22.04 客户端 :Win11_x64 前提条件 :需要确保在Windows机器上能够ping通Ubuntu服务器, 并且服务端与客户端均已安装了Git软件 服务端上的配置操作 以Ubuntu服务器作为Git服务端的运行环境,并方便后期免密推送及管理代码的需求,需要先安装OpenSSH及创建git专属用

    2024年02月15日
    浏览(50)
  • 安卓手机搭建Gitea-自己的git服务器

    官网的介绍是: Gitea的首要目标是创建一个极易安装,运行非常快速,安装和使用体验良好的自建 Git 服务。我们采用Go作为后端语言,这使我们只要生成一个可执行程序即可。并且他还支持跨平台,支持 Linux, macOS 和 Windows 以及各种架构,除了x86,amd64,还包括 ARM 和 PowerPC

    2023年04月24日
    浏览(50)
  • 搭建自己的git服务器--win10系统使用Gitea快速搭建git-server

    2023-03-28 本文简单介绍了使用gitea开源软件搭建自己git服务器的过程; 本文记录的在win10系统上快速安装gitea,实现了git服务端的搭建。 搭建自己的git仓库服务端,搭建到云主机,连接方便,但大文件受限于网速。 搭建到局域网,网速快。各有优势,多试多玩,总有收获。 安

    2024年02月13日
    浏览(50)
  • 【网络篇】如何搭建自己的DNS服务器

    平时练习域名解析,一般直接修改的/etc/hosts文件。对于服务器数量小的情况完全可以,但是如果服务器数量较多,每个都修改比较麻烦。 DNS是作为域名解析。在实际的生产过程中,尤其是对于内网搭建的情况,DNS不可能使用互联网的DNS,这时需要我们自行搭建。 我这里准备

    2024年02月05日
    浏览(30)
  • 分享本周所学——在Windows上搭建自己的Git服务器并支持互联网远程访问

            大家好,欢迎来到《分享本周所学》第十期。本人是一名人工智能初学者(虽然我最近写的东西都跟人工智能没什么关系),刚刚上完大一。之前想跟同学搞项目,我的学校自己有一个GitLab服务器,于是就把项目存在上面,但是后来收到学校邮件说学校服务器不允

    2024年02月09日
    浏览(35)
  • 如何快速搭建自己的阿里云服务器(宝塔、Xshell、Xftp配置)

    目录 一、前言 二、准备工作 1、新手申请 2、安全组设置  3、修改实例 4、下载Xshell和Xftp  5、安装并配置Xshell 6、本地连接云服务器  7、安装并配置Xftp  三、搭建环境 1、安装宝塔服务器运维面板 2、登录宝塔Linux面板 3、查看宝塔主页  4、测试环境 对于新手或者学生党来说

    2024年02月05日
    浏览(42)
  • 如何搭建Git服务器?

    一、为什么要搭建Git服务器? 我们都知道gitHub是目前最大的代码托管网站,但是其也有一限制,就是私有托管没有提供免费的服务,许多人常常要花钱去购买私有仓库,但是如果能够自己搭建一个git服务器,就可以随便使用啦。 二、准备工作 1、GitBlit: 是一个纯 Java 库用来

    2024年02月15日
    浏览(30)
  • 如何利用家庭宽带和自己家里的电脑,搭建可在公网访问的服务器

    像软件开发工程师,我们经常需要在任何地方访问家里的文件服务器,或者通过 http与https访问自己家里的web服务器。但是,由于公网IP稀缺,家庭宽带一般都没有 IPv4的公网IP。而且,即使你有IPv4的公网IP,一般运营商为家庭宽带封禁了80,8080,443,23等端口。非常的不方便。  

    2024年02月05日
    浏览(96)
  • 如何搭建自己的V Rising自建服务器,以及常见的V Rising服务器问题解决方案

    V rising官方服务器经常无法连接,无法和小伙伴玩耍;如何搭建自己的V rising服务器呢?还可以修改掉落倍率,加快游戏进度,搭建自己的私人服务器。 最近V rising这个游戏很火呀,迫不及待地和小伙伴一起玩;但是,V rising官方的服务器实在是太不稳定:延时高、丢包率高,

    2023年04月18日
    浏览(26)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包