在GitHub中生成个人访问令牌后,是否有必要将其存储在机器本地的某个地方?
如果是,是否有首选的存储方式?
在GitHub中生成个人访问令牌后,是否有必要将其存储在机器本地的某个地方?
如果是,是否有首选的存储方式?
当前回答
在我的用例中,我将PAT存储在密码管理器中,例如LastPass, KeePass, 1Password。当我在Linux环境(例如Docker)中需要它时,我将PAT保存在一个环境变量中,然后使用git的凭据帮助器设置。例如:
git config --global credential.helper 'cache --timeout 600'
<< eof tr -d ' ' | git credential-cache store
protocol=https
host=github.com
username=nonce
password=${GITHUB_PAT}
eof
对于PAT,用户名可以是任何东西,除了空白。以下是详细阐述的要点:
https://gist.github.com/rwcitek/da862e9e27cc28d3e96e62a2ca4b2b64
其他回答
使用git代替。基本上,将每个https://github调用替换为访问令牌+ https://
git config——global url."https://<username>:<github-token>@github.com/". insteadof "https://github.com/
现在每次调用github都会自动添加您的凭据。
我在这里找到了很好的答案。 更多关于git insteadOf的信息。
以我为例,在Ubuntu中,被接受的解决方案不能处理像这样的消息
Git: 'credential-manager'不是Git命令
但是店长却做得很好:
git config --global credential.helper store
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
sudo apt update
sudo apt install gh
gh auth login
它将要求输入协议和令牌
然后我再次克隆回购。它不是要信物
你可以使用pass存储github https令牌。
将git主机映射到pass条目的两个备选方案:
Bash脚本映射到右传递项:
#!/usr/bin/env bash
# assuming "get" action from git and a config like this
# git config --global credential.helper $XDG_BIN_HOME'/git_credentials_from_pass $@'
while IFS= read -r line
do
echo "$line"
if [[ "$line" =~ host=.*github.com.* ]]; then
echo "username=your_user_name"
echo "password=$(pass show token_github.com/your_username)"
#else ...
fi
done
改变your_username和token_github.com的方式,你设置它通过pass插入。
这将添加令牌传递,无需输入或粘贴两次:
echo your_github_token | sed p | pass add token_github.com/your_username
安装pass-git-helper:
git config --global credential.helper '!pass-git-helper $@'
pass-git-helper需要一个ini文件来映射git请求和pass条目。 $ {XDG_CONFIG_HOME} / pass-git-helper git-pass-mapping.ini例子:
[DEFAULT]
username_extractor=entry_name
[github.com*]
target=token_${host}/your_github_username
尝试启用此功能以帮助跨推/拉持久化
git config credential.helper store
对于正在克隆的repo / macOS用户/安装iTerm2 https://iterm2.com/
使直到
只要在需要时单击该片段即可。 另外,你在用哦,我的zsh,对吧? https://github.com/ohmyzsh/ohmyzsh