Ran*_*Rag 601 linux git github
每当我试图推进我的repo git要求两者username & password.
我每次都重新输入密码没有问题,但问题是输入用户名.我https用来克隆我的存储库.
那么,我如何配置git以便它不会username在每个上面都要求git push.
我是linux的新手,但是Windows中的IIRC git push只要求输入密码.
Ale*_*rov 902
编辑(由主持人和评论建议的@ dk14)
警告:如果您使用credential.helper store答案,您的密码将完全未加密("按原样")存储在~/.git-credentials.请参阅下面的评论部分或"链接"部分的答案,特别是如果您的雇主对安全问题的容忍度为零.
理由:答案中描述的方法可能会引入一个主要漏洞,特别是对于运营私人财务数据的企业而言.结合较小的安全问题(通常存在于此)或最小的社交工程,它允许劫持项目网络中的大多数凭证,因为事先知道"裸"密码的位置.这可能包括部分访问生产环境的凭证,因此事实上提升了[内部]攻击者的访问权限并允许在某种程度上掩盖黑客的身份.
作为旁注(为了通过示例扩展此警告),尽管为员工提供了安全的虚拟环境,但几个大型企业项目似乎仍然存在这个问题......基本上我对摆脱$ git help credentialsauth的呼吁在这方面遭到拒绝 - 但是,在时间,我不认为可以将攻击(如credential.helper store)注入到Git上托管的辅助bash/build脚本中,以便将其传播到用户之间,甚至可能是匿名的(因为有些会自动同步).公平地说,这种黑客通常不会(至少)直接访问关键系统.除非是~/.git-credentials.
编辑#2(@ dk14):
即使被接受,它也没有回答实际OP关于省略用户名(不是密码)的问题.对于那些确切问题的读者来说,@ grawity的答案可能会派上用场.
原始答案(来自@Alexander Zhu):
您可以使用以下命令存储凭据
$ git config credential.helper store
$ git push http://example.com/repo.git
Username: <type your username>
Password: <type your password>
Run Code Online (Sandbox Code Playgroud)
另外我建议你阅读
$ git help credentials
小智 165
您可以在.git/config本地存储库的文件中完成此操作.此文件包含一个名为"remote"的部分,其中包含一个名为"url"的条目.'url'条目应包含您正在讨论的存储库的https链接.
当您使用您的用户名为主机'url'添加前缀时,git不应该再询问您的用户名.这是一个例子:
url = https://username@repository-url.com
Run Code Online (Sandbox Code Playgroud)
Jay*_*tel 106
运行以下命令以启用凭据缓存:
$ git config credential.helper store
$ git push https://github.com/repo.git
Username for 'https://github.com': <USERNAME>
Password for 'https://USERNAME@github.com': <PASSWORD>
Run Code Online (Sandbox Code Playgroud)
使用时还应指定缓存过期
git config --global credential.helper "cache --timeout 7200"
Run Code Online (Sandbox Code Playgroud)
启用凭据缓存后,它将缓存7200秒(2小时).
阅读凭证文件
$ git help credentials
Run Code Online (Sandbox Code Playgroud)
Luk*_*ski 53
如果Git仍然要求您输入用户名和密码,请尝试更改https://github.com/为git@github.com:远程URL:
$ git config remote.origin.url
https://github.com/dir/repo.git
$ git config remote.origin.url "git@github.com:dir/repo.git"
Run Code Online (Sandbox Code Playgroud)
Ben*_*Ben 43
我找到的最简单方法是使用此命令:
git config --global credential.https://github.com.username <your_username>
Run Code Online (Sandbox Code Playgroud)
这适用于逐站点并修改您的全局git配置.
要查看更改,请使用:
git config --global --edit
Run Code Online (Sandbox Code Playgroud)
小智 30
如果您使用的是https而不是ssh,则可以编辑"url",.git/config如user701648所示,但如果您需要,还可以添加密码:
url = https://username:password@repository-url.com
Run Code Online (Sandbox Code Playgroud)
Ala*_*met 21
您可以通过在Git配置文件中添加以下内容,为给定站点的所有存储库设置用户名.您需要将" https://example.com "和"我"更改为实际网址和实际用户名.
[credential "https://example.com"]
username = me
Run Code Online (Sandbox Code Playgroud)
(这直接来自"git help credentials")
Lay*_*Err 10
更改克隆的repo工作原理:
git remote set-url origin git@github.com:gitusername/projectname.git
Run Code Online (Sandbox Code Playgroud)
使用缓存凭据可以正常工作,并且设置超时时间可以减少烦恼.如果您使用Windows凭据帮助程序,那应该是最简单的方法(特别是在SSH被阻止的情况下).
除了user701648的答案,您还可以使用以下命令将凭据存储在主文件夹中(所有项目的全局),而不是项目文件夹
$ git config --global credential.helper store
$ git push http://example.com/repo.git
Username: <type your username>
Password: <type your password>
Run Code Online (Sandbox Code Playgroud)
当我只是git pull,git pull origin xxx时,git会询问用户名和密码.
git config credential.helper store
Run Code Online (Sandbox Code Playgroud)
有用.
最简单的方法是创建~/.netrc具有以下内容的文件:
machine github.com
login YOUR_GITHUB_USERNAME
password YOUR_GITHUB_PASSWORD
Run Code Online (Sandbox Code Playgroud)
(如此处所示:https : //gist.github.com/ahoward/2885020)
您甚至可以关闭该文件的权限,以便没人输入以下内容来读取密码:
chmod 600 ~/.netrc
Run Code Online (Sandbox Code Playgroud)
使用个人访问令牌而不是密码:
请注意,从 2021 年 8 月 13 日开始,github 在对 Git 操作进行身份验证时将不再接受帐户密码,并将要求对 GitHub.com 上所有经过身份验证的 Git 操作使用基于令牌的身份验证。您还可以根据需要继续使用 SSH 密钥。
因此,对于给定的身份验证上下文,您必须使用用户名的静态配置(使用 github 时):
https://username:<personal-access-tokens>@repository-url.com
Run Code Online (Sandbox Code Playgroud)
例如,要推送到https://github.com/blueray453/window-application-calls,您必须运行:
git push https://blueray453:<personal-access-token>@github.com/blueray453/window-application-calls
Run Code Online (Sandbox Code Playgroud)
请注意,我们正在使用<personal-access-tokens>而不是<password>
有关如何创建个人访问令牌的更多详细信息,请查看文档。总之,文档说的是,您必须转到https://github.com/settings/tokens并单击Generate new token (classic),然后选择public_repo公共存储库或repo私有存储库。然后使用该密钥作为密码。
为什么只有以下三种解决方案:
github文档说:
通过 HTTPS 的 GitHub... 每次使用 Git 向 GitHub 进行身份验证时,系统都会提示您输入凭据以向 GitHub 进行身份验证,除非您使用凭据助手缓存它们。
...
使用 SSH ... 每次使用 Git 向 GitHub 进行身份验证时,系统都会提示您输入 SSH 密钥密码,除非您已 存储密钥。
github建议通过 HTTPS 连接。可能是因为他们希望您使用 Git Credential Manager。我没有使用过它,所以将其留给有经验的人。
这里我只给出Git和SSH的解决方案。Git 的文档讨论了如何避免使用 gitcredentials 一遍又一遍地输入相同的凭据。
解决方案1
使用凭据助手来缓存密码(在内存中保存一小段时间)。
git config --global credential.helper cache
Run Code Online (Sandbox Code Playgroud)
现在运行:
git push https://username:<personal-access-tokens>@repository-url.com
Run Code Online (Sandbox Code Playgroud)
之后,只要跑步git push就足够了。
解决方案2
使用凭据助手来存储密码(无限期地存储在磁盘上)。
git config --global credential.helper 'store --file ~/.my-credentials'
Run Code Online (Sandbox Code Playgroud)
您可以在文档中找到凭证的保存位置(如果未使用 --file 显式设置)。
现在运行:
git push https://username:<personal-access-tokens>@repository-url.com
Run Code Online (Sandbox Code Playgroud)
之后,只要跑步git push就足够了。
在这种情况下, 的内容~/.my-credentials将如下所示:
https://username:<personal-access-tokens>@repository-url.com
Run Code Online (Sandbox Code Playgroud)
笔记:
为了解决gitcredentials 存储完全未加密的凭据(“按原样”)的问题,您始终可以加密文件并在使用前解密。
解决方案3
这几乎是直接复制粘贴生成新的 SSH 密钥并将其添加到 ssh-agent。
使用创建 ssh 密钥ssh-keygen -t ed25519 -C "your_email@example.com"。如果您使用的是不支持 Ed25519 算法的旧系统,请使用:ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
将 SSH 密钥添加到您在 GitHub 上的帐户。生成 SSH 密钥对后,必须将公钥添加到https://github.com/settings/ssh/new
如果未启动,请使用以下命令在后台启动 ssh-agent$ eval "$(ssh-agent -s)"
使用 来将您的 SSH 私钥添加到 ssh-agent 中$ ssh-add ~/.ssh/id_ed25519。如果您使用不同的名称创建了密钥,或者要添加具有不同名称的现有密钥,请将命令中的 id_ed25519 替换为您的私钥文件的名称。
测试 SSH 密钥:ssh -T git@github.com
将目录更改为存储库的本地克隆(如果您尚未在那里)并运行:git remote set-url origin git@github.com:username/your-repository.git。现在,git push希望能奏效。
如果您使用的是SSH版本,则密码不会有任何问题,只有一次生成SSH密钥(告诉git),这台 PC可以与此 github帐户一起使用,并且永远不会再询问任何有关此PC的访问权限。
自 2021 年 8 月 13 日起,不再接受基于密码的登录。您现在需要使用基于令牌/SSH 的身份验证。
如果您不想每次推送时都被询问,请安装GitHub CLI,然后缓存您的凭据:
git config --global credential.helper store
gh auth login
Run Code Online (Sandbox Code Playgroud)
并按照提示操作。但请务必检查Authenticate Git with your GitHub credentials?一下Y。
为了避免输入用户名,但仍被提示输入密码,那么您可以简单地克隆您的存储库,包括用户名:
git clone user_name@example.gitrepo.com/my_repo.git
Run Code Online (Sandbox Code Playgroud)
小智 5
在工作目录中获取 git 的 url :
git config remote.origin.url
Run Code Online (Sandbox Code Playgroud)
然后 :
git config credential.helper store
git push "url of your git"
Run Code Online (Sandbox Code Playgroud)
最后一次会询问用户名和密码
完毕。
ssh+key authentication比https+更可靠credential.helper
您可以将所有存储库配置为使用 SSH 而不是 HTTPS,如下所示:
git config --global url.ssh://git@github.com/.insteadOf https://github.com/
Run Code Online (Sandbox Code Playgroud)
url.<base>.insteadOf记录在这里。
| 归档时间: |
|
| 查看次数: |
392787 次 |
| 最近记录: |