Table of Contents

自己搭建Git服务器


基于SSH的简易服务器(SSH + 裸仓库)

本方式基于SSH,认证基于Linux的用户体系,服务器上用户A有一个Git Repo,需要使用A的密钥进行clone/push等操作。所以对安全性要求高的话,建议单独建立一个没有sudo权限的git用户:Linux用户管理

# 在服务器上创建Repo,注意init的时候要使用--bare创建裸仓库
# 这样创建的仓库可以被其他主机clone/push等,但是在(服务器)本地无法像普通的Repo一样访问
# 裸仓库是专门用作远程仓库推送/拉取用的,没有工作区(即没有实际的源代码文件),结构略有不同
# 普通仓库不能当作远程仓库被其他人 push。只有 裸仓库 才能接受其他仓库推送(避免工作区冲突)
# 注意文件夹的命名,后面有.git
ubuntu@instance-20250106-1816:~/git$ mkdir draft.git
ubuntu@instance-20250106-1816:~/git$ cd draft.git/
ubuntu@instance-20250106-1816:~/git/draft.git$ git init --bare
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint: 	git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint: 	git branch -m <name>
Initialized empty Git repository in /home/ubuntu/git/draft.git/

# 在本地clone仓库
# clone的时候指定路径有点繁琐,但是之后会记录在remote,不用再输入了
# oracle是已经配置好的SSH别名
anny@annys-MacBook-Air Downloads % git clone oracle:/home/ubuntu/git/draft.git
Cloning into 'draft'...
warning: You appear to have cloned an empty repository.
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: 	git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: 	git branch -m <name>

# clone好之后就可以正常使用了,和Github没什么区别,注意文件夹的名称,没有.git
anny@annys-MacBook-Air Downloads % cd draft 

Gitea / GitLab / Gogs

提供了Web界面,好像可以使用Docker