在GitHub生成个人访问令牌后,是否有必要将其存储在本地?
如果是,有没有任何首选的存储方式?
在GitHub生成个人访问令牌后,是否有必要将其存储在本地?
如果是,有没有任何首选的存储方式?
由于PAT可用于在命令行或API上使用Git时代替密码,因此您可以使用git凭据助手来安全地缓存它。
例如,在Windows上,可以通过Windows凭据管理器使用GCM - 适用于Windows、Mac或Linux的Git凭据管理器:
git config --global credential.helper manager-core
# Git 2.39+
git config --global credential.helper manager
(manager-core将被替换为/重命名为Git 2.39+的< strong> manager , Q4 2022)
第一次推送到存储库时,会弹出一个窗口询问您的凭据:用户名和您的PAT。
下一次,它不会询问,并直接重用该PAT,该PAT仍然安全地存储在您的凭据管理器中。
类似的想法适用于带有OSX钥匙串的Mac和Linux与GNOME Keyring(在2021年,它将需要一个DBus会话和libsecret
),但在2021年,GCM-Core涵盖了这些用例。
这个想法仍然是:在加密的凭据存储中存储PAT。
git config --global credential.helper manager-core
# Git 2.39+:
git config --global credential.helper manager
在 Git 2.39 (2022年第4季度) 之前,对于 Linux:
您需要安装 git-credential-manager-core
,下载其 最新版本, 如 gcmcore-linux_amd64.2.0.474.41365.deb
。
sudo dpkg -i <path-to-package>
git-credential-manager-core configure
git config --global credential.credentialStore
。正如agent18在评论中所指出的那样,在安装了GCM在Linux平台上管理的凭据存储有四个选项:
- freedesktop.org Secret Service API
- GPG/
pass
兼容文件- Git内置的credential cache
- 纯文本文件
默认情况下,GCM未配置。
您可以通过设置GCM_CREDENTIAL_STORE
环境变量或credential.credentialStore
Git配置设置来选择要使用的凭据存储。
libsecret-1-0
和libsecret-1-dev
之后,使用git-credential-libsecret
是一个不错的第一步。credential-manager-core
所包含。git push
后,最后两行给了我以下错误:
/var/tmp/.net/user/git-credential-manager-core/unqypyc0.awl/git-credential-manager-core get: 1: /var/tmp/.net/user/git-credential-manager-core/unqypyc0.awl/git-credential-manager-core: not found
- Ilya Palachevgit config -e --global
并向其中添加一个credentialStore值(明文、gpg、secretservice),问题得以解决。
感谢@VonC。 - Mekky_Mayata在我的情况下,在Ubuntu操作系统中,原本被接受的解决方案无效,并显示如下信息:
git: 'credential-manager' is not a git command
但是,使用 store
而不是 manager
可以很好地解决问题:
git config --global credential.helper store
或者,您可以在主目录中创建一个~/.netrc
文件,并将登录凭据保存到其中。
cat ~/.netrc
machine github.com login <login-id> password <token-password>
.netrc
的文件,并将其内容设置为答案中所示。如果您使用的是 Windows,请尝试以下方法
https://dev59.com/i2025IYBdhLWcg3wZlHd - zrash为了将您的凭证存储在缓存中,避免每次执行git操作时都需要登录,请按照以下步骤操作:
git config --global --replace-all credential.helper cache
git push
或git pull
。git push
或任何git操作,从现在开始将不再要求输入登录凭据。测试环境 Ubuntu 20.04,几乎是全新安装,使用的 Git 版本为 2.25.1,Unity 版本为 7.5。
身份验证基础知识
Github 需要一个身份验证密钥(带有特定权限)。每个身份验证密钥都有一定的权限(读取私人仓库、读写公共仓库等),并且“充当密码”,同时可以随时撤销用户的权限。
个人访问令牌
git push
仓库并在提示时输入生成的令牌(非常长的密码)作为密码。不同方式存储密码
xclip
将其带回剪贴板,并在每次粘贴时使用它(太麻烦了)git config credential.helper cache <time-limit-of-cache>
进行缓存。但在超时后,您仍然需要以某种方式将密码放回剪贴板。git config credential.helper store
(不要使用 --global)永久存储它。这是未加密的。您可以打开文件并读取它。(例如,如果有人可以访问您的笔记本电脑,则可以使用可启动的 USB 读取密码(假设您的整个系统没有加密))。sudo apt-get install libsecret-1-0 libsecret-1-dev
sudo make --directory=/usr/share/doc/git/contrib/credential/libsecret
git config credential.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret
这允许以加密格式存储密码/个人访问令牌。如果您需要,git config
文件可以在本地repo的.git/config
文件中找到,如这里所示。
P.S. 有很多地方建议使用Gnome-keyring,但显然已经弃用了。
为多个帐户存储密码/PAT
这变得棘手,@VonC建议我们需要一个Git-Credential-Manager core
(GCM核心)。基于我在这个答案中的发现,这个答案得到了改进。
First install GCM core
sudo dpkg -i <path-to-package>
git-credential-manager-core configure
git config --global credential.credentialStore secretservice
as we use libsecret
Get latest git
In my case I had git 2.25 and got error error: unknown option 'show-scope'
. It appears that GCM core is using higher git
(atleast 2.26).
So install the latest and greatest git
as per here:
sudo add-apt-repository ppa:git-core/ppa
sudo apt-get update
apt list git # shows the latest git currently 2.31
sudo apt-get install git #or sudo apt-get upgrade
Update git remote path with username built in
GCM core needs this to identify the different accounts.:(
git remote set-url origin https://user1@github.com/user1/myRepo1.git
git remote set-url origin https://user2@github.com/user1/myRepo1.git
^^^^^
您的~/.gitconfig
文件将包含以下内容:
[credential]
helper = /usr/bin/git-credential-manager-core
credentialStore = secretservice
[credential "https://dev.azure.com"]
useHttpPath = true
git config --global credential.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret erase
命令,然后保存新的 token。我已经在使用 libsecret 了。 - rvimieirogit remote set-url origin https://username:your-personal-access-token@github.com/username/repo.git
。 - iKev61.envrc
(https://direnv.net/) 加载它们。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
设置为我的环境变量。my-encrypted-vars.ssh
放在 .git
中,以避免将其检入源代码。 - CervEd尝试启用此功能以帮助在推送/拉取时实现持久性
git config credential.helper store
对于持续克隆repo / macOS用户 / 安装iTerm2 https://iterm2.com/
启用工具栏
需要时只需点击片段。 附:您在使用oh-my-zsh吗? https://github.com/ohmyzsh/ohmyzsh
git config --global credential.helper cache
git config --global credential.helper 'cache --timeout=3600'
请查看以下 Github 页面:
这不是一个永久的商店,据其他评论所说,凭证不应存储为明文,这是一种安全风险。我使用一个密码管理器(https://bitwarden.com/)来存储PAT(个人访问令牌),然后在第一次使用时将其复制进去,然后缓存。如果您在Github帐户上启用了2FA,则需要PAT。https://docs.github.com/en/github/using-git/caching-your-github-credentials-in-git
git config --global url."https://<username>:<github-token>@github.com/".insteadOf "https://github.com/
现在,每次调用 github 都会自动附加您的凭据。environment-variables.bat
的批处理文件(记住,我在Windows上),它设置了包括访问令牌在内的所有必需的环境变量。2. 我在我的构建脚本和批处理文件中调用它来运行我的测试。3. environment-variables.bat
在源代码控制中被忽略。4. 但是,在源代码控制中,有一个名为environment-variables.bat.sample
的文件,其中包含相同的内容,但是使用虚假的令牌/密码。因此,我只需将此文件重命名为environment-variables.bat
,将虚假密码替换为真实密码,一切都可以正常工作。
虽然这并不是所有情况下的完美解决方案。
在我的项目中,我遇到了一个问题,即未来需要使用更多 tokens/passwords 来处理更多的 API。
因此,在我的 environment-variables.bat
中的 tokens 数量将会增加,这将使潜在的贡献者很难实际执行所有集成测试。而且,我仍然不知道该如何处理这个问题。