Git push需要用户名和密码

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.

  • 如果您不能使用ssh进行安全限制(比如我),您可以这样做:`git remote set-url origin https:// name:password @ github.com/repo.git` [(摘自此处的评论)] (http://stackoverflow.com/questions/8588768/git-push-username-password-how-to-avoid) (126认同)
  • 为什么使用HTTPS克隆是一个常见的错误?GitHub现在建议使用HTTPS. (120认同)
  • 要弄清楚如何更改URL,请访问:http://stackoverflow.com/a/2432799/60488(剧透:`git remote set-url origin git:// new.url.here`) (61认同)
  • 使用本指南修复了我的"Permission denied(publickey)":https://help.github.com/articles/generating-ssh-keys/. (27认同)
  • @smftre默认情况下是这种情况,但你可以使用[helper来缓存你的凭据](http://git-scm.com/docs/git-credential-cache). (8认同)
  • 然后正如我所说,你的ssh键永远不会发挥作用.如果要使用ssh,则需要使用ssh clone url (4认同)
  • 您可以使用以下命令检查您的遥控器:git remote -v然后如果它不正确,您可以使用:git remote rm aliasname(如ex.的"origin")并使用以下命令重新添加:git remote add git @ github. com:xxxxx再次像这里提到的那样,https样式遥控器不会使用你的SSH密钥. (4认同)
  • @chharvey现在GitHub再次推荐HTTPS!请参阅https://help.github.com/articles/set-up-git/#next-steps-authenticating-to-github-from-git (4认同)
  • git还可以缓存您的用户名和密码.设置需要30秒.https://help.github.com/articles/caching-your-github-password-in-git/#platform-linux (3认同)
  • 发出带有相同问题的`git remote -v`返回`origin https:// github.com/myusername/my-repo-name.git`修复问题后返回`origin git@github.com:myusername/my- repo-name`(用于获取和推送).希望这有助于理解差异. (2认同)
  • 我还认为使用 https 克隆是一个坏主意,因为您添加了一个必须确保安全的额外机制,以允许拉/推而无需每次都输入通行证。通过 SSH,您就拥有了负责处理它的公钥基础设施。我不知道缓存机制有多安全,但如果 ssh 克隆给你带来了超能力,为什么还要使用它呢? (2认同)

Jay*_*tel 327

使用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>
Run Code Online (Sandbox Code Playgroud)

使用时还应指定缓存过期,

git config --global credential.helper 'cache --timeout 7200'
Run Code Online (Sandbox Code Playgroud)

启用凭据缓存后,它将缓存7200秒(2小时).

  • 这应该是公认的答案。它恰好回答了这个问题。 (7认同)
  • 很好的答案 - 有一个缺陷:提到的两个“git config”命令应该相互匹配。一个引用`--global`,另一个不直接复制粘贴达不到预期效果。 (3认同)

小智 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.

  • 解决问题的最简单方法,只需编辑远程源 URL。就这样。完毕。谢谢! (2认同)

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或更新版本,你可以使用.

使用凭证助手在git中缓存您的github密码:

如果您使用HTTPS克隆GitHub存储库,您可以使用凭据帮助程序告诉Git每次与GitHub交谈时都记住您的GitHub用户名和密码.

这也适用于Linux,Mac和Windows.

  • 从安全角度来看,这是一个非常糟糕的主意. (38认同)
  • 是的,它适用于Linux,但不适用于Windows上的gitbash. (3认同)
  • @dolmen我明白你的意思,但如果你介意把密码放在明文中,你可以......加密它;)见http://stackoverflow.com/a/18362082/6309.这甚至与Github的双因素身份验证(2FA)兼容:http://stackoverflow.com/a/18607931/6309 (3认同)
  • 如果你调用你的文件`%HOME%\ _ netrc`(而不是`〜/ .netrc`),它可以在Windows会话或Windows上的git bash中完美运行.另请参阅http://stackoverflow.com/a/18362082/6309以加密该文件. (3认同)
  • 你不需要用明文或加密它.帮助程序实用程序将为您缓存它并需要30秒来设置.https://help.github.com/articles/caching-your-github-password-in-git/#platform-linux (3认同)
  • 这在 Linux 中非常有效,尤其是通过 VPN 使用 git。 (2认同)

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)

  • 我试过这个。它每次都要求我“释义”。我没有设置一个 (3认同)
  • @Raphi运行`git remote -v`看看会发生什么 (2认同)
  • 其他答案应放在此建议之前。 (2认同)

小智 55

如果您使用ssh和您的私有密钥使用密码加密,那么你还是会被提示输入私钥当你做网络操作使用Git来输入密码/密码一样push,pullfetch.

使用ssh-agent保存私钥密码/密码凭据

如果您希望每次都不必输入密码,您可以使用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手册中所述:

-k
Run Code Online (Sandbox Code Playgroud)

终止当前代理(由SSH_AGENT_PID环境变量给出).

可选超时

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

$ ssh-add -t <timeout>
Run Code Online (Sandbox Code Playgroud)

其中<timeout>的格式为<n>h用于<n>小时,<n>m<n>分钟,等等.

根据ssh-agent手册:

-t life
Run Code Online (Sandbox Code Playgroud)

为添加到代理的身份的最长生命周期设置默认值.生命周期可以以秒或以sshd_config(5)中指定的时间格式指定.使用ssh-add(1)为标识指定的生命周期会覆盖此值.如果没有此选项,则默认的最长生命周期是永

有关更多时间格式,请参阅此页面.

