Git push需要用户名和密码

Git push需要用户名和密码,git,github,git-push,git-clone,git-pull,Git,Github,Git Push,Git Clone,Git Pull,我将Git存储库从我的GitHub帐户克隆到我的PC 我想同时使用我的PC和笔记本电脑,但只使用一个GitHub帐户 当我尝试使用我的PC推送或从GitHub拉出时,它需要用户名和密码,但在我使用笔记本电脑时则不需要 我不想每次与origin交互时都键入用户名和密码。我在这里遗漏了什么?一个常见的原因是使用默认(HTTPS)而不是SSH进行克隆。您可以通过转到您的存储库,单击“克隆或下载”,然后单击URL字段上方的“使用SSH”按钮,并更新您的源远程服务器的URL,如下所示: git remot

我将Git存储库从我的GitHub帐户克隆到我的PC

我想同时使用我的PC和笔记本电脑,但只使用一个GitHub帐户

当我尝试使用我的PC推送或从GitHub拉出时,它需要用户名和密码,但在我使用笔记本电脑时则不需要


我不想每次与
origin
交互时都键入用户名和密码。我在这里遗漏了什么?

一个常见的原因是使用默认(HTTPS)而不是SSH进行克隆。您可以通过转到您的存储库,单击“克隆或下载”,然后单击URL字段上方的“使用SSH”按钮,并更新您的源远程服务器的URL,如下所示:

git remote set-url origin git@github.com:username/repo.git

这在GitHub上有记录:。

您基本上有两种选择

如果您在两台计算机上使用同一用户,则需要将.pub密钥复制到您的PC,以便GitHub知道您是同一用户

如果您已经为PC创建了一个新的.pub文件,并且希望将计算机视为不同的用户,则需要在GitHub网站上注册新的.pub文件

如果这仍然不起作用,可能是因为ssh配置不正确,并且ssh无法找到密钥的位置。试一试

ssh -vv username@github.com

获取SSH失败的更多信息。

我遇到了同样的问题,我找到的最简单的解决方案是使用SSH URL而不是HTTPS URL:

ssh://git@github.com/username/repo.git
而不是这个:

https://github.com/username/repo.git

现在,您可以只使用SSH密钥而不是
用户名
密码

进行验证,除了更改为SSH之外,如果您不介意将密码以明文形式输入,还可以继续使用HTTPS。将其放入
~/.netrc
中,它不会要求您输入用户名/密码(至少在Linux和Mac上是这样):

machine github.com
登录
密码
添加(参见VonC的第二条评论):在Windows上,文件名是
%HOME%\\u netrc

如果要加密,请阅读VonC的第一条注释

另一个添加内容(请参阅用户137717的评论),如果您有Git 1.7.10或更新版本,则可以使用它

:

如果使用HTTPS克隆GitHub存储库,则可以使用 凭证助手,告诉Git记住您的GitHub用户名和 每次与GitHub对话时都输入密码


这也适用于Linux、Mac和Windows。

HTTPS更新:

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 GitHub为Windows启动了一个新程序,在您使用HTTPS时存储您的凭据:

使用:

  • 从下载程序

  • 运行程序后,它将编辑您的
    .gitconfig
    文件。重新检查它是否编辑了正确的
    .gitconfig
    ,以防您有多个。如果没有编辑正确的,请将以下内容添加到
    .gitconfig

    [credential]
        helper = !'C:\\Path\\To\\Your\\Downloaded\\File\\git-credential-winstore.exe'
    
    注意
    [credential]
    之后的换行符。这是必需的

  • 打开命令行客户端,尝试一次git push origin master。如果它要求您输入密码,请输入密码,您就完成了。密码已保存


如果您正在使用SSH,并且您的私钥是用密码短语加密的,那么当您使用Git进行网络操作时,仍然会提示您输入私钥的密码短语/密码,如
推送
拉取

