如何把一個專案同時儲存至 Bitbucket 和 Github?

最近接案,遇到的公司是使用 Bitbucket, 但自己習慣都是用 Github, 就在思考同一個檔案能否同時 push 上 Bitbucket 和 GitHub,方便自己作業。後來有成功,來分享上網找到的方法及遇到的問題。

1. 在檔案 terminal 寫入你要新增的git:

原本的 Repo 已在 Github, 想同時新增至 Bitbucket :

$ git remote add bitbucket https://your_user@bitbucket.org/your_repo.git

原本的 Repo 已在 Bitbucket, 想同時新增至 Github :

$ git remote add github https://github.com/your_user/your_repo.git

2. 檢查是否有新增到:

$  git remote

若有出現以下,就表示新增成功:

github
origin

3. 當 git add 及 git commit 結束, 可選擇 git push 的地方

$ git push github 
$ git push bitbucket 

當兩邊都push上去就表示成功啦。


但是我遇到問題了

git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists

因為github權限被拒絕,所以無法上傳檔案至github。


解決方法

  1. 到 github 的 setting > developer settings > personal accsess tokens > generate new token
  1. 填寫token名稱、期限、及勾選需要的權限, 並按下 generate token,就會產出一組密碼,並複製他。
  1. 打開 vscode , 打開 home 資料夾
  1. 新增 .my-credentials 檔案, 輸入以下並儲存:
https://your_user:token

https://iris1114:ghp_xxxxxxxxxxxxxxxxx@github.com
  1. 回到 Repo terminal, 刪除舊的remote
git remote remove github 
  1. 再重新執行上述一開始提到的三個步驟就成功 push 上去啦。

參考網站

stackoverflow

寫得有點隨便, 不過就記錄,以免下次又忘了解決方法~
也希望幫助到有需要的人~

Leave a Comment

Your email address will not be published. Required fields are marked *

ZH-TW EN ES