Mac 使用 Git 連接 GitHub, Bitbucket
在Mac上使用Git連接GitHub, Bitbucket的流程基本上是有八成像!
本篇將會介紹使用自定義的key來連接。
畢竟有可能需要使用多組key連去不同的Repository,或是需要將連接GitHub和Bitbucket的SSH key分開!
但如果想知道git基本指令的話,可以參考這兩篇:Git 讀書會、連猴子都能懂的Git入門指南
Step 1
SSH-keygen建立key
自建SSH key
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Andy-MBP:~ Andy$ ssh-keygen -t rsa -f ~/.ssh/BitBucket-ssh Generating public/private rsa key pair. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /Users/Andy/.ssh/BitBucket-ssh. Your public key has been saved in /Users/Andy/.ssh/BitBucket-ssh.pub. The key fingerprint is: SHA256:9xxxkqwnxxxxxxxxxxSOFI Andy@Andy-MBP.local The key's randomart image is: +---[RSA 2048]----+ | . | | . . | | . + o | | O o o | | = * S | | + + ooE o . | |= . ++= = * o . | |o+.+oo + O B . | |..oo... =o= . | +----[SHA256]-----+ |
『 ~/.ssh/ 』資料夾內應該有剛才建立的SSH Key
1 2 3 4 5 6 7 8 9 10 11 12 |
Andy-MBP:~ Andy$ ls -l .ssh/ total 96 -rw------- 1 Andy staff 1.6K 2 8 16:00 BitBucket-ssh -rw-r--r-- 1 Andy staff 401B 2 8 16:00 BitBucket-ssh.pub -rw------- 1 Andy staff 1.6K 2 4 14:35 andy-GreenGeek -rw-r--r-- 1 Andy staff 395B 2 4 14:35 andy-GreenGeek.pub -rw------- 1 Andy staff 1.7K 4 10 2016 github_rsa -rw-r--r-- 1 Andy staff 405B 4 10 2016 github_rsa.pub -rw------- 1 Andy staff 5.9K 1 3 03:19 known_hosts -rw------- 1 Andy staff 5.6K 8 10 23:15 known_hosts.old -rw------- 1 Andy staff 1.6K 1 3 03:15 my-ssh-key -rw-r--r-- 1 Andy staff 394B 1 3 03:15 my-ssh-key.pub |
Step 2
複製Public Key
複製的方法有很多,這種是最不安全的!
也可以直接用vim開啟key直接複製,或是『cat .ssh/config | pbcopy』!
(最安全的方法就屬使用 pbcopy)
1 2 |
Andy-MBP:~ Andy$ cat .ssh/BitBucket-ssh.pub ssh-rsa AAAAB3NzaC1ycxxxxxxxxxxm61yN Andy@Andy-MBP.local |
將Public key 匯入 GitHub或BitBucket
Step 3
允許連線
贊助廣告
設定允許與GitHub或BitBucket連線
指令說明:
-i:表示我想要用哪一個特定的public key跟伺服器連線,這邊就要給你指定的private key的路徑
BitBucket
1 2 3 4 |
Andy-MBP:~ Andy$ ssh -T git@bitbucket.org -i .ssh/BitBucket-ssh logged in as polun-wang. You can use git or hg to connect to Bitbucket. Shell access is disabled. |
GitHub
如果你有按照上述步驟建立GitHub-ssh的ssh key,也可以這樣下指令
1 |
Andy-MBP:~ Andy$ ssh -T git@github.com -i .ssh/GitHub-ssh |
Step 4
讓git用對的SSH Key
方法-1
將ssh key加入SSH Identify清單
1 2 |
Andy-MBP:~ Andy$ ssh-add ~/.ssh/BitBucket-ssh Identity added: /Users/Andy/.ssh/BitBucket-ssh (/Users/Andy/.ssh/BitBucket-ssh) |
查看一下SSH Identify清單,剛剛增加的key應該要在清單之中!
1 2 |
Andy-MBP:~ Andy$ ssh-add -l 2048 SHA256:9neoGxxxxxxOWOFI /Users/Andy/.ssh/BitBucket-ssh (RSA) |
方法-2
設定.SSH內的config檔
1 2 |
Andy-MBP:~ Andy$ cd ~/.ssh/ Andy-MBP:.ssh Andy$ vim config |
1 2 3 4 5 6 7 8 9 10 11 |
# Github Host github.com HostName github.com User git IdentityFile /Users/Andy/.ssh/github_rsa # Bitbucket Host bitbucket.org HostName bitbucket.org User git IdentityFile /Users/Andy/.ssh/BitBucket-ssh |
Step 5
測試!
從Bitbucket上複製Repository到MAC上
1 2 3 4 5 6 |
Andy-MBP:Vue.js Andy$ git clone git@bitbucket.org:polun-wang/vue.js-learning.git Cloning into 'vue.js-learning'... remote: Counting objects: 3, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 0 (delta 0) Receiving objects: 100% (3/3), done. |
但我之前執行『 git clone 』一直遇到這個錯誤『Permission denied.』
原因就是因為沒有將ssh key加入SSH Identify清單
123456 Cloning into 'vue.js-learning'...Permission denied (publickey).fatal: Could not read from remote repository.Please make sure you have the correct access rightsand the repository exists.