本文由【云老大】 TG@yunlaoda360 撰写
方法一:使用 Google Cloud Source Repositories
- 创建 Google Cloud 项目 :如果还没有创建项目,需在 Google Cloud Console 中创建一个新项目。
- 启用相关 API :在 Google Cloud Console 中启用 Cloud Source Repositories API。
- 安装 Git 和 Google Cloud CLI :在本地机器上安装 Git 和 Google Cloud CLI。
- 初始化 Google Cloud CLI :使用命令
gcloud init初始化 CLI,按照提示登录账号并选择项目。 - 创建私有 Git 仓库 :运行命令
gcloud source repos create [REPO_NAME],将[REPO_NAME]替换为仓库名称。 - 将仓库添加为远程仓库 :在本地 Git 仓库中运行命令
git remote add google https://source.developers.google.com/p/[PROJECT_ID]/r/[REPO_NAME],将[PROJECT_ID]和[REPO_NAME]替换为实际的项目 ID 和仓库名称。 - 推送代码到私有仓库 :运行命令
git push google master,将本地代码推送到私有仓库。
方法二:手动搭建 Git 服务器
- 创建谷歌云服务器实例 :在 Google Cloud Console 中创建一个 Compute Engine 虚拟机实例,选择合适的操作系统和配置。
- 连接到服务器 :使用 SSH 工具连接到创建的实例。
- 安装 Git :在服务器上运行命令
sudo apt update和sudo apt install git安装 Git。 - 创建 Git 用户 :运行命令
sudo adduser git创建一个专门用于运行 Git 服务的用户。 - 创建证书登录 :收集所有需要登录的用户的公钥,将其导入到
/home/git/.ssh/authorized_keys文件中。 - 初始化 Git 仓库 :在服务器上选定一个目录作为 Git 仓库,如
/srv/sample.git,然后运行命令sudo git init --bare sample.git初始化裸仓库,并将仓库的拥有者改为git用户。 - 克隆仓库到本地 :在本地机器上运行命令
git clone git@[SERVER_IP]:/srv/sample.git,将[SERVER_IP]替换为服务器的公网 IP 地址。