Too*_*ooL 1327 authentication github git-pull git-push git-clone
我从我的Github帐户克隆了一个git存储库到我的电脑.
我想使用我的PC和笔记本电脑,但有一个Github帐户.
当我尝试使用我的PC从Github推送或拉出时,它需要用户名和密码,但在使用笔记本电脑时则不需要!
每次与原点交互时,我都不想输入用户名和密码.我在这里缺少什么?
Tek*_*kub 1842
常见的错误是使用默认值(HTTPS)而不是SSH进行克隆.您可以转到存储库,单击"克隆或下载",然后单击URL字段上方的"使用SSH"按钮并更新原始远程URL,如下所示:
git remote set-url origin git@github.com:username/repo.git
Run Code Online (Sandbox Code Playgroud)
这在GitHub中有记录:将远程URL从HTTPS切换到SSH.
Jay*_*tel 327
运行以下命令以启用凭据缓存:
$ 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>
Run Code Online (Sandbox Code Playgroud)
使用时还应指定缓存过期,
git config --global credential.helper 'cache --timeout 7200'
Run Code Online (Sandbox Code Playgroud)
启用凭据缓存后,它将缓存7200秒(2小时).
小智 128
我遇到了同样的问题,我发现最简单的解决方案是使用SSH URL而不是HTTPS URL:
ssh://git@github.com/username/repo.git
Run Code Online (Sandbox Code Playgroud)
而不是这个:
https://github.com/username/repo.git
Run Code Online (Sandbox Code Playgroud)
您现在可以只用username
而不是password
和验证username
.
rin*_*ius 114
除了更改为SSH之外,如果您不介意以明文形式输入密码,也可以继续使用HTTPS.把它放在你的~/.netrc
,它不会要求你的用户名/密码(至少在Linux和Mac上):
machine github.com
login <user>
password <password>
Run Code Online (Sandbox Code Playgroud)
添加(参见VonC的第2条评论):在Windows上,文件名是%HOME%\_netrc
.
如果您想要加密,还请阅读VonC的第一条评论.
另一个补充(参见user137717的评论),如果你有git 1.7.10或更新版本,你可以使用.
如果您使用HTTPS克隆GitHub存储库,您可以使用凭据帮助程序告诉Git每次与GitHub交谈时都记住您的GitHub用户名和密码.
这也适用于Linux,Mac和Windows.
Dav*_*ide 82
对于那些对以前的答案感到困惑的外行人,您可以这样做:
yourname/yourrepo.git
会有类似的回应
git remote -v
Run Code Online (Sandbox Code Playgroud)
那么你可以运行许多其他人建议的命令,但是现在你知道你的名字和你的回复,所以你可以yourname/yourrepo.git
从上面剪切和粘贴到
origin https://yourname@github.com/yourname/yourrepo.git (fetch)
origin https://yourname@github.com/yourname/yourrepo.git (push)
Run Code Online (Sandbox Code Playgroud)
小智 55
如果您使用ssh和您的私有密钥使用密码加密,那么你还是会被提示输入私钥当你做网络操作使用Git来输入密码/密码一样push
,pull
和fetch
.
如果您希望每次都不必输入密码,您可以使用ssh-agent
每个终端会话存储一次私钥密码凭证,正如我在回答"无法打开与您的身份验证代理的连接"中所述:
$ eval `ssh-agent -s`
$ ssh-add
Run Code Online (Sandbox Code Playgroud)
在Windows msysgit Bash中,您需要评估输出ssh-agent
,但我不确定您是否需要在其他开发环境和操作系统中执行相同操作.
ssh-add
在主.ssh
文件夹中查找名为的私钥id_rsa
,这是默认名称,但您可以将文件路径传递给具有不同名称的密钥.
完成终端会话后,可以ssh-agent
使用kill标志关闭-k
:
$ ssh-agent -k
Run Code Online (Sandbox Code Playgroud)
如ssh-agent
手册中所述:
Run Code Online (Sandbox Code Playgroud)-k
终止当前代理(由SSH_AGENT_PID环境变量给出).
此外,它可以采取可选的超时参数,如下所示:
$ ssh-add -t <timeout>
Run Code Online (Sandbox Code Playgroud)
其中<timeout>
的格式为<n>h
用于<n>
小时,<n>m
为<n>
分钟,等等.
根据ssh-agent
手册:
Run Code Online (Sandbox Code Playgroud)-t life
为添加到代理的身份的最长生命周期设置默认值.生命周期可以以秒或以sshd_config(5)中指定的时间格式指定.使用ssh-add(1)为标识指定的生命周期会覆盖此值.如果没有此选项,则默认的最长生命周期是永
在Cygwin中使用ssh-agent时,Cygwin用户应该意识到潜在的安全风险:
人们应该认识到cygwin [1]下ssh-agent的潜在危险,虽然在本地netstat和远程portscan下,似乎没有人可以访问/ tmp/ssh-foo中指定的端口......?
Run Code Online (Sandbox Code Playgroud)[1]: http://www.cygwin.com/ml/cygwin/2001-01/msg00063.html
在引用的链接:
但请注意,cygwin的unix域套接字是基本不可靠的 ,所以我强烈地 在cygwin下发现ssh-agent的使用.
当您在cygwin下运行ssh-agent时,它会在
/tmp/ssh-$USERNAME/
目录中创建AF_UNIX套接字 .在cygwin下AF_UNIX套接字通过AF_INET套接字模拟.你可以很容易地看到,如果你/tmp/ssh-$USERNAME/agent-socket-*
通过记事本查看 文件.你会看到类似的东西Run Code Online (Sandbox Code Playgroud)!<socket >2080
然后跑步
netstat -a
和惊喜!你有一些程序在监听端口2080.它是ssh-agent.当ssh从服务器接收RSA质询时,它指的是相应的/tmp/ssh-$USERNAME/agent-socket-*
(在cygwin下,在我们的例子中,这意味着它将打开连接localhost:2080
)并要求ssh-agent用它拥有的私钥处理RSA质询,然后它只是通过响应从ssh-agent收到服务器.在unix下,这样的场景没有问题,因为当程序试图访问AF_UNIX套接字时,unix内核会检查权限.但是,对于AF_INET套接字,连接是匿名的(读为"不安全").想象一下,你运行了cygwin ssh-agent.恶意黑客可以端口扫描您的盒子,找到ssh-agent使用的开放端口,打开与ssh服务器的连接,从它接收RSA质询,通过他找到的开放端口将其发送到您的ssh-agent,接收RSA响应,发送给ssh服务器和瞧,他成功登录到您的服务器.
Sag*_*she 47
来源:设置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)
Run Code Online (Sandbox Code Playgroud)
Eri*_*ang 33
当您使用https进行git pull&push时,只需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
ssh
我认为使用ssh
协议是一个更好的解决方案https
,即使设置步骤稍微复杂一点.
粗略的步骤:
ssh-keygen
在linux上,在Windows上msysgit
提供类似的命令.~/.ssh
.并通过ssh-add
命令将其添加到ssh代理.remote.origin.url
将git存储库更改为ssh
样式,例如git@gitlab.com:myaccount/myrepo.git
提示:
https
和ssh
协议.只需更改remote.origin.url
就足够了,或者您可以repo_home/.git/config
直接编辑以更改值(例如vi
在Linux上使用).
通常我会为每个协议添加一行,并使用其中一个注释掉#
.
例如
[remote "origin"] url = git@gitlab.com:myaccount/myrepo.git # url = https://myaccount@gitlab.com/myaccount/myrepo.git fetch = +refs/heads/*:refs/remotes/origin/*
K M*_*lam 15
您可以在Git中缓存GitHub密码:
只需按照github 官方文档中的说明操作即可.
按照上述链接中的说明操作后,您应该可以在不每次输入用户名/密码的情况下向/从您的仓库进行推/拉.
Adi*_*l B 15
如果您在 Github 帐户上启用了 2FA,您的常规密码将无法用于此目的,但您可以生成个人访问令牌并在其位置使用它。
访问GitHub 中的Settings
-> Developer Settings
->Personal Access Tokens
页面(https://github.com/settings/tokens/new),并生成具有所有Repo权限的新 Token :
然后页面将显示新的令牌值。保存此值并在推送到 GitHub 上的存储库时使用它代替密码:
> git push origin develop
Username for 'https://github.com': <your username>
Password for 'https://<your username>@github.com': <your personal access token>
Run Code Online (Sandbox Code Playgroud)
Ori*_*rif 14
Settings
-> Developer Settings
-> Personal Access Tokens
GitHub页面(https://github.com/settings/tokens/new),生成一个具有所有Repo权限的新Tokengithub.com
-> 单击Show password
然后粘贴token
您刚刚复制的内容。对我.git/config
有用的是编辑和使用
[remote "origin"]
url = https://<login>:<password>@gitlab.com(...).git
Run Code Online (Sandbox Code Playgroud)
毫无疑问,这是一种存储密码的不安全方式,但有些环境/情况可能不是问题.
小智 9
如果 SSH 密钥或.netrc
文件对您不起作用,那么另一个简单但安全性较低的解决方案是git-credential-store - 在磁盘上存储凭据的助手:
git config --global credential.helper store
Run Code Online (Sandbox Code Playgroud)
默认情况下,凭据将保存在文件中~/.git-credentials
。它将被创建和写入。
请注意,使用此帮助程序会将未加密的密码存储在磁盘上,仅受文件系统权限保护。如果这可能不是可接受的安全权衡。
这是另一种选择:
而不是写作
git push origin HEAD
Run Code Online (Sandbox Code Playgroud)
你可以写:
git push https://user:pass@yourrepo.com/path HEAD
Run Code Online (Sandbox Code Playgroud)
显然,对于大多数shell,这将导致密码在历史记录中被缓存,因此请记住这一点.
直接更新你的 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/*
Run Code Online (Sandbox Code Playgroud)
使用以下格式更改 SSH 的 URL 部分:
url = git@github.com:happy_feet/my_code.git
Run Code Online (Sandbox Code Playgroud)
(以上格式不会随GitHub或Bitbucket等各种Git远程服务器而改变。如果您使用Git进行版本控制,则相同):
注意:连接到远程 Git 存储库的 SSH 方式将要求您将公共 SSH 密钥添加到您的 Git 远程服务器(如 GitHub 或 Bitbucket。在设置页面中搜索 SSH 密钥)。
要了解如何生成 SSH 密钥,请参阅: 创建 SSH 密钥
这是我的工作解决方案,它也通过了 2FA。
git remote set-url origin https://{yourAccountName}:{theCopiedToken}@github.com/{yourAccountName}/{repositoryName}.git
现在您已登录并可以推/拉等。
你基本上有两个选择。
如果您在两台计算机上使用相同的用户,则需要将 .pub 密钥复制到您的 PC,以便 GitHub 知道您是同一用户。
如果您已为 PC 创建了新的 .pub 文件并希望将计算机视为不同的用户,则需要在 GitHub 网站上注册新的 .pub 文件。
如果这仍然不起作用,可能是因为 ssh 配置不正确,并且 ssh 无法找到密钥的位置。尝试
ssh -vv username@github.com
Run Code Online (Sandbox Code Playgroud)
获取 SSH 失败原因的更多信息。
小智 5
我遇到过同样的问题。
所以我.git/config
从我的项目中更改了文件,
url = https://github.com/<your-user-here>/<your-repo-here>
Run Code Online (Sandbox Code Playgroud)
到
url = git@github.com:<your-user-here>/<your-repo-here>
Run Code Online (Sandbox Code Playgroud)
并将SSH公钥添加到设置中的Git配置文件中。
对于 SSH 公钥:
cat ~/.ssh/id_rsa.pub
Run Code Online (Sandbox Code Playgroud)
对于Windows Git用户,运行git config --global credential.helper store
后如果还是提示输入密码,最好检查一下配置文件写到哪里了,用这个命令
git config --list --show-origin
Run Code Online (Sandbox Code Playgroud)
就我而言,在手动编辑配置文件 'C:\Program Files\Git\mingw64\etc\gitconfig' 并添加以下文本后,它起作用了。
[credential]
helper = store
Run Code Online (Sandbox Code Playgroud)
小智 5
这对我有用:
git remote set-url origin https://username@github.com/username/reponame.git
Run Code Online (Sandbox Code Playgroud)
例子:
git remote set-url origin https://jsmith@github.com/jsmith/master.git
Run Code Online (Sandbox Code Playgroud)
如果您克隆了 HTTPS 而不是 SSH,并且在拉、推和获取时遇到用户名和密码提示的问题。你可以简单地为 UBUNTU 解决这个问题
第一步:移动到根目录
cd ~/
Run Code Online (Sandbox Code Playgroud)
创建一个文件 .git-credentials
这些内容加进该文件与你usename
password
和githosting URL
https://user:pass@example.com
Run Code Online (Sandbox Code Playgroud)
然后执行命令
git config --global credential.helper store
Run Code Online (Sandbox Code Playgroud)
现在,您将能够毫不费力地从您的存储库中拉取并获取所有详细信息。
列出您当前的 SSH 密钥:
ls -l ~/.ssh
Run Code Online (Sandbox Code Playgroud)
生成新的 SSH 密钥:
ssh-keygen -t ed25519 -C "your_email@example.com"
Run Code Online (Sandbox Code Playgroud)
您应该将其中替换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
Run Code Online (Sandbox Code Playgroud)
文件id_ed25519
和id_ed25519.pub
现在应该已添加。
在后台启动 ssh-agent:
eval $(ssh-agent -s)
Run Code Online (Sandbox Code Playgroud)
将您的 SSH 私钥添加到 ssh-agent:
ssh-add ~/.ssh/id_ed25519
Run Code Online (Sandbox Code Playgroud)
接下来将公钥输出到终端屏幕:
cat ~/.ssh/id_ed25519.pub
Run Code Online (Sandbox Code Playgroud)
将输出复制到剪贴板 ( Ctrl+ Insert)。
转至https://github.com/<your-github-username>
并使用您的用户名和密码登录。
单击右上角的 GitHub 头像,然后单击“设置”。在左侧窗格中单击SSH 和 GPG 密钥。单击绿色按钮New SSH key
并将公共 SSH 密钥粘贴到标记为Key的文本区域中。使用描述性标题来告诉您将在哪台计算机上使用此 SSH 密钥。单击添加 SSH 密钥。
如果您当前的本地存储库是使用http和username创建的,则需要重新创建它以便与 SSH 兼容。
首先检查以确保您有一个干净的工作树,这样您就不会丢失任何工作:
git status
Run Code Online (Sandbox Code Playgroud)
然后cd ..
到父目录和rm -fr <name-of-your-repo>
.
最后克隆一个使用 SSH 而不是用户名/密码的新副本:
git clone git@github.com:[your-github-username]/[repository-name].git
Run Code Online (Sandbox Code Playgroud)
参考资料:
https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/generate-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
归档时间: |
|
查看次数: |
1129127 次 |
最近记录: |