Git 是一款開源的分布式版本控制系統(tǒng),而 GitHub 是依托 Git 的代碼托管平臺。
GitHub 利用 Git 極其強大的克隆和分支功能漠烧,使得社區(qū)成員能夠自由地參與到開源項目中去辽聊。
我們在遠程服務(wù)器創(chuàng)建一套代碼后纱控,怎么通過Git和Github關(guān)聯(lián)、維護酸钦、糾錯怪得?
1. 安裝/查看 Git
如果你已經(jīng)安裝好了 Git,可以忽略這一步
# Ubuntu 安裝 Git
$ apt-get install git
# 查看 Git 版本信息
$ git version
# 配置 Git 用戶信息
$ git config --global user.name "your name"
$ git config --global user.email "youremail@gmail.com"
2. 開啟 SSH 服務(wù)
# Ubuntu 安裝 SSH
$ apt-get install ssh
# 查看 SSH 服務(wù)狀態(tài)
$ ps -e | grep sshd
3. 生成 SSH KEY
使用 ls -al ~/.ssh 命令查看 ssh key 是否存在钝鸽,若存在則忽略這一步
# 生成 SSH KEY
$ ssh-keygen -t rsa -C "yourname@gmail.com"
# 查看 SSH KEY
$ cd ~/.ssh
# 復制 SSH KEY, vim 打開后復制
$ vim ~/.ssh/id_rsa.pub
4. Github 上添加SSH KEY
登錄 GitHub汇恤,打開 Personal settings 頁面,選擇 SSH and GPG keys 選項
添加 SSH key 之后拔恰,Linux 就可以通過 SSH 建立本地 Git 與 GitHub 的連接了。更具體的可以參見Github 添加SSH KEY
Git push 解決403問題
將遠程服務(wù)器的代碼提交到Github:
$ echo "# HEMS_NILM_Bayesian" >> README.md
$ git init
$ git add .
$ git commit -m "first commit"
$ git branch -M main
$ git remote add origin https://github.com/XXX/xxxx.git
$ git push -u origin main
remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead. remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information. fatal: unable to access 'https://github.com/xxx/xxxx.git/': The requested URL returned error: 403
這里官網(wǎng)上有詳細的自查解決403問題的方法基括。
但是對我來說以上方法都不適用颜懊,兩天后我發(fā)現(xiàn)我只需要將HTTPS方式改為SSH 方式就可以了
$ git remote add origin git@github.com:XXX/xxxx.git
成功!