每次我尝试向我的repo推送时,git都要求输入用户名和密码
。
我每次重新输入密码都没有问题,但问题是输入用户名。我使用https
来克隆我的存储库。
那么,如何配置git,使其不会在每次git push
时要求输入用户名
。
我是Linux新手,但如果我没记错,在Windows中git push
只需要输入密码。
每次我尝试向我的repo推送时,git都要求输入用户名和密码
。
我每次重新输入密码都没有问题,但问题是输入用户名。我使用https
来克隆我的存储库。
那么,如何配置git,使其不会在每次git push
时要求输入用户名
。
我是Linux新手,但如果我没记错,在Windows中git push
只需要输入密码。
最简单的方法是创建一个包含以下内容的~/.netrc
文件:
machine github.com
login YOUR_GITHUB_USERNAME
password YOUR_GITHUB_PASSWORD
(如此显示:https://gist.github.com/ahoward/2885020)
您甚至可以通过键入以下内容关闭此文件的权限,以使任何人都无法读取您的密码:
chmod 600 ~/.netrc
当我只执行 git pull 命令, 或者 git pull origin xxx 命令时,Git会要求输入用户名和密码。
git config credential.helper store
它起作用了。
git config credential.helper store
git push https://github.com/xxx.git
然后输入您的用户名和密码。
完成
git push https://github.com/xxx.git
而不是 git push/push https://github.com/xxx.git
。 - Mohsin使用个人访问令牌而非密码:
请注意,从2021年8月13日开始,Github将不再接受账户密码进行Git操作的身份验证,并要求在GitHub.com上进行所有经过身份验证的Git操作时使用基于令牌的身份验证。您也可以继续使用SSH密钥。
因此,对于给定身份验证上下文的静态用户名配置(在使用Github时),您必须使用以下方式:
https://username:<personal-access-tokens>@repository-url.com
git push https://blueray453:<personal-access-token>@github.com/blueray453/window-application-calls
请注意,我们正在使用<personal-access-tokens>
而不是<password>
有关如何创建个人访问令牌的更多详细信息,请查看documentation。简而言之,文档中说的是,您需要前往https://github.com/settings/tokens并点击Generate new token (classic)
,在那里选择public_repo
用于公共存储库或repo
用于私有存储库。然后将该密钥用作密码。
为什么仅限于以下三种解决方案:
GitHub documentation中提到:
通过HTTPS访问GitHub... 每次您使用Git与GitHub进行身份验证时,除非您使用凭据助手缓存它们,否则将提示您输入凭据以进行GitHub身份验证。github 推荐 使用HTTPS进行连接。可能是因为他们希望你使用 Git凭证管理器。我没有使用过它,所以将留给有经验的人来处理。
这里,我只会提供Git和SSH解决方案。Git的文档讨论了如何避免反复输入相同的凭证,可以使用gitcredentials。
解决方案1
使用凭证助手缓存密码(在内存中短暂保存)。
git config --global credential.helper cache
现在运行:
git push https://username:<personal-access-tokens>@repository-url.com
之后,只需运行git push
即可。
解决方案2
使用凭据助手将密码存储在磁盘上(永久存储)。
git config --global credential.helper 'store --file ~/.my-credentials'
你可以在文档中找到凭证将保存的位置(如果没有使用 --file 参数明确设置)。
现在运行:
git push https://username:<personal-access-tokens>@repository-url.com
git push
即可。~/.my-credentials
的内容将如下所示:https://username:<personal-access-tokens>@repository-url.com
注意:
为了解决concern,即gitcredentials将凭据完全未加密("as is"),您始终可以在使用之前对文件进行加密和解密。
解决方案 3
这几乎是直接从生成新的SSH密钥并将其添加到ssh-agent复制粘贴过来的。
使用ssh-keygen -t ed25519 -C "your_email@example.com"
创建SSH密钥。如果您使用的是不支持Ed25519算法的旧系统,请使用:ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
在生成SSH密钥对后,将公钥添加到您的GitHub帐户中。请访问https://github.com/settings/ssh/new
如果尚未启动,请使用$ eval "$(ssh-agent -s)"
在后台启动ssh-agent
使用$ ssh-add ~/.ssh/id_ed25519
将您的SSH私钥添加到ssh-agent中。如果您使用了不同的名称创建密钥,或者要添加的现有密钥具有不同的名称,请在命令中将id_ed25519替换为您的私钥文件名。
测试SSH密钥:ssh -T git@github.com
进入本地克隆的存储库目录(如果您还没有进入),然后运行:git remote set-url origin git@github.com:username/your-repository.git
。现在,git push
应该可以正常工作。
ssh
+ key authentication
比https
+ credential.helper
更加可靠。
您可以按照以下方式配置为在所有存储库中使用SSH而不是HTTPS:
git config --global url.ssh://git@github.com/.insteadOf https://github.com/
url.<base>.insteadOf
在这里有文档记录 这里。
在工作目录中获取您的git url:
git config remote.origin.url
git config credential.helper store
git push "url of your git"
将仅提示输入一次用户名和密码。
完成。
自 2021 年 8 月 13 日起,不再接受基于密码的登录。现在,您需要使用令牌/SSH-based 身份验证。
如果您不想每次推送时都被要求提供凭据,请安装 GitHub CLI ,然后缓存您的凭据:
git config --global credential.helper store
gh auth login
按照提示进行操作。但要确保在 Authenticate Git with your GitHub credentials?
处选择 Y
。
git clone user_name@example.gitrepo.com/my_repo.git