網頁

2021/8/14

GitHub macOS使用GCM Core存取GitHub repository

在macOS使用Git Credential Manager Core(GCM Core)管理存取GitHub repository的憑證及進行Git操作驗證。


本篇已廢棄

改參考「Mac 使用SSH Key push到GitHub repository」或「GitHub 使用Personal access tokens推送remote repo」。



今天(2021/8/14)用Git push本地分支到GitHub的遠端分支時,出現下面錯誤訊息無法推送。

$ git push -u origin ...
remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.
remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information.
fatal: unable to access 'https://github.com/...': The requested URL returned error: 403

原因是GitHub在去年(2020年)6月公告從2021年8月13日將不再接受用GitHub帳號密碼的方式進行Git操作的驗證,而必須改用token或SSH key驗證。中間也收過不少次email通知說要改用token驗證,但都沒理會直到今天才來處理這件事。

若以HTTPS存取GitHub要求使用personal access token(PAT)驗證。必須先在GitHub設定頁面產生token(一筆亂碼),然後記下此token,日後Git操作需要驗證時輸入此token即可。

不過官方現在建議改用GCM Core來管理憑證,這工具是幫你產生token並把token cache起來,在需要的時候幫你拿出來驗證,省去自己手動去GitHub產生token及每次自己輸入token的麻煩。


使用GCM Core驗證

在macOS使用GCM Core的方式如下。

首先用Homebrew安裝Git,在命令列輸入brew install git。你可能會覺得原本已經有Git了幹麻還裝Git?因為原本用的是macOS自帶的Git,而GCM Core需要新版的Git,所以要安裝Homebrew的Git。

$ brew install git
...
==> Installing git
==> Pouring git--2.32.0_1.big_sur.bottle.tar.gz
==> Caveats
The Tcl/Tk GUIs (e.g. gitk, git-gui) are now in the `git-gui` formula.

Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

Emacs Lisp files have been installed to:
  /usr/local/share/emacs/site-lisp/git
==> Summary
🍺  /usr/local/Cellar/git/2.32.0_1: 1,517 files, 42.0MB
==> Caveats
==> git
The Tcl/Tk GUIs (e.g. gitk, git-gui) are now in the `git-gui` formula.

Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

Emacs Lisp files have been installed to:
  /usr/local/share/emacs/site-lisp/git

接著用Homebrew安裝microsoft git,輸入brew tap microsoft/git

$ brew tap microsoft/git
==> Tapping microsoft/git
Cloning into '/usr/local/Homebrew/Library/Taps/microsoft/homebrew-git'...
remote: Enumerating objects: 275, done.
remote: Counting objects: 100% (146/146), done.
remote: Compressing objects: 100% (146/146), done.
remote: Total 275 (delta 86), reused 0 (delta 0), pack-reused 129
Receiving objects: 100% (275/275), 66.48 KiB | 476.00 KiB/s, done.
Resolving deltas: 100% (161/161), done.
Tapped 4 casks (20 files, 90.0KB).

上面安裝過程中會跳出幾次git-credential-osxkeychain要求授權存取GitHub credential的對話筐,輸入本機登入密碼按Allow




安裝GCM Core,輸入brew install --cask git-credential-manager-core,過程中會要求輸入本機登入密碼。

$ brew install --cask git-credential-manager-core
==> Downloading https://github.com/microsoft/Git-Credential-Manager-Core/release
==> Downloading from https://github-releases.githubusercontent.com/158405551/570
######################################################################## 100.0%
==> Installing Cask git-credential-manager-core
==> Running installer for git-credential-manager-core; your password may be nece
Package installers may write to any location; options such as `--appdir` are ignored.
Password:
installer: Package name is Git Credential Manager Core
installer: Installing at base path /
installer: The install was successful.
🍺  git-credential-manager-core was successfully installed!

然後會跳出允許GCM Core存取GitHub權限的頁面,點選[Authorize GitCredentialManager]。



接著輸入GitHub密碼。



授權成功。



安裝完後即可順利存取GitHub repository。


若要解除安裝GCM Core,輸入brew uninstall --cask git-credential-manager-core

$ brew uninstall --cask git-credential-manager-core


沒有留言:

張貼留言