因为学校内网的问题,无法直接使用github在本地和服务器之间同步代码,就想到了自建Git来进行代码的同步。
经历很多问题之后,现在终于可以正常使用了,写一下博客记录一下。
在服务器上创建裸仓库
要先创建一个空的目录,用于存放裸仓库。
mkdir -p /path/to/your-repo.git
之后进行初始化
git init --bare
这个时候,裸仓库已经可以用了。
配置远端仓库
本地配置
在本机,远端仓库可以配置为SSH的形式。
建议使用.ssh/config先配置好SSH,之后git直接使用已经配置好的SSH链接。
config配置如下(其实VScode也采用的这个config):
Host Host名称HostName IPUser usernamePort 22IdentityFile 秘钥文件
然后就可以直接配置SSH远端仓库
git remote add origin username@host:/path/to/your-repo.git
但是这里注意,本地的分支可能是main
,远端默认分支可能是master
。
这里为了后续操作方便,可以创建master
分支,然后切换到master
上,删掉main
。
服务器配置
服务器的仓库不需要使用SSH链接,直接填写仓库路径。
git remote add origin /path/to/your-repo.git
仓库初始化
添加gitignore,其实不用手动写,github有的:
github Python gitignore
记得自己添加.DS_Store
先add gitignore,不然后面清除缓存麻烦。第一次push指定远端分支。
git add .gitignore
git commit
git push origin master
之后所有的东西上传到仓库
git add .
git commit init
git push
大功告成!这样就可以有自己的Git仓库啦。