简体   繁体   English

如何从 Git 中删除缓存的凭据?

[英]How to remove cached credentials from Git?

I ran:我跑了:

$ git config credential.helper store

And then:接着:

$ git push origin master

After pushing, I entered my credentials and they were saved.推送后,我输入了我的凭据并保存了它们。
I read that they are stored in plaintext, and so now I want to remove my credentials from being saved and entered by default.我读到它们以明文形式存储,所以现在我想删除我的凭据,使其不被默认保存和输入。

How can I do this?我怎样才能做到这一点?

在终端中运行以下命令以删除存储在缓存中的凭据

git config --global --unset credential.helper

Your credentials are stored in the file you (or the thing using git credential-store ) specified when you (or it) ran the command, as described in the documentation .您的凭据存储在您(或它)运行命令时指定的文件(或使用git credential-store的东西)中,如文档中所述 The default is $HOME/.git-credentials .默认值为$HOME/.git-credentials You should be able to open this file in your editor and edit it, or simply remove it entirely.您应该能够在编辑器中打开此文件并对其进行编辑,或者干脆将其完全删除。

Note that you may also want to change the credential helper so that these are not stored again.请注意,您可能还想更改凭证助手,以便不再存储这些凭证。 See the git credential-cache documentation as well, for instance.例如,请参阅git credential-cache文档

As he did not give any flag as global or local or system , it would take local by default , so the correct way is to move to the respective folder(repository) and type this command由于他没有给出任何标志为globallocalsystem默认情况下将采用 local ,因此正确的方法是移动到相应的文件夹(存储库)并键入此命令

git config --local --unset credential.helper

or要么

git config --unset credential.helper

Reference: https://www.atlassian.com/git/tutorials/setting-up-a-repository/git-config参考: https : //www.atlassian.com/git/tutorials/setting-up-a-repository/git-config

If you would like the daemon to exit early, forgetting all cached credentials before their timeout, you can issue an exit action, run the following command.如果您希望守护进程提前退出,在超时之前忘记所有缓存的凭据,您可以发出退出操作,运行以下命令。

git credential-cache exit git 凭证缓存退出

For Linux users:对于 Linux 用户:

I ran into this question while trying to switch from password authentication to PAT (personal access token).我在尝试从密码身份验证切换到 PAT(个人访问令牌)时遇到了这个问题。

I used https to work with Github.我使用 https 与 Github 合作。 Github suggests (at the time of writing this answer) that the switch can be done by simplying entering my PAT instead of my password. Github建议(在撰写此答案时)可以通过简单地输入我的 PAT 而不是我的密码来完成切换。 However, the problem I had, was that my password was cached, so I never had to enter it .但是,我遇到的问题是我的密码被缓存了,所以我不必输入它

Source: https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token来源: https : //docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token

So I had to look for my cached/stored password in order to remove it, so that I can then enter my PAT instead of my password when prompted.因此,我必须查找缓存/存储的密码才能将其删除,以便在出现提示时输入我的 PAT 而不是我的密码。

I found the password in the following file:我在以下文件中找到了密码:

/home/MyUsername/.gitconfig

I deleted the file, and simply pasted the PAT instead of my password the next time.我删除了该文件,并在下次粘贴 PAT 而不是我的密码。

