364 字
2 分鐘
Git Authentication Problem Notes

Situation#

在Linux系統中,將遠端origin設定好,準備push上去時,系統提示輸入Username for 'https://github.com'Password for 'https://<my account>/',照著輸入完之後,居然跳出:

remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/<my account>/<my repo>/'

但是,當我轉而使用windows的cmd操作push時,卻連密碼都沒問我,就成功推送了。

Solution#

REF_1#

GitHub/Discussions: “github respiratory authentication failed”

From August 13, 2021, GitHub is no longer accepting account passwords when authenticating Git operations. You need to add a PAT (Personal Access Token) instead, and you can follow the below method to add a PAT on your system.

Create Personal Access Token on GitHub
From your GitHub account, go to Settings => Developer Settings => Personal Access Token => Generate New Token (Give your password) => Fillup the form => click Generate token => Copy the generated Token, it will be something like ghp_sFhFsSHhTzMDreGRLjmks4Tzuzgthdvfsrta

這個操作是要我生成PAT(就是存取用Token),於是我照做了。 我生成了StudyingSpace Repo Access For Local Git ,到期日是2024/4/22。

REF_2#

GitHub/Docs: “Using a personal access token on the command line”

Terminal window
git clone https://HOSTNAME/USERNAME/REPO.git
>> Username: YOUR_USERNAME
>> Password: YOUR_PERSONAL_ACCESS_TOKEN

照做之後,果然可以成功push了。但是,還有個小問題:每次進行操作,都要輸入一次UsernamePassword,超級麻煩。

REF_3#

GitHub/Docs: “Caching your GitHub credentials in Git” 我用了 GitHub CLI 的HTTPS協定授權,最後總算是成功設定自動授權,以後就可以自由使用remote的各種功能了。

REF_4#

StackOverflow

Terminal window
git config --global credential.helper store

這個方法要簡便的多,也不用額外安裝任何東西,只是記住密碼而已。

Git Authentication Problem Notes
https://blade520.com/posts/git-origin-problem-2024/
作者
Blade/磯江
發佈於
2024-11-12
許可協議
CC BY-NC-SA 4.0