mac 使用命令行向 github 提交代碼

讓 mac 本地和本身的 github 網站創建鏈接(ssh)vue

下載安裝 git 網址: https://git-scm.com/downloadsgit

查看安裝是否成功: git -versiongithub

$ git versionshell

git version 2.15.1 (Apple Git-101)json

chengyuandeMBP:.ssh chengyuan$ echarts

建立 ssh 密鑰:查看是否有 ssh dom

$ cd ~/.sshssh

$ lside

id_rsa id_rsa.pub known_hostsfetch

建議無論有沒有都要從新建立 ssh 

建立 ssh 

$ ssh-keygen -t rsa -C xxx@xx.com                  // 1⃣️xxx@xx.com爲你註冊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 352091626@qq.com
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 註冊時的郵箱

 

 

 

 github ssh key 配置

查看 key 配置是否生效 ssh - T git@github.com

$ ssh -T git@github.com

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 git@github.com:xxxx/xxxx.git // 鏈接遠程倉庫 (即 github)
$ git push -u origin master                // 首次提交

注意 git@github.com:xxxx/xxxx.git == git@github.com: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 git@github.com

不 OK,去從新配置 key 

OK! 接着查看遠程倉庫詳細信息

$ git remote -v

origin git@github.com:dopocheng/alone-part.git (fetch)

origin git@github.com:dopocheng/alone-part.git (push)

dopocheng 必須是你本身的 GitHub 用戶名, alone-part 是你第一次提交的某個項目對應的遠程倉庫名(repository)

GitHub 查看倉庫名

如若不對就要添加或修改遠程倉庫

$ git remote add origin git@github.com:dopocheng/alone-part.git

fatal: remote origin already exists.

先刪除 git remove origin

再添加 git remote add origin git@github.com:dopocheng/alone-part.git(我用的是SSH 你也能夠切換 use https)

這樣再次查看遠程倉庫信息就對了 git push 就沒有問題了!!!

相關文章
相關標籤/搜索