Github recommends using their Git Credential Manager Core (GCM Core) for storing credentials (source: https://docs.github.com/en/get-started/getting-started-with-git/caching-your-github-credentials-in-git ). Github 建议使用他们的 Git Credential Manager Core (GCM Core) 来存储凭证(来源: https : //docs.github.com/en/get-started/getting-started-with-git/caching-your-github-credentials-在-git )。 I installed this as well, and the PAT credentials are now stored in a different location:我也安装了它,PAT 凭据现在存储在不同的位置:

/home/MyUsername/.gcm (it's a folder) /home/MyUsername/.gcm (这是一个文件夹)

I wrote this answer in more details and perhaps a little offtopic as starting in 2 days (August 13th 2021), password authentication will no longer be possible, and I suppose some users might have difficulty in locating their cached password.我更详细地写了这个答案,也许在 2 天后(2021 年 8 月 13 日)开始有点题外话,密码身份验证将不再可能,我想有些用户可能难以找到他们缓存的密码。

TL;DR look for: TL; DR 寻找:

/home/MyUsername/.gitconfig

If you want to remove just one cached credential then that can be done in any operating system by driving the Git credential helper API from the Git command line:如果您只想删除一个缓存的凭据,则可以在任何操作系统中通过从 Git 命令行驱动 Git 凭据帮助程序 API 来完成:

Let's say the Git repository is git.example.com and you connect using HTTPS.假设 Git 存储库是 git.example.com 并且您使用 Z0E8433F9A404F1F3DBA1601C 进行连接。 At the operating system command prompt enter the Git command:在操作系统命令提示符处输入 Git 命令:

git credential reject

You're now on the command line for the credential API's reject function.您现在位于凭据 API 拒绝 function 的命令行上。 Type the following lines:键入以下行:

protocol=https
host=git.example.com

On the next line, close the commands with the "end of stdin" keystroke for your operating system.在下一行,使用操作系统的“标准输入结束”键关闭命令。 Typically Ctrl-D for Linux and Macos, and maybe Ctrl-Z for Windows.通常 Ctrl-D 用于 Linux 和 Macos,也许 Ctrl-Z 用于 Windows。

The credential has now been removed from the credential store.凭据现已从凭据存储中删除。 Since we are using Git's Credential API for the removal, it doesn't matter which credential store implementing that API is being used for the backend.由于我们使用 Git 的凭据 API 进行删除,因此后端使用实现 API 的凭据存储并不重要。

Do not alter Git's configuration of user.email or credential.helper before using the above command.在使用上述命令之前,请勿更改 Git 的 user.email 或 credential.helper 的配置。 Run the command as the userid you use to connect with the remote Git repository, not as a super-user.以用于连接远程 Git 存储库的用户 ID 而非超级用户身份运行命令。

None of the above solutions worked for me.以上解决方案都不适合我。 I revoked the access by deletion of the token I had generated the machine.我通过删除我生成的机器的令牌撤销了访问权限。

I logged into GitHub webpage went to: click picture > settings > Developer settings > Personal access tokens > delete the token for that particular machine我登录到 GitHub 网页去了:点击图片 > 设置 > 开发者设置 > 个人访问令牌 > 删除该特定机器的令牌

对我来说 unset 命令不起作用,而是我删除了文件:

rm ~/.git-credentials

None of the above worked for me.以上都不适合我。 I am using a mac what i had to do was to remove my credential manually from keychain app我正在使用 Mac 我必须做的是从钥匙串应用程序中手动删除我的凭据

Steps脚步

  • Ensure credential.helper is osxkeychain确保 credential.helper 是 osxkeychain
$ git config --global credential.helper
osxkeychain
  • Open keychain app打开钥匙串应用

  • Search for the credential (Eg Using username) by selecting (All Items)通过选择(所有项目)搜索凭证(例如使用用户名) 钥匙链访问

  • Press delete key按删除键删除条目

  • Run the git command (push, pull etc).运行 git 命令(推、拉等)。 This should prompt your new password这应该会提示您的新密码

for me all options didn't work, I just edited file "config" inside .git/ in my repository.对我来说,所有选项都不起作用,我只是在我的存储库中的.git/中编辑了文件“config”。

cd .git/

nano config

search for [remote "origin"] and edit above:搜索[remote "origin"]并在上面编辑:

url=YOUR REPOSITORY URL (WITH OR WITHOUT user and pass like username:password@gitlab.com/username/repository.git ) url=YOUR REPOSITORY URL (有或没有用户并像username:password@gitlab.com/username/repository.git一样传递)

save and try push your repo保存并尝试推送您的回购

If you're on windows 10, there is a credential manager in the OS to.如果您使用的是 Windows 10,则操作系统中有一个凭证管理器。

Go to Start menu, type "Credential Manager", then inside this manager, select "Windows Credentials", seek for "git:https//github.com", expand it, then you'll find "Delete" inside of it.进入开始菜单,输入“Credential Manager”,然后在这个管理器里面,选择“Windows Credentials”,寻找“git:https//github.com”,展开它,然后你会在里面找到“Delete”。

enter image description here在此处输入图片说明

Universal and simple solution:通用且简单的解决方案:

echo "url=https://github.com" | git credential reject

This cancels authorization on GitHub.这将取消对 GitHub 的授权。

Oh goodness, here's another location - this one for KDE users.哦,天哪,这是另一个位置——这个是为 KDE 用户准备的。 Spent a long time trying the ones already listed here...花了很长时间尝试这里已经列出的那些......

Launch KDE Wallet Manager (also accessible from KDE Wallet settings in the system settings app), search for "github" and in the "ksshaskpass" section, remove the corresponding Github entry.启动 KDE 钱包管理器(也可以从系统设置应用程序中的 KDE 钱包设置访问),搜索“github”并在“ksshaskpass”部分中删除相应的 Github 条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM