兩種方式:
- 使用 HTTP 模式芙粱,項目必須為public的坟比,否則無權(quán)限 pull 和 push 代碼
-
重點介紹第二種 SSH 模式
填入 SSH Key
如果你本地有直接輸入即可裳食,如果沒有需要生成SSH幕与,命令如下:
ssh-keygen -t rsa -C "your.email@example.com" -b 4096
然后會讓你輸入文件名疗琉,密碼雁竞,如下圖
生成SSH.jpeg
切換到rsa文件所在的目錄,打開 rsa.pub 文件漾抬,復(fù)制key宿亡,并在gitlab中配置即可
// 打開 rsa.pub 文件
cat rsa.pub
key.jpeg
特殊的情況
我先用http模式鏈接到gitlab,然后才發(fā)現(xiàn)項目是private的(尷尬了...)
// http模式鏈接到gitlab
git remote add origin https://github.com/LibertyPhoenix/myLean.git
使用SSH再次綁定遠(yuǎn)程倉庫時纳令,報錯如下:
fatal: remote origin already exists
解決方案
// 刪除git配置文件中的遠(yuǎn)程倉庫信息
git remote rm origin
可以用命令查看前后配置文件信息的對比:
vi .git/config
刪除前
刪除后
之后再使用命令綁定遠(yuǎn)程倉庫即可
git remote add origin git@github.com/LibertyPhoenix/myLean.git
遇到的一些問題
已在本地和gitlab配置了SSH key挽荠,但是仍報錯
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
這種情況的原因是沒有權(quán)限直接對master分支進(jìn)行操作
切換到develop分支
如果遠(yuǎn)程倉庫有develop分支克胳,而本地fetch后無法看到,說明沒有和遠(yuǎn)程倉庫關(guān)聯(lián)上
解決方案
暴力方法圈匆,直接換一個目錄漠另,clone遠(yuǎn)程倉庫代碼(已配置SSH),就直接關(guān)聯(lián)上了遠(yuǎn)程倉庫跃赚,這時就能看到其他分支了
切換到develop分支
git checkout develop
然后提交代碼即可
git add .
git commit -m "first"
git push
配置 gitignore
Android:
https://github.com/github/gitignore/blob/master/Android.gitignore