Git 免密認證:Git Credential Helper
提(ti)到(dao) Git 免密,也許大多數人會想到(dao) SSH。但是當 SSH 不可(ke)用的時候怎么(me)辦?
比如(ru)你(ni)沒(mei)有提(ti)交 SSH 公鑰的(de)權限(xian)。
又比如自建的 Git,如果所有域名(ming)都在 Cloudflare 的保護(hu)下,不(bu)想暴露 ip 地址,就只能(neng)用 https。(PS: 這種情況(kuang)也可以用 Cloudflare Tunnel,而且需要客戶端額外配置,不(bu)是很推薦(jian))
Git Credential Helper
Git Credential Helper 就是解決在 HTTPS 協(xie)議(yi)下 Git 操作時需要頻(pin)繁(fan)輸入密碼或(huo)者 PAT 的痛點。
設置僅需一行代碼:
git config --global credential.helper <helper_name>
macOS
git config --global credential.helper osxkeychain
Windows
新版(ban)的(de) Windows Git 應該,無需單(dan)獨設(she)置。
git config --global credential.helper manager
# 或者
git config --global credential.helper wincred
Linux
如果有(you)桌面環境,可以存到(dao)GNOME Keyring 或者 KDE Wallet
git config --global credential.helper libsecret
如果沒(mei)有,可以(yi)看下面(mian)的
通用方法
-
Cache
默認在內存(cun)中持續(xu)15分鐘,如果有需要可(ke)以(yi)延長時間。
git config --global credential.helper cache # 一小時 git config --global credential.helper 'cache --timeout=3600' -
Store
這種方法不安(an)全,因為是(shi)明文存儲,不要在公共環境或(huo)者(zhe)生產環境使用,僅作為最(zui)后的手段。
git config --global credential.helper store # 或者指定位置 git config --global credential.helper 'store --file ~/.my-credentials' -
GCM: Git Credential Manager
安裝方法見:
只(zhi)要(yao)安裝(zhuang),無需配置即可(ke)使用(yong)。
對于Windows 用戶來說,只要安裝了 GCM,所(suo)有的(de) WSL 均(jun)可分享憑證,很方便。