Cygwin用户的安全警告

在Cygwin中使用ssh-agent时,Cygwin用户应该意识到潜在的安全风险:

人们应该认识到cygwin [1]下ssh-agent的潜在危险,虽然在本地netstat和远程portscan下,似乎没有人可以访问/ tmp/ssh-foo中指定的端口......?

[1]: http://www.cygwin.com/ml/cygwin/2001-01/msg00063.html
Run Code Online (Sandbox Code Playgroud)

引用的链接:

但请注意,cygwin的unix域套接字是基本不可靠的 ,所以我强烈地 在cygwin下发现ssh-agent的使用.

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

!<socket >2080
Run Code Online (Sandbox Code Playgroud)

然后跑步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)

  • 这是迄今为止恕我直言的最佳答案. (9认同)
  • 我更喜欢'netrc'凭证帮助(http://stackoverflow.com/a/18362082/6309)来缓存*多*凭证(无需记住每个密码).但是如果你在Windows上并且想要使用内存缓存,则需要winstore(http://stackoverflow.com/a/15310274/6309) (2认同)

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

@Update - 使用 ssh

我认为使用ssh协议是一个更好的解决方案https,即使设置步骤稍微复杂一点.

粗略的步骤:

  • 使用命令创建ssh密钥,例如ssh-keygen在linux上,在Windows上msysgit提供类似的命令.
  • 将私钥保存在适当位置的本地计算机上,例如~/.ssh.并通过ssh-add命令将其添加到ssh代理.
  • 将公钥上传到git服务器.
  • remote.origin.url将git存储库更改为ssh样式,例如git@gitlab.com:myaccount/myrepo.git
  • 然后在拉或推时,无需输入用户名或密码.

提示:

  • 如果您的ssh密钥有密码短语,则默认情况下,您需要在每次重新启动计算机后首次使用密钥时输入密码.

@Update - 切换httpsssh协议.

只需更改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/*

  • 不,用https:// username:password @ github.com /`提供密码是安全的.请参阅http://stackoverflow.com/questions/4980912/username-and-password-in-https-url (3认同)

K M*_*lam 15

您可以在Git中缓存GitHub密码:

只需按照github 官方文档中的说明操作即可.

按照上述链接中的说明操作后,您应该可以在不每次输入用户名/密码的情况下向/从您的仓库进行推/拉.

  • 这是最好的答案,应该是公认的IMO (5认同)

Adi*_*l B 15

如果您在 Github 帐户上启用了 2FA,您的常规密码将无法用于此目的,但您可以生成个人访问令牌并在其位置使用它。

访问GitHub 中的Settings-> Developer Settings->Personal Access Tokens页面(https://github.com/settings/tokens/new),并生成具有所有Repo权限的新 Token :

生成 GitHub 个人访问令牌

然后页面将显示新的令牌值。保存此值并在推送到 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)

  • 我认为您仍然需要在推送或以其他方式与远程交互之前添加“git config credential.helper store”,否则下次与远程交互时该令牌将不再存在。正确的? (2认同)

Ori*_*rif 14

对于 Mac 操作系统

  1. 进入你的Github Settings-> Developer Settings-> Personal Access TokensGitHub页面(https://github.com/settings/tokens/new),生成一个具有所有Repo权限的新Token
  2. 在您的 mac 中搜索钥匙串访问 -> 搜索github.com -> 单击Show password然后粘贴token您刚刚复制的内容。
  3. 转到 CLI,它会再次要求您输入用户名和密码,输入您的 Github 用户名并将令牌粘贴为密码,然后您就可以在其余使用 CLI 的时间中继续使用了。


WoJ*_*WoJ 9

对我.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。它将被创建和写入。

请注意,使用此帮助程序会将未加密的密码存储在磁盘上,仅受文件系统权限保护。如果这可能不是可接受的安全权衡。


Lac*_*mov 9

这是另一种选择:

而不是写作

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,这将导致密码在历史记录中被缓存,因此请记住这一点.


Sur*_*nes 6

直接更新你的 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 密钥


Rez*_*aba 6

这是我的工作解决方案,它也通过了 2FA。

  1. 在浏览器上登录 github。
  2. 转到此页面并创建令牌 https://github.com/settings/tokens
  3. 复制生成的令牌字符串。
  4. 在终端中输入此命令并使用您自己的值

git remote set-url origin https://{yourAccountName}:{theCopiedToken}@github.com/{yourAccountName}/{repositoryName}.git

现在您已登录并可以推/拉等。


ral*_*nja 5

你基本上有两个选择。

如果您在两台计算机上使用相同的用户,则需要将 .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)


Lia*_*ang 5

对于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)


Akh*_*N S 5

如果您克隆了 HTTPS 而不是 SSH,并且在拉、推和获取时遇到用户名和密码提示的问题。你可以简单地为 UBUNTU 解决这个问题

第一步:移动到根目录

cd ~/
Run Code Online (Sandbox Code Playgroud)

创建一个文件 .git-credentials

这些内容加进该文件与你usename passwordgithosting URL

https://user:pass@example.com
Run Code Online (Sandbox Code Playgroud)

然后执行命令

git config --global credential.helper store
Run Code Online (Sandbox Code Playgroud)

现在,您将能够毫不费力地从您的存储库中拉取并获取所有详细信息。


Hen*_*nke 5

列出您当前的 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_ed25519id_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 密钥


如果您当前的本地存储库是使用httpusername创建的,则需要重新创建它以便与 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 次

最近记录:

5 年,10 月 前