使用ssh代理保存私钥密码短语/密码凭据 如果希望避免每次都必须输入密码短语,可以使用
ssh-agent
在每个终端会话中存储一次私钥密码短语凭据,如我在中所述:

$eval`ssh代理-s`
$ssh添加
在WindowsMSYSGitBash中,您需要评估
ssh代理的输出,但我不确定您是否需要在其他开发环境和操作系统中执行同样的操作

ssh add
在您的家庭
.ssh
文件夹中查找名为
id\u rsa
的私钥,这是默认名称,但您可以将文件路径传递给具有不同名称的密钥

杀死特工 完成终端会话后,可以使用kill标志
-k
关闭
ssh代理

$ssh代理-k
正如报告中所述:

杀死当前代理(由SSH\u agent\u PID环境变量提供)

可选超时 此外,它还可以采用可选的超时参数,如下所示:

$ssh add-t
其中
的格式为
h
表示
小时,
m
表示
分钟,依此类推

根据报告:

为添加到代理的标识的最大生存期设置默认值。寿命可以以秒或秒为单位指定 中指定的时间格式。为具有此值的标识指定的生存期将覆盖此值。如果没有此选项,默认的最大生存期为永久

Cygwin用户的安全警告 Cygwin用户应注意:

人们应该认识到这一点 Cygwin下ssh代理的潜在危险,尽管在本地netstat和远程端口扫描下,/tmp/ssh foo中指定的端口似乎不可供任何人访问

[1]: http://www.cygwin.com/ml/cygwin/2001-01/msg00063.html
以及

但是,请注意,Cygwin的Unix域套接字从根本上讲是不安全的,因此我强烈反对在Cygwin下使用ssh代理

在Cygwin下运行ssh代理时,它会在
/tmp/ssh-$USERNAME/
目录中创建AF_UNIX套接字。在Cygwin下,通过AF_INET套接字模拟AF_UNIX套接字。如果您通过记事本查看
/tmp/ssh-$USERNAME/agent socket-*
文件,就可以很容易地看到这一点。你会看到类似的东西

!<socket >2080
origin    https://yourname@github.com/yourname/yourrepo.git (fetch)
origin    https://yourname@github.com/yourname/yourrepo.git (push)
!2080
然后运行
netstat-a
和惊喜!您有一些程序正在监听端口2080。是ssh代理。当ssh从服务器接收到RSA质询时,它引用correspondin
!<socket >2080
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 [remote "origin"] url = git@gitlab.com:myaccount/myrepo.git # url = https://myaccount@gitlab.com/myaccount/myrepo.git fetch = +refs/heads/*:refs/remotes/origin/*
git remote -v
origin    https://yourname@github.com/yourname/yourrepo.git (fetch)
origin    https://yourname@github.com/yourname/yourrepo.git (push)
git remote set-url origin git@github.com:yourname/yourrepo.git
$ 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'
url = https://github.com/<your-user-here>/<your-repo-here>
url = git@github.com:<your-user-here>/<your-repo-here>
cat ~/.ssh/id_rsa.pub
[remote "origin"]
        url = https://<login>:<password>@gitlab.com(...).git
git config --global credential.helper store
git push origin HEAD
git push https://user:pass@yourrepo.com/path HEAD
git config --list --show-origin
[credential]
    helper = store
git remote set-url origin https://username@github.com/username/reponame.git
git remote set-url origin https://jsmith@github.com/jsmith/master.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/*
url = git@github.com:happy_feet/my_code.git
 # 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
cd ~/
https://user:pass@example.com
git config --global credential.helper store
> git push origin develop
Username for 'https://github.com': <your username>
Password for 'https://<your username>@github.com': <your personal access token>
ls -l ~/.ssh
ssh-keygen -t ed25519 -C "your_email@example.com"
ls -l ~/.ssh
eval $(ssh-agent -s)
ssh-add ~/.ssh/id_ed25519
cat ~/.ssh/id_ed25519.pub
git status
git clone git@github.com:[your-github-username]/[repository-name].git