跳转至

让SSH服务器当Git 服务器

前言:B站刷视频看到这篇 熟肉翻译[^1],内容让我有尝试的兴趣。

如果还有兴趣,可以去看git book[^2]

1.环境

一台能远程SSH连接的服务器,多台客户端。客户端需要都能ssh连接这台服务器。

2.远程服务器

#创建一个仓库,仓库名称叫hello-test
git init --bare hello-test
#会发现有HEAD hooks info 等文件和文件夹
cd hello-test && ls -la

3.客户端

#你能通过scp 拿到远程仓库的HEAD文件
scp 账号@ip:hello-test/HEAD .
cat HEAD

你会发现HEAD里面的内容就是git支持的内容,因为git也支持类似scp和ssh 的功能

# 全局设置默认分支为 main
git config --global init.defaultBranch main

#git clone 直接获取远程的仓库,第一次提示是个空仓库
git clone 账号@ip:hello-test
#进入hello-test
cd hello-test
#创建一个文件,随笔写点内容
echo "hello world" >> hello.txt
#之后和git操作一样
git add hello.txt
git commit -m "hello-world"
#git 将内容提交到远程
git push origin main

5.远程服务器能直接拿到客户端提交的日志

git logs

6.其他客户端能直接clone拿到最新的项目

git clone 账号@ip:hello-test
cd hello-test && ls -la
cat hello.txt

参考资料

[^1]:[中英熟肉] 你不需要 GitHub:任何 SSH 服务器都是一个 Git 服务器 | Тsфdiиg

视频作者推荐:git 官网的 git book 的4.1章

[^2]:4.1 Git on the Server - The Protocols