从零搭建 Git 服务器

适合不想把代码放到 GitHub/GitLab 公共平台、想自己搭的团队或个人。
从最轻量的 Gitea 到纯 Git 裸仓库,两种方案都讲。


方案一:Gitea(推荐)

Gitea 是一个 Go 语言写的 Git 服务,单二进制文件就能跑,内存占用很低(512MB 机器都行)。有 Web 界面、Issue、PR、CI 集成。

安装

# 下载二进制(选最新版本替换版本号)
wget -O /usr/local/bin/gitea https://dl.gitea.com/gitea/1.22/main/gitea-1.22-linux-amd64
chmod +x /usr/local/bin/gitea

# 创建运行用户
adduser --system --group --disabled-password --shell /bin/bash --home /home/git git

# 创建目录结构
mkdir -p /var/lib/gitea/{custom,data,log}
chown -R git:git /var/lib/gitea
chmod -R 750 /var/lib/gitea

配置 systemd 服务

vim /etc/systemd/system/gitea.service
[Unit]
Description=Gitea (Git with a cup of tea)
After=network.target

[Service]
ExecStart=/usr/local/bin/gitea web --config /var/lib/gitea/custom/conf/app.ini
Restart=always
User=git
Group=git
WorkingDirectory=/var/lib/gitea

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable --now gitea

访问配置

浏览器打开 http://你的IP:3000,第一次访问会进入安装页面。

填几个关键项:

配置项 建议值
站点名称 随便写
SSH 服务端口 22(如果没被占)或 2222
Gitea HTTP 端口 3000
数据库 SQLite3(小团队够用)
管理员账号 第一个注册的用户会自动成为管理员

用 Nginx 反代绑定域名

server {
    listen 80;
    server_name git.example.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

日常操作

# 备份
cp -r /var/lib/gitea/data/gitea.db /backup/

# 升级(替换二进制 + 重启)
systemctl stop gitea
cp /tmp/gitea-new /usr/local/bin/gitea
systemctl start gitea

# 查看日志
journalctl -u gitea -f

方案二:纯 Git 裸仓库(最轻量)

如果不需要 Web 界面,只想要一个地方 push/pull 代码,一个 git init --bare 就够了。

服务端

# 在服务器上
ssh user@your-server

# 创建一个专门放仓库的目录
mkdir -p /home/git/repos
cd /home/git/repos

# 创建一个裸仓库(没有工作目录,只存版本历史)
git init --bare myproject.git

客户端

# 在你本地
git remote add origin user@your-server:/home/git/repos/myproject.git
git push -u origin main

加个 SSH 密钥

把客户端的公钥加到服务端的 ~/.ssh/authorized_keys,以后 push/pull 就不用输密码了。

# 在服务端
echo "ssh-ed25519 AAA..." >> /home/git/.ssh/authorized_keys

多仓库管理

多个项目就建多个裸仓库:

cd /home/git/repos
git init --bare project-a.git
git init --bare project-b.git
git init --bare project-c.git

客户端分别 remote:

git remote add origin user@server:/home/git/repos/project-a.git

hook:推送后自动部署

在裸仓库的 hooks 目录放一个脚本,每次 push 后自动把代码同步到 Web 目录。

vim /home/git/repos/myproject.git/hooks/post-receive
#!/bin/bash
TARGET="/var/www/html"
GIT_WORK_TREE=$TARGET git checkout -f

# 或者如果项目需要构建
# cd $TARGET && npm install && npm run build
chmod +x /home/git/repos/myproject.git/hooks/post-receive

方案三:GitLab(重量级)

如果你需要 CI/CD、容器镜像仓库、代码审查等完整功能,用 GitLab。但它对机器要求高——4GB 内存起步。

安装(Ubuntu)

# 安装依赖
sudo apt update
sudo apt install -y curl openssh-server ca-certificates postfix

# 添加 GitLab 仓库并安装
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
sudo EXTERNAL_URL="https://git.example.com" apt install gitlab-ce

装完等几分钟,浏览器访问配置的地址,第一次会要求设 root 密码。

GitLab 的优缺点

优点 缺点
功能最全,开箱即用 吃资源(4G 内存起步)
自带 CI/CD(GitLab Runner) 升级维护比重
代码审查、Issue、Wiki 都有 小团队用显得重

三种方案对比

Gitea 裸仓库 GitLab
内存占用 ~200MB 0(不需要常驻进程) 2~4GB
安装时间 5 分钟 1 分钟 20~30 分钟
Web 界面
用户管理 靠系统用户
CI/CD 内置 CI(轻量) GitLab CI(功能全)
代码审查 基础版 完整版
适合场景 小团队(< 20人) 个人 / 极简 中大型团队

个人建议

  • 就一个人用:裸仓库,简单直接
  • 小团队(2~20人):Gitea,功能够用又轻量
  • 中大型团队或需要完整 CI/CD:GitLab

常见问题

push 时提示 Permission denied

# 确认服务器的目录权限
sudo chown -R git:git /home/git/repos

仓库太大 clone 慢

# 只 clone 最近一次提交,不拉历史
git clone --depth 1 user@server:/home/git/repos/myproject.git

想限制仓库大小

Gitea 可以在管理面板设置单个仓库大小限制。裸仓库的话,在服务端写个定时脚本检查:

#!/bin/bash
for repo in /home/git/repos/*.git; do
    size=$(du -sm "$repo" | cut -f1)
    if [ "$size" -gt 500 ]; then
        echo "[WARN] $repo is ${size}MB" | mail -s "Repo too large" admin@example.com
    fi
done

总结

# 极简方案(一个人用)
ssh user@server
mkdir -p /home/git/repos && cd /home/git/repos
git init --bare myproject.git
exit
# 本地
git remote add origin user@server:/home/git/repos/myproject.git
git push -u origin main

# 小团队方案(有 Web 界面)
systemctl enable --now gitea
# 浏览器配一下,搞定

# 大团队方案
apt install gitlab-ce

如果你是个人开发者,裸仓库方案 5 分钟就能搭好。推荐先从这个开始,不够用了再换 Gitea。

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