在linux系统中搭建git仓库
本文的场景为在本地虚拟机中搭建git仓库,以此管理主机中的项目。
环境说明
虚拟机 : VM
操作系统 :
主机:windows10
虚拟机:ubuntu16.04
连接协议:SSH
软件:git、github desktop(可选)
操作步骤
一、前置准备
在[git官网](Git - Downloading Package)中下载windows版本的git
在vm虚拟机中安装unbuntu操作系统
为虚拟机系统设置一个固定ip(如:192.168.56.101)
二、在 Ubuntu 虚拟机上安装并配置 Git 服务
安装git
1
2sudo apt update
sudo apt install git创建 Git 用户(用于管理仓库)
1
sudo adduser git
配置 SSH(确保主机能访问)
确认 SSH 服务已启动
1
sudo service ssh status
若未安装 SSH:
1
sudo apt install openssh-server
三、在 Ubuntu 中创建裸仓库
创建目录并初始化裸仓库
1
2
3
4sudo mkdir -p /home/git/repositories/myproject.git
sudo chown -R git:git /home/git/repositories
cd /home/git/repositories/myproject.git
sudo -u git git init --bare仓库路径:
/home/git/repositories/myproject.git
四、设置 SSH 密钥(Windows 与 Ubuntu 免密码连接)
在 Windows 上(用 Git Bash)生成 SSH 密钥
1
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
一直回车,默认生成在:
1
C:\Users\<YourUsername>\.ssh\id_rsa
将公钥复制到虚拟机的 git 用户
1
ssh-copy-id git@192.168.56.101
如果提示
ssh-copy-id
命令不存在,可以手动执行:1
cat ~/.ssh/id_rsa.pub
然后将内容复制,在虚拟机中执行:
1
2sudo -u git mkdir /home/git/.ssh
sudo -u git nano /home/git/.ssh/authorized_keys粘贴公钥内容后保存。
五、从 Windows 上推送项目到 Ubuntu 仓库
在 Windows 上打开 Git Bash,进入项目目录
1
cd /path/to/your/project
初始化并推送项目
1
2
3
4
5git init
git remote add origin git@192.168.56.101:/home/git/repositories/myproject.git
git add .
git commit -m "Initial commit"
git push -u origin master完成仓库搭建。