我最近在GitHub上将我的存储库同步到https://(由于防火墙问题),每次都会要求输入密码。
有没有一种方法可以缓存凭据,而不是每次进行身份验证 git push
?
我最近在GitHub上将我的存储库同步到https://(由于防火墙问题),每次都会要求输入密码。
有没有一种方法可以缓存凭据,而不是每次进行身份验证 git push
?
您可以创建自己的个人API令牌(OAuth),并像使用普通凭据一样使用它(位于:/settings/tokens
)。例如:
git remote add fork https://4UTHT0KEN@github.com/foo/bar
git push fork
.netrc
另一种方法是在~/.netrc
(Windows上的_netrc
)中配置您的用户名/密码,例如:
machine github.com
login USERNAME
password PASSWORD
protocol https
为了在使用HTTPS时缓存您的GitHub密码, 您可以使用凭证助手来告诉Git每次与GitHub通信时记住您的GitHub用户名和密码。
git config --global credential.helper osxkeychain
(需要osxkeychain助手
),git config --global credential.helper wincred
git config --global credential.helper cache
相关文章:
如果您不想像Mark所说的那样以纯文本形式存储密码,可以在获取和推送时使用不同的GitHub URL。在配置文件中,在[remote“origin”]
下:
url = git://github.com/you/projectName.git
pushurl = git@github.com:you/projectName.git
对于开源项目,在推送时它仍会要求密码,但在获取时不需要。
你可以使用凭证辅助工具。
git config --global credential.helper 'cache --timeout=x'
其中x
代表秒数。
store
、cache
和其他常见的东西吗? - Notinlistrepo
后,您可以编辑repo/.git/config
文件,并添加以下配置:[user]
name = you_name
password = you_password
[credential]
helper = store
那么您将不会再被要求输入用户名
和密码
。
helper = manager
可以工作(但第一次推送时会要求输入用户名+仓库)。 - Stéphane Laurent我知道这不是一种安全的解决方案,但有时你只需要一个简单的解决方案-而无需安装其他任何东西。由于helper = store对我无效,所以我创建了一个虚拟的助手:
创建一个脚本并将其放在您的用户bin文件夹中,这里命名为credfake,此脚本将提供您的用户名和密码:
#!/bin/bash
while read line
do
echo "$line"
done < "/dev/stdin"
echo username=mahuser
echo password=MahSecret12345
使其可执行:
chmod u+x /home/mahuser/bin/credfake
接下来在 git 中进行配置:
git config --global credential.helper /home/mahuser/bin/credfake
(或者只在一个仓库中使用它,而不使用--global)
然后 - voilá - git将使用这个用户名和密码。
git remote -v
origin https://gitlab.com/username/Repo.git (fetch)
origin https://gitlab.com/username/Repo.git (push)
git push
时跳过用户名和密码,可以尝试以下方法: git remote set-url origin https://username:password@gitlab.com/username/Repo.git
我刚刚将包含密码在内的用户详细信息添加到了原始URL中。
注意:如果用户名是电子邮件ID,则无法正常工作。
git remote -v
origin https://username:password@gitlab.com/username/Repo.git (fetch)
origin https://username:password@gitlab.com/username/Repo.git (push)
ssh -F /path/to/your/config $*
GIT_SSH=/path/to/your/wrapper
。这里,git remote (origin) 必须使用git@github.com:user/project.git
格式。为了安全起见,最好使用凭据,但是您可以使用缓存来保留一段时间:
git config --global credential.helper cache
git config credential.helper 'cache --timeout=3600'
windowsservercore
的 Docker 容器内运行吗? - Peter Mortensen> git push
Select an authentication method for 'https://github.com/':
1. Web browser (default)
2. Device code
3. Personal access token
option (enter for default): 1
info: please complete authentication in your browser...
git-credential-manager-core configure
git config --global credential.credentialStore cache
git config --global credential.cacheoptions=--timeout 72000
熟悉 gnome-keyring 或 KWallet 的高级用户可能更喜欢将凭据存储更改为 libsecret。
外观配置:由于我总是在上面的提示中选择“网页浏览器”,因此我设置了一个gitHubAuthModes首选项以跳过选择。最近版本的 GCM 包括一个 GUI,它会在认证流程中添加一个额外的点击,我禁用它。
git config --global credential.gitHubAuthModes browser
git config --global credential.guiPrompt false
如果您像我一样使用双因素身份验证,那么情况会有些不同。由于我在其他地方没有找到好的答案,所以我将其放在这里,以便以后可以找到它。
如果您使用双因素身份验证,则指定用户名/密码将无法工作-您将无法访问。但是,您可以使用应用程序访问令牌并使用Git的凭据助手为您缓存。以下是相关链接:
我不记得在哪里看到过了,但当您被要求提供用户名时,那就是您放置应用程序访问令牌的地方。然后留空密码。它在我的Mac上运作良好。
_netrc
文件。请参见下面我的回答。我发现这比在Windows上有点不稳定的git-credential-winstore.exe
(内存缓存)更安全。 - VonC