Git – GitHub 出現 「Please make sure you have the correct access rights and the repository exists.」

接手專案維護的時候,準備要 git pull 卻出現錯誤

案例一 2023/08/08

如果你看到

Host key verification failed.
fatal: Could not read from remote repository.

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

那代表你沒有存取 repository 的權限,可以觀察

git remote -v

# 如果出現類似以下格式,代表過去的連線使用 ssh,那麼將繼續使用
origin  git@github.com:fdjkgh580/laradock-with-mysql.git (fetch)
origin  git@github.com:fdjkgh580/laradock-with-mysql.git (push)

# 如果出現類似以下格式,代表過去的連線透過 http 的方式連線,應該趁這次換掉。
origin	https://github.com/{username}/{repo}.git (fetch)
origin	https://github.com/{username}/{repo}.git (push)

以現在的 Github 來說已經強烈建議改為 ssh 連線方式,因此不再建議使用輸入 Github 的個人帳號密碼來使用。請參考我這篇取解決這個問題:

Github – 使用 git push 使用 ssh key 免帳號密碼

案例二 不建議的方式

ERROR: Repository not found.
fatal: Could not read from remote repository.

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

這是因為使用 ssh 連線到 GitHub 但可能出現 key 遺失或遭受更改的關係,如果不是要自動化處理,我們可以替換成詢問 username 與 password 的模式來解決這個困擾。

# 先確定目前遠端位置
git remote -v

# 如果出現這樣,代表前人用的是 SSH 連線方式
origin	git@github.com:{username}/{repo}.git (fetch)
origin	git@github.com:{username}/{repo}.git (push)

# 打這行會不給修改
git remote add origin https://github.com/{username}/{repo}.git

# 會說已經存在
fatal: remote origin already exists.

# 所以要打這行,修改為走 https 通道的詢問模式
git remote set-url origin https://github.com/{username}/{repo}.git

# 接著確認一下
git remote -v

# 代表成功替換
origin	https://github.com/{username}/{repo}.git (fetch)
origin	https://github.com/{username}/{repo}.git (push)

# 接著 pull 
git pull

# 就會詢問我們 GitHub 帳號密碼了
Username for 'https://github.com':

參考來源

發表迴響