从Git中删除凭据

985

我正在处理几个代码库,但最近只在我们内部的那个上工作,一切都很顺利。

今天我必须提交并推送代码到另一个代码库,但是我遇到了一些问题。

$ git push appharbor master
error: The requested URL returned error: 403 while accessing https://gavekortet@appharbor.com/mitivo.git/info/refs?service=git-receive-pack
fatal: HTTP request failed

我无法做任何事情,以便再次出现输入密码的提示。

我该如何在我的系统上重置凭据,以便Git会要求我输入那个仓库的密码?

我已经尝试过:

  • git config --global --unset core.askpass

以取消密码:

  • git config credential.helper 'cache --timeout=1'

以避免凭据缓存……

似乎没有什么作用,有没有更好的想法?


你有一个 ~/.netrc 文件吗? - robinst
4
@robinst,这是一台Windows电脑,我无法找到那个文件,即使在Git Bash中也找不到... - balexandre
对于Windows机器,我更喜欢使用新的(git 1.8.3)凭据助手netrc,它可以将多个凭据存储在加密文件中。这比每次为每个会话输入密码要好,因为缓存只会为一定时间“缓存”密码。请参见此处的完整示例 - VonC
我没有 .netrc 文件。我在 ~(C:\Users\Myself)中有一个名为 .git-credentials 的文件,但擦除它并没有起作用,我仍然登录到 Git Shell。此外,控制面板凭据管理器似乎没有存储任何内容。本地和全局 Git 配置文件看起来都没问题。我的工作站是从一位离职的员工那里继承而来的,所以他可能设置了一些我不知道如何关闭的非正统凭据缓存机制。我希望这种行为不是 Git Windows 的默认行为。在 Mac 上,凭据缓存是默认的,但至少会显示在钥匙串访问中。 - damix911
1
关注安全的人可能希望在此之后确保执行 rm ~/.git-credentials。被提示输入密码并不意味着它们没有被存储。 - ribamar
显示剩余3条评论
40个回答

10

这种方法适用于我,并且与操作系统无关。虽然有点笨重,但很快,使我能够重新输入凭据。

只需找到您希望重新输入凭据的远程别名即可。

$ git remote -v 
origin  https://bitbucket.org/org/~username/your-project.git (fetch)
origin  https://bitbucket.org/org/~username/your-project.git (push)

