git clone
一个 GitHub 仓库时,提供两种协议链接
- HTTPS:
https://github.com/user/repo.git
- SSH:
[email protected]:user/repo.git
第一次操作时,会遇到鉴权验证问题,需要进行配置后,方可成功连接到远程仓库。
HTTPS
> git clone https://github.com/user/repo.git
Cloning into 'xx'...
Username for 'https://github.com': # 登录用户名,例如邮箱
Password for 'https://github.com': # PAT,切记不是登录密码
GitHub 推荐(且必须)使用 PAT(Personal Access Token,个人访问令牌)代替密码进行鉴权。
令牌与基于密码的身份验证相比,提供了许多安全优势:
- 唯一性:令牌特定于 GitHub,可以按使用或按设备生成。
- 可撤销:可以随时单独撤销令牌,而无需更新未受影响的凭据。
- 有限性:令牌可以方便设置权限。
- 随机性:令牌不会受到简单密码可能会受到的字典类型或蛮力尝试的影响。
GitHub 支持两种类型的 PAT:fine-grained personal access token 和 personal access token (classic)。GitHub 推荐使用前者。PAT 的创建可见 GitHub 文档和腾讯云。
然而,每一次连接都需要鉴权,麻烦不少。可以使用 GitHub CLI 或 GitHub Desktop。同时 Git 也拥有一个凭证系统来处理此问题:
git config --global credential.helper xxx
-
默认不缓存,即每一次连接都会询问 Username 和 Password(二者合称凭证)。
-
cache
将凭证存储在内存中一段时间。
可选
--timeout <seconds>
参数,设置存储有效时间(默认为900
,即 15 分钟)。似乎不支持 Windows,见 "credential-cache unavailable; no unix socket support" · Issue #3892 · git-for-windows/git。
-
store
将凭证以明文存储在硬盘中。
可选
--file <path>
参数,自定义存储凭证的文件路径(默认为~/.git-credentials
)。 -
系统凭证管理器
-
osxkeychain
特定于 Mac,将凭证存储到系统用户的钥匙串。
-
wincred
特定于 Windows,将凭证不加密存储到 Windows 凭证管理器。
-
libsecret
特定于 Linux。
-
-
OAuth
-
manager
见 git-ecosystem/git-credential-manager。
安装见 git-credential-manager/docs/install.md,Arch Linux 下安装 git-credential-managerAUR。然后配置凭证存储方式,设置环境变量
GCM\_CREDENTIAL\_STORE
或 Git 设置选项credential.credentialStore
为secretservice
。首次鉴权会弹出验证窗口(如下),可用浏览器登录验证,或者使用 PAT。
-
oauth
-
-
密码管理器
- git-credential-gopass: stores in gopass password manager.
- git-credential-lastpass: stores in LastPass password manager.
- git-credential-1password: stores in 1Password password manager.
- git-credential-keepassxc: stores in KeePassXC password manager.
-
Host specific
- git-credential-netlify: authenticates to Netlify.
- git-credential-azure: authenticates to Azure Repos.
同时允许配置多个凭证系统,更多可见 Git - 凭证存储。
SSH
> git clone [email protected]:user/repo.git
Cloning into 'repo'...
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
需要配置 SSH 密钥。如何操作见 GitHub 文档,介绍得很清楚。