讓 mac 本地和自己的 github 網站建立連接(ssh) 下載安裝 git 網址: https://git-scm.com/downloads 查看安裝是否成功: git -version $ git version git version 2.15.1 (Apple Git-101) che ...
讓 mac 本地和自己的 github 網站建立連接(ssh)
下載安裝 git 網址: https://git-scm.com/downloads
查看安裝是否成功: git -version
$ git version
git version 2.15.1 (Apple Git-101)
chengyuandeMBP:.ssh chengyuan$
創建 ssh 密鑰:查看是否有 ssh
$ cd ~/.ssh
$ ls
id_rsa id_rsa.pub known_hosts
建議不管有沒有都要重新創建 ssh
創建 ssh
$ ssh-keygen -t rsa -C [email protected] // 1⃣️[email protected]為你註冊GitHub時的郵箱賬號
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/dpc/.ssh/id_rsa): // 2⃣️.ssh預設路徑,不輸入則不修改
Enter passphrase (empty for no passphrase): // 3⃣️密碼長度至少為4,否則失敗
Enter same passphrase again:
Your identification has been saved in /Users/dpc/.ssh/id_rsa.
Your public key has been saved in /Users/dpc/.ssh/id_rsa.pub.
The key fingerprint is:
8d:d3:5f:31:ae:13:48:f0:78:df:a1:8f:a5:a4:c0:06 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
| . |
| + |
| E . + + |
| o * o + + |
| S + = = |
| . o + O |
| . * . |
| . |
| |
+-----------------+
打開 github 點擊自己頭像 settings ➡️ 左側導航欄 SSH and GPG keys ➡️ new SSH key
回到命令行 $ open ~/.ssh
彈出的文件夾中打開 id_rsa.pub 文件並複製裡面內容(也可以自己根據路徑去找)
將複製的內容 粘貼到 new SSH key 的 key 中 Add 保存,title 隨便 我填了自己的 github 註冊時的郵箱
查看 key 配置是否生效 ssh - T [email protected]
$ ssh -T [email protected]
Enter passphrase for key '/Users/dpc/.ssh/id_rsa': // 剛纔設置的密碼****
Hi dopocheng! You've successfully authenticated, but GitHub does not provide shell access. //祝賀你 ssh 設置成功!!
提交代碼
1.初次提交代
進入要提交代碼的文件夾下(important!!)
$ git init // 初始化本地倉庫
$ git add xx.json // 添加要提交的代碼文件
$ git commit -m "你的註釋...." // 提交到本地倉庫
$ git remote add origin [email protected]:xxxx/xxxx.git // 連接遠程倉庫 (即 github)
$ git push -u origin master // 首次提交
註意 [email protected]:xxxx/xxxx.git == [email protected]:dopocheng(github用戶名不知道的點頭像)/alone-part(項目名隨便取建議和項目一樣的名字).git
2.修改代碼或換本地電腦提交
git status 查看修改的文件
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: "src/views/complex-component/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243 (2).txt"
modified: src/views/echarts/covid-19.vue
no changes added to commit (use "git add" and/or "git commit -a")
有修改且要題提交的 git add 然後git commit
$ git add src/views/echarts/covid-19.vue // 添加要提交的代碼文件
$ git commit -m "新冠狀病毒統計修改" // 提交到本地倉庫
$ git push origin master
如果 git push 失敗
查看上面的 key 配置 ssh - T [email protected]
不 OK,去重新配置 key
OK! 接著查看遠程倉庫詳細信息
$ git remote -v
origin [email protected]:dopocheng/alone-part.git (fetch)
origin [email protected]:dopocheng/alone-part.git (push)
dopocheng 必須是你自己的 GitHub 用戶名, alone-part 是你第一次提交的某個項目對應的遠程倉庫名(repository)
GitHub 查看倉庫名
如若不對就要添加或修改遠程倉庫
$ git remote add origin [email protected]:dopocheng/alone-part.git
fatal: remote origin already exists.
先刪除 git remove origin
再添加 git remote add origin [email protected]:dopocheng/alone-part.git(我用的是SSH 你也可以切換 use https)
這樣再次查看遠程倉庫信息就對了 git push 就沒有問題了!!!