我从我的 GitHub 帐户克隆了一个 Git 存储库到我的 PC。
我想同时使用我的 PC 和笔记本电脑,但只有一个 GitHub 帐户。
当我尝试使用我的 PC 向 GitHub 推送或从 GitHub 拉取时,它需要用户名和密码,但在我使用笔记本电脑时不需要!
我不想每次与 origin
互动时都输入我的用户名和密码。我在这里错过了什么?
一个常见原因是使用默认 (HTTPS) 而不是 SSH 进行克隆。您可以通过转到您的存储库,单击“克隆或下载”,然后单击 URL 字段上方的“使用 SSH”按钮并更新源远程的 URL 来纠正此问题,如下所示:
git remote set-url origin git@github.com:username/repo.git
您可以使用以下命令检查是否已将远程添加为 HTTPS 或 SSH:
git remote -v
这记录在 GitHub:Switching remote URLs from HTTPS to SSH。
使用 Git 存储库进行永久身份验证
运行以下命令以启用 credential caching:
$ git config credential.helper store
$ git push https://github.com/owner/repo.git
Username for 'https://github.com': <USERNAME>
Password for 'https://USERNAME@github.com': <PASSWORD>
您还应该指定缓存过期,
git config --global credential.helper 'cache --timeout 7200'
启用凭证缓存后,将缓存 7200 秒(2 小时)。
git remote set-url origin https://git@github.com/<owner>/<repo>.git
更改您的远程 URL 这也适用于 2FA
git config
命令应该相互匹配。一个引用--global
,另一个不直接复制粘贴,没有达到预期的效果。
我刚刚遇到了同样的问题,我发现最简单的解决方案是使用 SSH URL 而不是 HTTPS:
ssh://git@github.com/username/repo.git
而不是这个:
https://github.com/username/repo.git
您现在可以使用 just 验证 SSH 密钥而不是 username
和 password
。
git remote -v
查看当前 URL 并用 git remote set-url --push origin
设置它们
除了更改为 SSH,如果您不介意以明文形式输入密码,您还可以继续使用 HTTPS。将它放在您的 ~/.netrc
中,它不会询问您的用户名/密码(至少在 Linux 和 Mac 上):
machine github.com
login <user>
password <password>
添加(参见 VonC 的第二条评论):在 Windows 上,文件名为 %HOME%\_netrc
。
如果您想加密,还请阅读 VonC 的第一条评论。
如果您有 Git 1.7.10 或更高版本,则可以使用另一个附加功能(请参阅 user137717 的评论)。
Cache your GitHub password in Git using a credential helper:
如果您使用 HTTPS 克隆 GitHub 存储库,则可以使用凭证助手告诉 Git 在每次与 GitHub 通信时记住您的 GitHub 用户名和密码。
这也适用于 Linux、Mac 和 Windows。
%HOME%\_netrc
(而不是 ~/.netrc
)。另请参阅 stackoverflow.com/a/18362082/6309 以加密该文件。
对于对前面的答案感到困惑的外行,您可以这样做:
git remote -v
这将回应类似
origin https://yourname@github.com/yourname/yourrepo.git (fetch)
origin https://yourname@github.com/yourname/yourrepo.git (push)
然后您可以运行许多其他人建议的命令,但是现在您从上面知道了您的姓名和您的仓库,因此您可以将上面的 yourname/yourrepo.git
剪切并粘贴到:
git remote set-url origin git@github.com:yourname/yourrepo.git
paraphrase
。我没有设置一个
yourrepo
?
git remote -v
看看结果如何
https://github.com/orgname/yourrepo.git
如果您使用 SSH 并且您的私钥使用密码加密,那么当您使用 Git 进行网络操作时,系统仍会提示您输入私钥的密码/密码 像 push
、pull
和 fetch
。
使用 ssh-agent 保存私钥密码/密码凭证
如果您想避免每次都输入密码,您可以使用 ssh-agent
在每个终端会话中存储一次您的私钥密码凭据,正如我在 my answer to Could not open a connection to your authentication agent 中解释的那样:
$ eval `ssh-agent -s`
$ ssh-add
在 Windows msysgit Bash 中,您需要评估 ssh-agent
的输出,但我不确定您是否需要在其他开发环境和操作系统中执行相同操作。
ssh-add
在您的主 .ssh
文件夹中查找名为 id_rsa
的私钥,这是默认名称,但您可以将文件路径传递给具有不同名称的密钥。
杀死代理
完成终端会话后,您可以使用终止标志 -k
关闭 ssh-agent
:
$ ssh-agent -k
如 ssh-agent
manual 中所述:
-k 终止当前代理(由 SSH_AGENT_PID 环境变量提供)。
可选超时
此外,它可以采用一个可选的超时参数,如下所示:
$ ssh-add -t <timeout>
其中 <timeout>
的格式为 <n>h
表示 <n>
小时,<n>m
表示 <n>
分钟,依此类推。
-t life 为添加到代理的身份的最长生命周期设置默认值。生命周期可以以秒或 sshd_config(5) 中指定的时间格式指定。使用 ssh-add(1) 为身份指定的生命周期会覆盖此值。如果没有这个选项,默认的最大生命周期是永远。
See this page for more time formats。
Cygwin 用户的安全警告
Cygwin 用户应了解 potential security risk with using ssh-agent in Cygwin:
人们应该认识到 ssh-agent 在 Cygwin 1 下的潜在危险,尽管在本地 netstat 和远程 portscan 下似乎没有人可以访问 /tmp/ssh-foo 中指定的端口...? [1]:http://www.cygwin.com/ml/cygwin/2001-01/msg00063.html
在 cited link:
但是,请注意 Cygwin 的 Unix 域套接字根本不安全,因此我强烈反对在 Cygwin 下使用 ssh-agent。当您在 Cygwin 下运行 ssh-agent 时,它会在 /tmp/ssh-$USERNAME/ 目录中创建 AF_UNIX 套接字。在 Cygwin 下,AF_UNIX 套接字通过 AF_INET 套接字进行模拟。如果您通过记事本查看 /tmp/ssh-$USERNAME/agent-socket-* 文件,您可以很容易地看到这一点。您会看到类似 !
git push
时,它仍然要求输入密码,{2等,我不喜欢存储凭据的建议,尽管它是我的个人机器(作为最佳实践)。 ssh-add
的这个建议确实有帮助。谢谢 :)
来源:Set Up Git
以下命令会将您的密码保存在内存中一段时间(对于 Git 1.7.10 或更高版本)。
$ git config --global credential.helper cache
# Set git to use the credential memory cache
$ git config --global credential.helper 'cache --timeout=3600'
# Set the cache to timeout after one hour (setting is in seconds)
当你使用 https 进行 Git pull &推送,只需为您的项目配置 remote.origin.url
,以避免每次推送时输入用户名(或/和密码)。
如何配置remote.origin.url
:
URL format: https://{username:password@}github.com/{owner}/{repo} Parameters in URL: * username
Optional, the username to use when needed.
authentication, if specified, no need to enter username again when need authentication. Don't use email; use your username that has no "@", otherwise the URL can't be parsed correctly, * password optional, the password to use when need authentication. If specified, there isn't any need to enter the password again when needing authentication. Tip: this value is stored as plain text, so for security concerns, don't specify this parameter, * e.g git config remote.origin.url https://eric@github.com/eric/myproject
@Update - 使用 ssh
我认为使用 ssh
协议是比 https
更好的解决方案,尽管设置步骤稍微复杂一些。
粗略的步骤:
使用命令创建 ssh 密钥,例如 Linux 上的 ssh-keygen,Windows 上的 msysgit 提供类似的命令。
将本地机器上的私钥保存在适当的位置,例如~/.ssh。并通过 ssh-add 命令将其添加到 ssh 代理。
将公钥上传到 Git 服务器。
将 Git 存储库的 remote.origin.url 更改为 ssh 样式,例如 git@gitlab.com:myaccount/myrepo.git
然后当拉或推时,就不需要输入用户名或密码了。
提示:
如果您的 ssh 密钥有密码,则默认情况下,您需要在每次重新启动计算机后首次使用该密钥时输入它。
@Update - 在 https 和 ssh 协议之间切换。
只需更改 remote.origin.url
就足够了,或者您可以直接编辑 repo_home/.git/config
以更改值(例如,在 Linux 上使用 vi
)。
通常我为每个协议添加一行,并使用 #
注释掉其中一个。
例如
[remote "origin"] url = git@gitlab.com:myaccount/myrepo.git # url = https://myaccount@gitlab.com/myaccount/myrepo.git fetch = +refs/heads/*:refs/remotes/origin/*
git-fetch(1)
仅提及基于 git
/ssh
的 URL。
https://username:password@github.com/
中提供密码是安全的。请参阅stackoverflow.com/questions/4980912/…
你可以在 Git 中缓存你的 GitHub 密码:
只需按照 GitHub official documentation 中的说明进行操作即可。
按照上述链接中的说明进行操作后,您应该能够向/从您的存储库推/拉,而无需每次都输入您的用户名/密码。
如果您在 Github 帐户上启用了 2FA,则您的常规密码将无法用于此目的,但您可以生成个人访问令牌并使用它来代替。
访问 Settings
-> Developer Settings
-> GitHub (https://github.com/settings/tokens/new) 中的 Personal Access Tokens
页面,并生成具有所有 Repo 权限的新令牌:
https://i.stack.imgur.com/hEuYv.png
然后页面将显示新的令牌值。保存此值并在推送到 GitHub 上的存储库时使用它代替您的密码:
> git push origin develop
Username for 'https://github.com': <your username>
Password for 'https://<your username>@github.com': <your personal access token>
git config credential.helper store
,否则下次您与遥控器交互时,此令牌将不再存在。正确的?
这是另一种选择:
而不是写
git push origin HEAD
你可以写:
git push https://user:pass@yourrepo.com/path HEAD
显然,对于大多数 shell,这将导致密码被缓存在历史记录中,所以请记住这一点。
对我有用的是编辑 .git/config
并使用
[remote "origin"]
url = https://<login>:<password>@gitlab.com(...).git
不用说,这是一种不安全的密码存储方式,但在某些环境/情况下,这可能不是问题。
如果 SSH 密钥或 .netrc
文件不适合您,那么另一个简单但不太安全的解决方案可能适合您的是 git-credential-store - Helper to store credentials on disk:
git config --global credential.helper store
默认情况下,凭据将保存在文件 ~/.git-credentials
中。它将被创建并写入。
请注意,使用此帮助程序会将未加密的密码存储在磁盘上,仅受文件系统权限保护。如果这可能不是一个可接受的安全折衷方案。
我遇到过同样的问题。
所以我从我的项目中更改了 .git/config
文件,
url = https://github.com/<your-user-here>/<your-repo-here>
至
url = git@github.com:<your-user-here>/<your-repo-here>
并将 SSH 公钥添加到设置中的 Git 配置文件中。
对于 SSH 公钥:
cat ~/.ssh/id_rsa.pub
cat ~/.ssh/id_rsa.pub
获取 rsa 密钥并粘贴到 git web 门户设置中。
直接更新你的 Git 配置文件(如果你不想记住花哨的命令):
在您喜欢的文本编辑器中打开您的 .git/config
文件。它将位于您克隆的文件夹或执行 git init
的存储库中。进入该存储库。 .git
是一个隐藏文件夹,按 Ctrl + H 应该会显示隐藏文件夹(终端中的 ls -a
)。
以下是 .git/config
文件的示例。复制并粘贴这些行,并确保使用您的 Git 信息更新这些行。
[user]
name = Tux
email = tux@gmail.com
username = happy_feet
[remote "origin"]
url = https://github.com/happy_feet/my_code.git
fetch = +refs/heads/*:refs/remotes/origin/*
使用以下格式更改 SSH 的 URL 部分:
url = git@github.com:happy_feet/my_code.git
(上述格式不会随着各种Git远程服务器如GitHub或Bitbucket而改变。如果您使用Git进行版本控制,则相同):
注意:连接到远程 Git 存储库的 SSH 方式需要您将公共 SSH 密钥添加到 Git 远程服务器(如 GitHub 或 Bitbucket。在设置页面中搜索 SSH 密钥)。
要了解如何生成 SSH 密钥,请参阅: Creating SSH keys
如果您克隆了 HTTPS 而不是 SSH,并且在拉取、推送和获取时遇到用户名和密码提示问题。你可以简单地为 UBUNTU 解决这个问题
第一步:移动到根目录
cd ~/
创建一个文件 .git-credentials
与您一起将此内容添加到该文件中 usename
password
和 githosting URL
https://user:pass@example.com
然后执行命令
git config --global credential.helper store
现在,您将能够毫不费力地从您的 repo 中推送和获取所有详细信息。
githosting URL
是什么?例如,Gitlab
会是什么?
这对我有用:
git remote set-url origin https://username@github.com/username/reponame.git
例子:
git remote set-url origin https://jsmith@github.com/jsmith/master.git
对于 Windows Git 用户,运行 git config --global credential.helper store
后,如果仍然提示输入密码,您最好检查配置文件的写入位置,使用此命令
git config --list --show-origin
就我而言,在手动编辑配置文件“C:\Program Files\Git\mingw64\etc\gitconfig”并添加以下文本后,它就可以工作了。
[credential]
helper = store
你基本上有两个选择。
如果您在两台机器上使用相同的用户,则需要将 .pub 密钥复制到您的 PC,以便 GitHub 知道您是同一用户。
如果您已经为您的 PC 创建了一个新的 .pub 文件并希望将这些机器视为不同的用户,您需要在 GitHub 网站上注册新的 .pub 文件。
如果这仍然不起作用,可能是因为 ssh 配置不正确,并且 ssh 无法找到密钥的位置。尝试
ssh -vv username@github.com
获取更多信息为什么 SSH 失败。
HTTPS 更新:
GitHub 推出了一个适用于 Windows 的新程序,可在您使用 HTTPS 时存储您的凭据:
要使用:
从这里下载程序
运行程序后,它将编辑您的 .gitconfig 文件。重新检查它是否编辑了正确的 .gitconfig 以防你有几个。如果没有编辑正确的,请将以下内容添加到您的 .gitconfig [credential] helper = !'C:\\Path\\To\\Your\\Downloaded\\File\\git-credential-winstore.exe ' 注意 [credential] 之后的换行符。这是必需的。
打开您的命令行客户端并尝试 git push origin master 一次。如果它要求您输入密码,请输入密码,然后您就完成了。密码已保存!
您需要执行两个步骤 -
git remote remove origin git remote add origin git@github.com:NuggetAI/nugget.git
请注意,Git URL 是 SSH URL 而不是 HTTPS URL...您可以从这里选择:
https://i.stack.imgur.com/kmjwG.png
列出您当前的 SSH 密钥:
ls -l ~/.ssh
生成新的 SSH 密钥:
ssh-keygen -t ed25519 -C "your_email@example.com"
您应该将 your_email@example.com
替换为您的 GitHub 电子邮件地址。
当提示输入 Enter a file in which to save the key
时,按 Enter。
在 Enter passphrase (empty for no passphrase)
上 - 只需按 Enter(对于空密码)。
再次列出您的 SSH 密钥:
ls -l ~/.ssh
现在应该已添加文件 id_ed25519
和 id_ed25519.pub
。
在后台启动 ssh-agent:
eval $(ssh-agent -s)
将您的 SSH 私钥添加到 ssh-agent:
ssh-add ~/.ssh/id_ed25519
接下来将公钥输出到终端屏幕:
cat ~/.ssh/id_ed25519.pub
将输出复制到剪贴板 (Ctrl + Insert)。
转到 https://github.com/<your-github-username>
并使用您的用户名和密码登录。
点击您的 GitHub 头像在右上角,然后是设置。在左侧窗格中,单击 SSH 和 GPG 密钥。单击绿色按钮 New SSH key 并将公共 SSH 密钥粘贴到标有 Key 的文本区域中。使用描述性的标题,说明您将在哪台计算机上使用此 SSH 密钥。点击添加 SSH 密钥。
如果您当前的本地存储库是使用 http 和用户名创建的,则需要重新创建它以与 SSH 兼容。首先检查以确保您有一个干净的工作树,这样您就不会丢失任何工作:
git status
然后 cd ..
到父目录和 rm -fr <name-of-your-repo>
。
最后克隆一个使用 SSH 而不是用户名/密码的新副本:
git clone git@github.com:[your-github-username]/[repository-name].git
参考文献:
https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account
对于 Mac 操作系统
转到您的 Github 设置 -> 开发人员设置 -> GitHub 中的个人访问令牌页面(https://github.com/settings/tokens/new),并生成一个具有所有回购权限的新令牌 在您的 Mac 中搜索钥匙串访问 ->搜索 github.com -> 单击显示密码,然后粘贴您刚刚复制的令牌。转到 CLI,它将再次询问用户名和密码,输入您的 Github 用户名并将令牌粘贴为密码,您应该可以继续使用 CLI 的其余时间。
github.com
,那没关系,只需执行第 3 步,然后它就会显示在 Keychain Access 中。
截至 2021 年,HTTPS 远程有一个安全的、用户友好的跨平台解决方案。无需再输入密码!不再有 SSH 密钥!不再有个人访问令牌!
安装由 GitHub (downloads) 开发的Git Credential Manager。它支持对 GitHub、BitBucket、Azure 和 GitLab 的无密码浏览器内 OAuth 身份验证。这意味着您可以启用 two-factor authentication on GitHub 和其他平台,从而大大提高您帐户的安全性。
推送时,您可以选择身份验证方法:
> 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...
在 Linux 上,需要进行少量设置。以下将凭证在内存中缓存 20 小时,因此您每天最多必须进行一次身份验证。
git-credential-manager-core configure
git config --global credential.credentialStore cache
git config --global credential.cacheoptions "--timeout 72000"
熟悉 gnome-keyring 或 KWallet 的高级用户可能更喜欢 change the credential store 而不是 libsecret。
化妆品配置 (docs):
更喜欢在终端而不是在 GUI 中选择身份验证方法(点击次数较少)始终使用浏览器方法而不是每次都被询问(甚至更少的按键)
git config --global credential.guiPrompt false
git config --global credential.gitHubAuthModes browser
如果您在 Windows 下使用 Git(例如 Git Bash)(并且如果您不想从 HTTPS 切换到 SSH),您也可以使用 Git Credential Manager for Windows
此应用程序将为您保留用户名和密码...
正如许多用户所说,您只需将 Git 存储库 URL 从 HTTPS 更改为 SSH。
如果您还没有在您的机器中生成 SSH 密钥,那么您将不得不这样做。
作为附加信息,在进行此更改后,我仍然遇到相同的错误:
没有权限。
就我而言,问题在于我使用 Windows Shell 执行 ngh 命令;由于此命令应打开请求 SSH 短语的提示,而 Windows Shell 不会打开此类提示,因此身份验证失败。
所以,我只需要打开 Git shell 并在那里执行 ngh 命令,每次它要求它时将 SSH 短语放在提示符中,然后“voilà”......它工作得很好!
# gen the pub and priv keys
# use "strange" naming convention, because those WILL BE more than 10 ...
ssh-keygen -t rsa -b 4096 -C "me@corp.com" -f ~/.ssh/id_rsa.me@corp.com@`hostname -s`
# set the git alias ONLY this shell session
alias git='GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa.me@corp.com.`hostname -s`" git'
# who did what when and why
git log --pretty --format='%h %ai %<(15)%ae ::: %s'
# set the git msg
export git_msg='issue-123 my important commit msg'
# add all files ( danger !!! ) and commit them with the msg
git add --all ; git commit -m "$git_msg" --author "Me <me@corp.com"
# finally
git push
检查您的 git 版本并更新。然后将解决问题
$ git update-git-for-windows
更新说明:当您的 PC git 版本和服务器 git 版本不匹配时,我们可以使用它。
不定期副业成功案例分享
git remote set-url origin git://new.url.here
)git remote set-url origin https://name:password@github.com/repo.git
(extracted from a comment here)Permission denied (publickey)
:help.github.com/articles/generating-ssh-keys。