当前位置:   article > 正文

如何搭建自己的git服务器_git服务器搭建

git服务器搭建

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

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

  1. yum install -y git
  2. yum install -y git-daemon

复制

  1. [root@master ~]# git --version
  2. git version 2.28.0
  3. [root@master ~]# yum install -y git-daemon
  4. Loaded plugins: fastestmirror
  5. Determining fastest mirrors
  6. * base: mirrors.aliyun.com
  7. * extras: mirrors.aliyun.com
  8. * updates: mirrors.aliyun.com
  9. Running transaction
  10. Installing : perl-Git-1.8.3.1-23.el7_8.noarch
  11. ...
  12. Installed:
  13. git-daemon.x86_64 0:1.8.3.1-23.el7_8
  14. Dependency Installed:
  15. git.x86_64 0:1.8.3.1-23.el7_8 perl-Git.noarch 0:1.8.3.1-23.el7_8
  16. Complete!

复制

虚拟机服务端

创建 git 目录

  1. [root@master ~]# mkdir git
  2. [root@master ~]# cd git
  3. [root@master git]# pwd
  4. /root/git

复制

创建 git 仓库文件夹

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

复制

初始化空目录仓库

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

复制

修改仓库的 mod 权限

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

复制

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

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

复制

开启 git daemon 服务

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

复制

本地机客户端

创建目录并初始化成仓库

  1. Administrator@PC-20200713AJJH MINGW64 /d/MyProject
  2. $ mkdir test-repo
  3. Administrator@PC-20200713AJJH MINGW64 /d/MyProject
  4. $ cd test-repo
  5. Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo
  6. $ git init
  7. Initialized empty Git repository in D:/MyProject/test-repo/.git/
  8. Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo (master)
  9. $

复制

查看 config 文件

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

  1. Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo (master)
  2. $ cd .git/
  3. Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo/.git (GIT_DIR!)
  4. $ vim config
  5. [core]
  6. repositoryformatversion = 0
  7. filemode = false
  8. bare = false
  9. logallrefupdates = true
  10. symlinks = false
  11. ignorecase = true

复制

关联远程仓库

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

复制

修改 config 文件

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

  1. [core]
  2. repositoryformatversion = 0
  3. filemode = false
  4. bare = false
  5. logallrefupdates = true
  6. symlinks = false
  7. ignorecase = true
  8. [remote "origin"]
  9. url = ssh://root@192.168.128.139/root/git/test-repo.git
  10. fetch = +refs/heads/*:refs/remotes/origin/*

复制

git commit 一些东西

  1. Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo/.git (GIT_DIR!)
  2. $ cd ..
  3. Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo (master)
  4. $ touch test.txt
  5. Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo (master)
  6. $ vim test.txt
  7. hello world
  8. Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo (master)
  9. $ git add test.txt
  10. warning: LF will be replaced by CRLF in test.txt.
  11. The file will have its original line endings in your working directory.
  12. Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo (master)
  13. $ git commit -m "first commit :)"
  14. [master (root-commit) a1e4f83] first commit :)
  15. 1 file changed, 1 insertion(+)
  16. create mode 100644 test.txt

复制

关联分支并推送

  1. Administrator@PC-20200713AJJH MINGW64 ~/Desktop/test-repo (master)
  2. $ git remote -v
  3. origin ssh://root@192.168.128.139/root/git/test-repo.git (fetch)
  4. origin ssh://root@192.168.128.139/root/git/test-repo.git (push)
  5. Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo (master)
  6. $ git push -u origin master
  7. root@192.168.128.139's password:
  8. Enumerating objects: 3, done.
  9. Counting objects: 100% (3/3), done.
  10. Writing objects: 100% (3/3), 217 bytes | 217.00 KiB/s, done.
  11. Total 3 (delta 0), reused 0 (delta 0)
  12. To ssh://192.168.128.139/root/git/test-repo.git
  13. * [new branch] master -> master
  14. Branch 'master' set up to track remote branch 'master' from 'origin'.

复制

虚拟机服务端

新建一个终端查看 push 记录

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

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

复制

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

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

本地机客户端

再次推送

  1. Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo (master)
  2. $ vim test.txt
  3. Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo (master)
  4. $ git add test.txt
  5. warning: LF will be replaced by CRLF in test.txt.
  6. The file will have its original line endings in your working directory.
  7. Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo (master)
  8. $ git commit -m "second commit"
  9. [master ec56e9e] second commit
  10. 1 file changed, 1 insertion(+)
  11. Administrator@PC-20200713AJJH MINGW64 /d/MyProject/test-repo (master)
  12. $ git push
  13. root@192.168.128.139's password:
  14. Enumerating objects: 5, done.
  15. Counting objects: 100% (5/5), done.
  16. Writing objects: 100% (3/3), 261 bytes | 261.00 KiB/s, done.
  17. Total 3 (delta 0), reused 0 (delta 0)
  18. To ssh://192.168.128.139/root/git/test-repo.git
  19. a1e4f83..ec56e9e master -> master

复制

虚拟机服务端

  1. [root@master test-repo.git]# git log
  2. commit ec56e9ee09edd5b4ab9ea5fe46927e91d4e09fd5 (HEAD -> master)
  3. Author: 2392863668 <2392863668@qq.com>
  4. Date: Tue Sep 15 23:21:26 2020 +0800
  5. second commit
  6. commit a1e4f83292ac8d9128c94a402ce2ada752fb14aa
  7. Author: 2392863668 <2392863668@qq.com>
  8. Date: Tue Sep 15 23:16:34 2020 +0800
  9. first commit :)

复制

从服务端克隆仓库

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

  1. Administrator@PC-20200713AJJH MINGW64 ~/Desktop
  2. $ git clone ssh://root@192.168.128.139/root/git/test-repo.git
  3. Cloning into 'test-repo'...
  4. root@192.168.128.139's password:
  5. remote: Counting objects: 6, done.
  6. remote: Compressing objects: 100% (2/2), done.
  7. remote: Total 6 (delta 0), reused 0 (delta 0)
  8. Receiving objects: 100% (6/6), done.
  9. Administrator@PC-20200713AJJH MINGW64 ~/Desktop
  10. $ cd test-repo/
  11. Administrator@PC-20200713AJJH MINGW64 ~/Desktop/test-repo (master)
  12. $ git log
  13. commit ec56e9ee09edd5b4ab9ea5fe46927e91d4e09fd5 (HEAD -> master, origin/master, origin/HEAD)
  14. Author: 2392863668 <2392863668@qq.com>
  15. Date: Tue Sep 15 23:21:26 2020 +0800
  16. second commit
  17. commit a1e4f83292ac8d9128c94a402ce2ada752fb14aa
  18. Author: 2392863668 <2392863668@qq.com>
  19. Date: Tue Sep 15 23:16:34 2020 +0800
  20. first commit :)
  21. Administrator@PC-20200713AJJH MINGW64 ~/Desktop/test-repo (master)
  22. $

复制

SSH 免密登录

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

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

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

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

复制

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

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

复制

后记

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

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

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/166732
推荐阅读
相关标签
  

闽ICP备14008679号