博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在虚拟机上搭建自己的 git 服务器并创建 git 仓库
阅读量:4229 次
发布时间:2019-05-26

本文共 8286 字,大约阅读时间需要 27 分钟。

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

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

yum install -y gityum install -y git-daemon
[root@master ~]# git --versiongit version 2.28.0[root@master ~]# yum install -y git-daemonLoaded plugins: fastestmirrorDetermining fastest mirrors * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.comRunning 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 initInitialized empty Git repository in /root/git/test-repo.git/[root@master test-repo.git]# ls -ltotal 16drwxr-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 HEADdrwxr-xr-x. 2 root root 4096 Sep 15 22:56 hooksdrwxr-xr-x. 2 root root   21 Sep 15 22:56 infodrwxr-xr-x. 4 root root   30 Sep 15 22:56 objectsdrwxr-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-repoAdministrator@PC-20200713AJJH MINGW64 /d/MyProject$ cd test-repoAdministrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo$ git initInitialized 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.txtAdministrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo (master)$ vim test.txthello worldAdministrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo (master)$ git add test.txtwarning: 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 -vorigin  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 masterroot@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 -> masterBranch '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 logcommit 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.txtAdministrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo (master)$ git add test.txtwarning: 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 pushroot@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 logcommit ec56e9ee09edd5b4ab9ea5fe46927e91d4e09fd5 (HEAD -> master)Author: 2392863668 <2392863668@qq.com>Date:   Tue Sep 15 23:21:26 2020 +0800    second commitcommit a1e4f83292ac8d9128c94a402ce2ada752fb14aaAuthor: 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.gitCloning 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 logcommit ec56e9ee09edd5b4ab9ea5fe46927e91d4e09fd5 (HEAD -> master, origin/master, origin/HEAD)Author: 2392863668 <2392863668@qq.com>Date:   Tue Sep 15 23:21:26 2020 +0800    second commitcommit a1e4f83292ac8d9128c94a402ce2ada752fb14aaAuthor: 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

 然后?然后就没了!这个时候你在本地机客户端再次克隆的时候,就不需要输入虚拟机服务端的密码了。

Administrator@PC-20200713AJJH MINGW64 ~/Desktop$ git clone ssh://root@192.168.128.139/root/git/test-repo.gitCloning 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]# lltotal 16drwxrwsr-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 HEADdrwxrwsr-x.  2 root root 4096 Sep 15 22:56 hooksdrwxrwsr-x.  2 root root   21 Sep 15 22:56 infodrwxrwsr-x. 10 root root   90 Sep 15 23:21 objectsdrwxrwsr-x.  4 root root   31 Sep 15 22:56 refs

但是竟然没有找到你 push 上去的 text.txt 文件,这是因为我们初始化仓库的时候用的命令 ”git init --bare” 初始化一个裸仓库,至于裸仓库和一般仓库的区别,请参考 。

更多的东西,就靠大家自己去探索啦! :)

转载地址:http://ijjqi.baihongyu.com/

你可能感兴趣的文章
Java Programming with Oracle JDBC
查看>>
Hack Proofing Your Network (Second Edition)
查看>>
XML Programming (Core Reference)
查看>>
Visual Studio .NET: The .NET Framework Black Book
查看>>
Best Kept Secrets in .NET
查看>>
SQL: The Complete Reference
查看>>
Wireless Network Hacks & Mods For Dummies
查看>>
Programming INDIGO
查看>>
.NET Development Security Solutions
查看>>
3ds Max 8 Bible
查看>>
Hack Proofing Your Web Applications
查看>>
Hack Proofing ColdFusion
查看>>
Routing TCP/IP, Volume II
查看>>
Unix Systems Programming: Communication, Concurrency and Threads, Second Edition
查看>>
Hack Proofing Your Network (Second Edition)
查看>>
XML Programming (Core Reference)
查看>>
Macromedia Studio 8 All-in-One Desk Reference For Dummies
查看>>
Telecom for Dummies
查看>>
ASP.NET 2.0 Illustrated
查看>>
PHP 5 in Practice
查看>>