GIT仓库本地部署

前言

很多时候我们不想依赖 GitHub/GitLab(或者网络不稳定/项目不方便上公网),但又希望在多台电脑之间同步项目代码。最稳妥的方案是:用一台 Linux 机器作为 Git 服务器(中央仓库),其它电脑都从它 clone / pull / push

本文将完整演示如何在内网/本地环境下搭建一个 Git 服务器,并形成标准工作流:每次开发前先 pull 最新,再 commit & push

前置条件

  • 1.服务器机可以通过 SSH 登录(例如 user@server-ip

  • 2.客户端机器安装 Gitgit --version

  • 3.网络可达(同一局域网更简单;公网也可,只要 SSH 能连通)

在服务器机创建 bare 仓库

bare 仓库是 Git 官方推荐用作"服务端仓库"的形式:

它没有工作区(不包含源码文件树),只存历史对象和引用,更安全、也不容易误操作。

在服务器上执行:

c 复制代码
mkdir -p ~/git
cd ~/git
git init --bare thesis.git

此时服务器上会出现:

bash 复制代码
~/git/thesis.git

它就是"中央仓库地址"。

把已有项目从电脑 A 推到服务器

电脑 A 里你已经初始化并 commit 过(比如毕业设计项目目录)。现在做两件事:

  • 1.增加远端 origin 指向服务器仓库
  • 2.push 当前分支到服务器
bash 复制代码
cd /path/to/your/thesis-project

git remote add origin user@server-ip:~/git/thesis.git
git push -u origin master   # 如果本地主分支叫 main 就改成 main

不确定分支名,可以通过命令 git branch --show-current 来显示。

在电脑 B 上 clone 项目副本

此时,可以通过 git clone 的方式在电脑 B 上克隆并修改项目,执行:

bash 复制代码
git clone user@server-ip:~/git/thesis.git
cd thesis

此时,主机 AB 都可以修改,提交。

工作流:先 pull,修改,再 commit & push

后续,任一电脑要修改该项目,则:

bash 复制代码
cd thesis
git pull --rebase

# 修改代码...

git add -A
git commit -m "your message"
git push

总结

完结撒花!

相关推荐
-拟墨画扇-8 小时前
Git | 分支管理操作
git·gitee·github·gitcode
ModestCoder_8 小时前
Git 版本管理教程
大数据·git·elasticsearch
YMGogre8 小时前
Git 提交信息规范
git
charlie1145141919 小时前
Git团队协作完全入门指南(下)
git
golang学习记12 小时前
Facebook 为什么不用 Git?
git·elasticsearch·facebook
GIS阵地12 小时前
git拉取时报错
大数据·git·elasticsearch
无限进步_15 小时前
C++ Vector 全解析:从使用到深入理解
开发语言·c++·ide·windows·git·github·visual studio
charlee4415 小时前
Git使用经验总结9-Git提交关联到Issue
git·issue
-拟墨画扇-15 小时前
Git | Bug分支操作
git·gitee·github·bug·gitcode