提交到Git返回错误代码403致命错误:HTTP请求失败

819

我能够通过HTTPS认证克隆该存储库的一个副本。我已经提交了一些更改并希望将它们推送回GitHub服务器。在Windows 7 x64上使用Cygwin。

C:\cygwin\home\XPherior\Code\lunch_call>git push
Password:
error: The requested URL returned error: 403 while accessing https://MichaelDrog
alis@github.com/derekerdmann/lunch_call.git/info/refs

fatal: HTTP request failed

同时设置为详细模式。我仍然感到相当困惑。

C:\cygwin\home\XPherior\Code\lunch_call>set GIT_CURL_VERBOSE=1

C:\cygwin\home\XPherior\Code\lunch_call>git push
Password:
* Couldn't find host github.com in the _netrc file; using defaults
* About to connect() to github.com port 443 (#0)
*   Trying 207.97.227.239... * 0x23cb740 is at send pipe head!
* Connected to github.com (207.97.227.239) port 443 (#0)
* successfully set certificate verify locations:
*   CAfile: C:\Program Files (x86)\Git/bin/curl-ca-bundle.crt
  CApath: none
* SSL connection using AES256-SHA
* Server certificate:
*        subject: 2.5.4.15=Private Organization; 1.3.6.1.4.1.311.60.2.1.3=US; 1.
3.6.1.4.1.311.60.2.1.2=California; serialNumber=C3268102; C=US; ST=California; L
=San Francisco; O=GitHub, Inc.; CN=github.com
*        start date: 2011-05-27 00:00:00 GMT
*        expire date: 2013-07-29 12:00:00 GMT
*        subjectAltName: github.com matched
*        issuer: C=US; O=DigiCert Inc; OU=www.digicert.com; CN=DigiCert High Ass
urance EV CA-1
*        SSL certificate verify ok.
> GET /derekerdmann/lunch_call.git/info/refs?service=git-receive-pack HTTP/1.1
User-Agent: git/1.7.4.3282.g844cb
Host: github.com
Accept: */*
Pragma: no-cache

< HTTP/1.1 401 Authorization Required
< Server: nginx/1.0.4
< Date: Thu, 15 Sep 2011 22:44:41 GMT
< Content-Type: text/plain
< Connection: keep-alive
< Content-Length: 55
< WWW-Authenticate: Basic realm="GitHub"
<
* Ignoring the response-body
* Expire cleared
* Connection #0 to host github.com left intact
* Issue another request to this URL: 'https://MichaelDrogalis@github.com/dereker
dmann/lunch_call.git/info/refs?service=git-receive-pack'
* Couldn't find host github.com in the _netrc file; using defaults
* Re-using existing connection! (#0) with host github.com
* Connected to github.com (207.97.227.239) port 443 (#0)
* 0x23cb740 is at send pipe head!
* Server auth using Basic with user 'MichaelDrogalis'
> GET /derekerdmann/lunch_call.git/info/refs?service=git-receive-pack HTTP/1.1
Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
User-Agent: git/1.7.4.3282.g844cb
Host: github.com
Accept: */*
Pragma: no-cache

< HTTP/1.1 401 Authorization Required
< Server: nginx/1.0.4
< Date: Thu, 15 Sep 2011 22:44:41 GMT
< Content-Type: text/plain
< Connection: keep-alive
< Content-Length: 55
* Authentication problem. Ignoring this.
< WWW-Authenticate: Basic realm="GitHub"
* The requested URL returned error: 401
* Closing connection #0
* Couldn't find host github.com in the _netrc file; using defaults
* About to connect() to github.com port 443 (#0)
*   Trying 207.97.227.239... * 0x23cb740 is at send pipe head!
* Connected to github.com (207.97.227.239) port 443 (#0)
* successfully set certificate verify locations:
*   CAfile: C:\Program Files (x86)\Git/bin/curl-ca-bundle.crt
  CApath: none
* SSL re-using session ID
* SSL connection using AES256-SHA
* old SSL session ID is stale, removing
* Server certificate:
*        subject: 2.5.4.15=Private Organization; 1.3.6.1.4.1.311.60.2.1.3=US; 1.
3.6.1.4.1.311.60.2.1.2=California; serialNumber=C3268102; C=US; ST=California; L
=San Francisco; O=GitHub, Inc.; CN=github.com
*        start date: 2011-05-27 00:00:00 GMT
*        expire date: 2013-07-29 12:00:00 GMT
*        subjectAltName: github.com matched
*        issuer: C=US; O=DigiCert Inc; OU=www.digicert.com; CN=DigiCert High Ass
urance EV CA-1
*        SSL certificate verify ok.
* Server auth using Basic with user 'MichaelDrogalis'
> GET /derekerdmann/lunch_call.git/info/refs HTTP/1.1
Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
User-Agent: git/1.7.4.3282.g844cb
Host: github.com
Accept: */*
Pragma: no-cache

* The requested URL returned error: 403
* Expire cleared
* Closing connection #0
error: The requested URL returned error: 403 while accessing https://MichaelDrog
alis@github.com/derekerdmann/lunch_call.git/info/refs

fatal: HTTP request failed

以下是我所拥有的Git和Curl版本:

C:\Users\XPherior>git --version
git version 1.7.4.msysgit.0

C:\Users\XPherior>curl --version
curl 7.21.7 (amd64-pc-win32) libcurl/7.21.7 OpenSSL/0.9.8r zlib/1.2.5
Protocols: dict file ftp ftps gopher http https imap imaps ldap pop3 pop3s rtsp
smtp smtps telnet tftp
Features: AsynchDNS GSS-Negotiate Largefile NTLM SSL SSPI libz

4
看一下这条评论,应该发布为答案。我让它在1.7.1版本中工作了,也遇到了缺少用户/密码提示的问题。感谢 https://dev59.com/K2s05IYBdhLWcg3wG-VR#9575906 提供的帮助... - sjas
3
检查用户对仓库是否具有读写权限。 - Céline Aussourd
42
将来展示示例HTTP头时,请勿粘贴“Authorization: Basic <stuff>”。这很容易被Base64解码并获取您的密码。@Mike:如果您自发布此问题以来没有更改GitHub密码,建议您这样做。 - FlipMcF
6
谢谢,我不知道这个事情。已经更改了密码。 - Mike
8
@Mike 没问题。现在我可以毫不掩饰地宣传一下我写的博客:http://goo.gl/bpae94 - FlipMcF
显示剩余9条评论
75个回答

997

我也遇到过同样的问题,后来找出了原因。

Github只支持ssh方式读写repo,虽然https方式也显示为“读写”。

所以你需要在PC上更改repo配置为ssh方式:

  1. 编辑repo目录下的.git/config文件。
  2. [remote "origin"]部分中查找url=条目。
  3. 将它从:
    url=https://MichaelDrogalis@github.com/derekerdmann/lunch_call.git
    更改为:
    url=ssh://git@github.com/derekerdmann/lunch_call.git
    即在@符号前的所有文本更改为ssh://git
  4. 保存config文件并退出。现在你可以使用git push origin master来同步你在GitHub上的repo。

64
当你使用git只读地址(未登录时的默认地址)而不是读写ssh地址进行克隆时,经常会遇到这种情况。 - Nitrodist
50
我的 .git/config 文件原来是 url=https://github.com/mynickname/my_repo.git,但我把它改为了 url=ssh://git@github.com/mynickname/my_repo.git,这样就行了。 - Guillaume Flandre
12
你可以使用git remote set-url命令更改一个仓库的URL。请参见下面的回答。 - fetsh
11
或者只需在命令行中使用'git remote set-url <name> git@github.com:<username> / <repo>.git'来更改它 - Igbanam
47
如果你不想用ssh作为解决方案,可以看一下其他答案。在https网址中加入您的用户名似乎可以起作用。(将配置网址更改为 https://UNAME@github.com/...,其中UNAME是你的用户名) - Cyrus
显示剩余13条评论

519

要确保使用 https 协议进行登录,首先应该将您的 身份验证凭据 设置为 git 的 远程 URI

git remote set-url origin https://yourusername@github.com/user/repo.git

当你尝试进行git push时,会要求输入密码。

实际上,这是基于HTTP身份验证格式的。你也可以设置密码:

https://youruser:password@github.com/user/repo.git

你应该意识到,如果这样做,你的 Github 密码将会以明文形式存储在你的 .git 目录中,显然是不可取的。


13
工作中SSH被阻止了,所以我必须使用HTTPS。添加用户名解决了我的问题。 - David Poole
43
这应该是被接受的答案。当有命令行界面可供使用时,没有必要手动编辑配置文件。 - Steve Bennett
4
对我而言,使用这种方式(https://username@github...)会要求输入密码并且可以工作,而ssh://git@github...则因期望公钥身份验证而失败。 - Steve Bennett
3
@Achint 这是一种身份验证格式。尽管不安全,但您可以将密码设置为https://youruser:password@github.com/user/repo.git - Thiago Macedo
8
请注意,如果您遇到这个问题,如果用户名或密码中有特殊字符,您需要对它们进行URL编码。例如,@符号需要编码为%40。 - Kosmo
显示剩余15条评论

132

对于Sean的答案,可以做出一个小小的补充。

不必手动编辑.git/config文件,你可以使用git remote set-url命令。

在你的情况下应该使用以下命令:

git remote set-url origin ssh://git@github.com/derekerdmann/lunch_call.git

我觉得这比折腾点文件更容易和更清晰。


我不知道这是否只是一篇旧帖子,而且git已经改变了,但是今天为了解决问题,我不得不使用git remote set-url origin ssh://git@github.com:derekerdmann/lunch_call.git,在github.com和仓库名称之间加上一个冒号。 - KevinL

67

在您的存储库目录下编辑.git/config文件

[remote "origin"]部分下找到url=条目

将其从url=https://github.com/rootux/my-repo.git更改为https://USERNAME@github.com/rootux/my-repo.git

其中USERNAME是您的Github用户名


这个答案并不适用于我的问题。我的.git/config中的URL设置为https://github.com/myrepo/subproject'/. - kilojoules
在使用Android Studio时遇到了这个问题,但现在已经解决了,一切正常。 - Johann
我的直觉是这更符合正确答案的方向,因为其他解决方案要么切换到SSH协议,要么将密码以明文形式存储。 - boomkin

51

其他回答建议切换到SSH,有些已经偏离了问题的实质。虽然GitHub支持HTTPS,但你必须使用你的GitHub密码登录,而不是你的SSH密码(这就是我遇到完全相同错误的原因所在)。

我曾经也遇到过同样的问题,但是确保在终端密码提示处使用我的实际GitHub密码,解决了问题,无需修改配置或转向SSH。

需要注意的是,许多公共机构(比如我的学校)会阻止SSH,但允许HTTPS(这也是我最初开始使用HTTPS克隆的唯一原因)。

希望这能帮助任何遇到同样问题的人……


1
但为什么?我过去不需要这样做。是证书过期了还是其他原因? - Thufir
不确定,我不是专家,这只是我在我的情况下让它工作的方式 :) - BMB
同@Thufir。我不知道为什么这突然有效了。 - Nick Manning
我可以使用用户名和密码在浏览器上成功登录。但是,当我尝试进行git push时,系统提示我输入u和p,结果是权限被拒绝。你知道我缺少什么吗? - Omar

45

如果您使用的是Windows操作系统,在使用git推送代码时,可能会出现权限认证失败的情况。这是因为Windows将外部仓库(在我们的例子中是github)的凭据存储在其自己的存储中,而这里保存的凭据可能与您当前需要使用的凭据不同。

图片描述

为避免这种问题,只需在Windows凭据管理器中找到github并删除保存的凭据。之后,当您推送代码时,git将请求您输入凭据,并允许您进行推送。


1
你可能需要检查Windows凭据管理器,并在“控制面板>用户帐户>凭据管理器> Windows凭据>通用凭据”下删除Github条目。详情请参阅此帖子:https://dev59.com/bl4c5IYBdhLWcg3wLXka#37450495 - Miguel
确切的答案(完美地为我工作) - Md Rehan
是的,它起到了作用,其他答案都没有起作用。在“Windows凭据>通用凭据”下删除git:username,然后在CMD中使用令牌作为push命令后要求的密码。 - undefined

33

Mac OS X上出现了相同的错误和解决方案。

一切都很顺利,直到我在GitHub上创建了一个新帐户并尝试推送。

$ git push -u origin master

出现错误:

远程:NEWUSER/NEWREPO.git的访问权限被拒绝,无法访问“https://github.com/NEWUSER/NEWREPO.git/”,错误代码为403。

可以通过为全局或当前存储库设置user.name来修复该问题。

$ git config –-global user.name NEWUSER
$ git config user.name NEWUSER

但是它并没有起作用。

我通过在钥匙串访问应用程序下的密码部分中删除与GitHub相关的OLDUSER来修复了这个问题,然后推送命令成功执行。

$ git push -u origin master

参考资料


从 Keychain Access 应用程序中删除 OLDUSER 对我有帮助。 - Augusto Triste
1
我遇到了同样的错误,但是由于不同但相关的原因,需要不同的解决方案。我试图推送到一个我没有写入权限的仓库(我不是协作者)。这更多是由于我不知道如何为不是我的仓库做出贡献。我以为我可以只创建一个新分支,然后发起一个拉取请求。但事实并非如此。我必须先fork该仓库,在fork上进行更改,然后从我的forked仓库向原始仓库发起拉取请求。可能是我犯的一个愚蠢的错误,但如果我能犯这个错误,那么我相信其他n00bs也会犯。 :) - Neo

23

这对我有效 -:

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

希望对你有所帮助


1
干得好。设置用户! - marlonpya
1
这个解决方案适用于在CentOS 6.10 x86上使用git v1.7。我原本期望会出现密码提示(而不是SSH密钥),并且确实出现了(通过在远程URL中在“github.com”之前添加“<username>@”)。谢谢! - vulcan raven

19

我认为@deepwaters的答案适用于旧版本。HTTPS URL需要包含用户名。我使用的是git 1.7.0.4版本,直到我添加了密码,git push origin master才会要求输入密码。


16

这里有很多答案,但这是解决我的问题的方法。

从2020年7月起,如果您使用HTTPS,则必须使用令牌身份验证访问GitHub

因此,您必须在个人资料设置页面中生成访问令牌并将其用作密码。选中写入存储库所需的框。

现在使用此命令将您的用户名添加到repo中:

git remote set-url origin https://yourusername@github.com/user/repo.git

当您尝试推送时,它会要求您输入密码。 输入您新生成的令牌。

如果您仍然无法推送,请使用 Web 界面将其作为个人仓库分叉以隔离任何权限问题。这样您就可以专注于解决身份验证问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接