git 時不同帳戶,不同專案的管理

【緣由】原先 laptop 上都是自己的 github account, 可因為前陣子也因為需要開新 account 用來 clone 了公司的 project, 於是修改了設定。導致早上要 submit 自己的東西到自己的 github 時卻發現 account 是公司的. 這花了不少時間理解,決定還是筆記一下。

 Host work.github.com  
   HostName github.com  
   User WORK_GITHUB_USERNAME  
   PreferredAuthentications publickey  
   IdentityFile ~/.ssh/id_work_rsa  
   IdentitiesOnly yes  
 Host personal.github.com  
   HostName github.com  
   User PERSONAL_GITHUB_USERNAME   
   PreferredAuthentications publickey  
   IdentityFile ~/.ssh/id_personal_rsa  
   IdentitiesOnly yes  
(1) 首先,修改 ~/.ssh/config, 增加各自的設定 (參考上例). HOST 的 [url] 只是接下來幫你找到該設定,怎麼填並無太大關係。

 $ git config user.name "John Doe"  
 $ git config user.email johndoe@example.com  
(2) 接著,設定 local repository 的帳號密碼。很重要!因為我就耗在這裡... orz  之前就是沒改這個部分,直接跳 (3)  後,submit 的仍是公司 account...

 [remote "origin"]  
   fetch = +refs/heads/*:refs/remotes/origin/*  
   url = git@personal.github.com:PERSONAL_GITHUB_USERNAME/project.git  
(3) 修改 .git/config 的 url, 如上例。

 git remote set-url origin git@work.github.com:your_work_organization/project.git  
 git remote add origin  git@personal.github.com:PERSONAL_GITHUB_USERNAME/project.git   
(4) 另外若是要設定 remote repository, 像是 github 新開 project 時可以參考上例。

* 2016/02/05 *
遇到以下錯誤,根據【參考2】只要加上 commit 就好了....
error: src refspec master does not match any.
error: failed to push some refs to 'git@personal.git.com:DISTRIBUTION/project.git'

* 2016/07/19 *
今天遇到無法 clone 在網頁上生成的 project, 最後發現問題在於 username 的部分, ex.,
git@personal.github.com:PSERSONAL_GITHUB_USERNAME/projectgit
根據【參考3】這李必須填git 而不是自己的帳號, 要注意一下.


【參考】(stackoverflow)  Git pushing to remote GitHub repository as wrong user
【參考2】(stackoverflow) src-refspec-master-does-not-match-any-when-pushing-commits-in-git
【參考3】(github) https://help.github.com/articles/error-permission-denied-publickey/

Comments

Popular posts from this blog

股票評價(Stock Valuation) - 股利折現模型

openwrt feed的使用

R 語言:邏輯回歸 Logistic Regression using R language (二)