复制项目路径 (https://bitbucket.org/org/~username/your-project.git)

然后移除远程仓库

$ git remote remove origin

然后将它添加回去

$ git remote add origin https://bitbucket.org/org/~username/your-project.git

如果您将密码和用户名添加到源中,那么这将起作用。例如:https://name:password@github.com/repo-url。然后您应该删除源并重新添加。 - hotcakedev
可以通过编辑本地仓库来完成相同的操作(例如,使用“git config -e”命令并将URL更改为origin)。删除origin是过度干预。 - Alexo Po.

9

对于macOS用户:

当您在同一台机器上使用多个Git账户时,会出现此错误。

请按照以下步骤删除github.com凭据:

  1. 前往Finder
  2. 前往应用程序
  3. 前往实用工具文件夹
  4. 打开钥匙串访问
  5. 选择github.com并右键单击

删除"github.com"

再次尝试推送或拉取git,它将要求输入凭据。 输入存储库帐户的有效凭据。 完成,现在可以点赞了。


9

不确定为什么会被踩。它确实有效。我在其他方面卡住了。 - Jason S
我今天又成功地使用了它。每次在工作中更改密码时,我都需要这样做。 - Jason S
2
对我来说非常有效。 - IronAces
同样的情况在这里。你甚至不需要在存储库目录中。 - Nicola Musatti

8
没有任何一个给出的答案适用于我。但最终以下方法对我有用:

没有给我有效的答案。但这是最终为我奏效的方法:
rm -rf ~/.git-credentials

这将删除任何凭据!当您使用新的 git 命令时,将会要求输入密码!


这个问题有一个Windows标签。你的解决方案可能适用于Linux,但不适用于Windows。 - Gqqnbig

7
您可以从以下文件中删除行credential.helper=!github --credentials C:\Program Files\Git\mingw64\etc\gitconfig,以便删除git的凭据。

6
@patthoyts的高票回答开始构建:
他的答案使用了但没有解释local vs. global vs. system配置。它们的官方git文档在这里,值得阅读。
例如,我使用Linux,并且不使用system配置,因此我从不使用--system标志,但通常需要区分--local--global配置。
我的用例是我有两个Github凭据;一个用于工作,一个用于娱乐。
以下是我处理问题的方法:
$ cd work
# do and commit work
$ git push origin develop
# Possibly prompted for credentials if I haven't configured my remotes to automate that. 
# We're assuming that now I've stored my "work" credentials with git's credential helper.

$ cd ~/play 
# do and commit play
$ git push origin develop                                                                   
remote: Permission to whilei/specs.git denied to whilei.                
fatal: unable to access 'https://github.com/workname/specs.git/': The requested URL returned error: 403

# So here's where it goes down:
$ git config --list | grep cred
credential.helper=store # One of these is for _local_
credential.helper=store # And one is for _global_

$ git config --global --unset credential.helper
$ git config --list | grep cred
credential.helper=store # My _local_ config still specifies 'store'
$ git config --unset credential.helper
$ git push origin develop
Username for 'https://github.com': whilei
Password for 'https://whilei@github.com':
Counting objects: 3, done.
Delta compression using up to 12 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 1.10 KiB | 1.10 MiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/whilei/specs.git
   b2ca528..f64f065  master -> master

# Now let's turn credential-helping back on:
$ git config --global credential.helper "store"
$ git config credential.helper "store"
$ git config --list | grep cred
credential.helper=store # Put it back the way it was.
credential.helper=store

值得注意的是,有一些方法可以完全避免这个问题,例如,您可以使用与Github相关联的~/.ssh/config文件和相应的SSH密钥(一个用于工作,一个用于娱乐),并相应地自定义命名远程主机来解决身份验证上下文化问题。

5
我的问题最终得以解决是通过使用GitHub桌面版,进入存储库设置,并从存储库URL中删除用户:密码@。然后,我尝试了一次命令行推送,并被提示输入登录凭据。在我输入凭据后,一切都恢复正常了。Visual Studio和命令行都可以工作,当然还有GitHub桌面版。
GitHub桌面版->存储库->存储库设置->远程选项卡
将主要远程存储库(origin)更改为:

https://pork@muffins@github.com/MyProject/MyProject.git

To:

https://github.com/MyProject/MyProject.git

点击“保存”

凭据将被清除。


将命令行中的原点重置为相同的原点解决了我的问题。我正在使用GitLab,所以我不能使用GitHub桌面版来完成这个任务。 - David Vargas
这真的是解决方案。 您在不同的存储库上使用不同的帐户,将凭据本地存储到存储库中,并且没有全局取消设置适用于此。 这里有一个UI解决方案,但可以使用“git config -e”更改或删除来自原始URL的凭据。 - Alexo Po.

5

请运行以下命令 -

git config --global credential.helper cache

然后运行类似git pull的任何git命令,它会要求输入用户名和密码。输入相关详细信息,如果您想保存Git凭证,请运行以下命令 -

git config --global credential.helper store

5

更新 实际上,useHttpPath是一个git配置,适用于所有Git Credential Managers(GCMs)。已更正。

原问题摘要

  • 在Windows上使用git
  • 在GitHub上处理多个仓库
  • 错误的凭据被用于另一个GitHub仓库

尽管标题说“删除凭据”,但描述让我认为您可能在GitHub上拥有多个帐户,例如针对工作和私人项目。如果是这样,请继续阅读,否则请忽略答案,但某个时候可能会有用。

原因

Git Credential Managers(简称GCM)如Microsoft的GCM for Windows默认按主机存储凭据。可以通过检查Windows凭据管理器来验证此操作(请参见其他答案以了解如何在英语、法语和德语Windows版本上访问它)。 因此,默认情况下无法在同一主机(此处为github.com)上使用多个帐户。

2020年10月,GCM for Windows已被弃用并取代为GCM Core。此处的信息仍适用于新的GCM,它甚至应使用GCM for Windows存储的凭据。

解决方案

配置git以包括每个凭据条目的完整仓库路径作为附加信息。在GCM for Windows上也有记录。
我个人喜欢包括HTTP(S)[repository]路径,以便能够为每个仓库使用单独的帐户。

针对所有可能的主机:

git config --global credential.useHttpPath true

仅适用于 github.com:

git config --global credential.github.com.useHttpPath true

参考 GCM 和 Git 文档,或许您需要特别说明一些不同的内容。


4
在我们的情况下,清除用户的.git-credentials文件中的密码即可解决问题。
c:\users\[username]\.git-credentials

2
是的,已经有几个人多次提到了这一点。 - Liam
重复的答案,缺少细节。 - Bazer Con

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