在GitHub中生成个人访问令牌后,是否有必要将其存储在机器本地的某个地方?

如果是,是否有首选的存储方式?


当前回答

您可以缓存您的凭证为一个定义的时间使用:

git config --global credential.helper cache

默认的缓存时间是900秒(15分钟),但可以通过以下方式更改:

git config --global credential.helper 'cache --timeout=3600'

参见以下Github页面:

https://docs.github.com/en/github/using-git/caching-your-github-credentials-in-git

这不是一个永久存储,并且与其他注释一样,凭证不应该以纯文本形式存储,这是一个安全风险。我使用密码管理器(https://bitwarden.com/)存储PAT(个人访问令牌),然后将其复制到第一次使用时,然后将其缓存到其中。如果您在Github帐户上启用2FA,则需要PAT。

其他回答

您可以缓存您的凭证为一个定义的时间使用:

git config --global credential.helper cache

默认的缓存时间是900秒(15分钟),但可以通过以下方式更改:

git config --global credential.helper 'cache --timeout=3600'

参见以下Github页面:

https://docs.github.com/en/github/using-git/caching-your-github-credentials-in-git

这不是一个永久存储,并且与其他注释一样,凭证不应该以纯文本形式存储,这是一个安全风险。我使用密码管理器(https://bitwarden.com/)存储PAT(个人访问令牌),然后将其复制到第一次使用时,然后将其缓存到其中。如果您在Github帐户上启用2FA,则需要PAT。

你可以使用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

以我为例,在Ubuntu中,被接受的解决方案不能处理像这样的消息

Git: 'credential-manager'不是Git命令

但是店长却做得很好:

git config --global credential.helper store

使用git代替。基本上,将每个https://github调用替换为访问令牌+ https://

git config——global url."https://<username>:<github-token>@github.com/". insteadof "https://github.com/

现在每次调用github都会自动添加您的凭据。

我在这里找到了很好的答案。 更多关于git insteadOf的信息。

我喜欢在存储库中对它们进行加密,并使用.envrc (https://direnv.net/)加载它们。

为了做到这一点,我使用ssh-vault来加密数据,使用我的ssh密钥,GitHub已经公开,例如:

echo MY_TOKEN="secret" | ssh-vault -u <github-user> create > my-encypted-vars.ssh

然后.envrc的内容看起来像这样:

echo "Enter ssh key password"
context=$(ssh-vault view $HOME/projects/my-encrypted.ssh | tail -n +2)
export ${context}

这将解密my-encrypted-vars中的数据。ssh文件,并设置MY_TOKEN到我的环境变量,每次我cd到项目目录。

通过这样做,令牌/变量被“安全地”存储,并且随时可以作为环境变量使用