如何禁用单个存储库的 git 凭证助手?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13198143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I disable git's credential helper for a single repository?
提问by Brant Bobby
If I have a credential helper set in my ~/.gitconfig
, how can I disable/bypass it in a specific repo and use nocredential helper?
如果我在我的凭据帮手一套~/.gitconfig
,我怎么可以禁用/绕过它在特定的回购和使用无凭证帮手?
I've tried editing the repo's .git/config
file to blank out the credential.helper
property like this:
我试过编辑 repo 的.git/config
文件来清空这样的credential.helper
属性:
[credential]
helper =
... but when I do a git push
I get the following error message, and Git uses my default credential helper from ~/.gitconfig
anyway:
...但是当我执行 a 时,git push
我收到以下错误消息,并且 Git~/.gitconfig
无论如何都会使用我的默认凭据帮助程序:
git: 'credential-' is not a git command. See 'git --help'.
Did you mean this?
credential
I'm using git version 1.7.12.
我正在使用 git 版本 1.7.12。
(Backstory/use case: Git is configured to use the store
helper, which stores passwords unencrypted on disk. The cache
helper is unavailable on this system. I have a couple repos for which I'd rather type the password every time than keep it on disk.)
(背景故事/用例:Git 配置为使用store
帮助程序,它将未加密的密码存储在磁盘上。该cache
帮助程序在此系统上不可用。我有几个存储库,我宁愿每次都输入密码而不是将其保存在磁盘上.)
采纳答案by VonC
With git 2.9 (June 2016), this (helper =
) will actually work!
使用 git 2.9(2016 年 6 月),这个 ( helper =
) 实际上会起作用!
See commit 2432137(26 Feb 2016) by Jeff King (peff
).
(Merged by Junio C Hamano -- gitster
--in commit 1b68962, 03 Apr 2016)
请参阅Jeff King ( ) 的commit 2432137(2016 年 2 月 26 日)。(由Junio C Hamano合并-- --在commit 1b68962,2016 年 4 月 3 日)peff
gitster
The
credential.helper
configuration variable is cumulative and there is no good way to override it from the command line.
As a special case, giving an empty string as its value now serves as the signal to clear the values specified in various files.
credential
: let empty credential specs reset helper listSince the
credential.helper
key is a multi-valued config list, there's no way to "unset" a helper once it's been set. So if your system/etc/gitconfig
sets one, you can never avoid running it, but only add your own helpers on top.Since an empty value for
credential.helper
is nonsensical (it would just try to run "git-credential-
"), we can assume nobody is using it. Let's define it to reset the helper list, letting you override lower-priority instances which have come before.
所述
credential.helper
配置变量是累积的,没有很好的办法从命令行覆写。
作为一种特殊情况,将空字符串作为其值现在用作清除各种文件中指定值的信号。
credential
: 让空凭证规范重置助手列表由于
credential.helper
键是一个多值的配置列表,一旦它被设置,就无法“取消设置”一个助手。因此,如果您的系统/etc/gitconfig
设置了一个,您将永远无法避免运行它,而只能在顶部添加您自己的助手。由于空值 for
credential.helper
是无意义的(它只会尝试运行“git-credential-
”),我们可以假设没有人使用它。让我们定义它来重置助手列表,让您覆盖之前出现的低优先级实例。
Even more convenient: With Git 2.26 (Q1 2020), this override applies even for any value.
更方便:使用 Git 2.26(2020 年第一季度),此覆盖甚至适用于任何值。
See commit 46fd7b3, commit 82eb249, commit 588c70e, commit 732f934, commit 3fa0e04(20 Feb 2020) by brian m. carlson (``).
(Merged by Junio C Hamano -- gitster
--in commit 2cbb058, 05 Mar 2020)
请参阅brian m 的commit 46fd7b3、commit 82eb249、commit 588c70e、commit 732f934、commit 3fa0e04(2020 年 2 月 20 日)。卡尔森 (``).
(由Junio C gitster
Hamano合并-- --在commit 2cbb058,2020 年 3 月 5 日)
credential
: use the last matching username in the configSigned-off-by: brian m. carlson
Everywhere else in the codebase, we use the rule that the last matching configuration option is the one that takes effect.
This is helpful because it allows more specific configuration settings (e.g., per-repo configuration) to override less specific settings (e.g., per-user configuration).
However, in the credential code, we didn't honor this setting, and instead picked the first setting we had, and stuck with it.
This was likely to ensure we picked the value from the URL, which we want to honor over the configuration.
It's possible to do both, though, so let's check if the value is the one we've gotten over our protocol connection, which if present will have come from the URL, and keep it if so.
Otherwise, let's overwrite the value with the latest version we've got from the configuration, so we keep the last configuration value.
credential
: 使用配置中最后一个匹配的用户名签字人:brian m. 卡尔森
在代码库的其他任何地方,我们都使用最后一个匹配的配置选项生效的规则。
这很有用,因为它允许更具体的配置设置(例如,每个存储库配置)覆盖不太具体的设置(例如,每个用户配置)。
然而,在凭证代码中,我们没有遵守这个设置,而是选择了我们拥有的第一个设置,并坚持使用它。
这可能确保我们从 URL 中选择值,我们希望在配置中尊重它。
但是,两者都可以执行,所以让我们检查该值是否是我们通过协议连接获得的值,如果存在,它将来自 URL,如果是,则保留它。
否则,让我们用从配置中获得的最新版本覆盖该值,因此我们保留最后一个配置值。
回答by Ricardo Coelho
What I've tried, and had worked well was:
我尝试过并且效果很好的是:
$ git config --system --unset credential.helper
But in order to that works fine, I had to set git-bash.exe with Administrator rights.
但为了使其正常工作,我必须使用管理员权限设置 git-bash.exe。
Unfortunately, I think this is a global variable. You should test and see if it works for single repositories.
不幸的是,我认为这是一个全局变量。您应该测试并查看它是否适用于单个存储库。
Good luck
祝你好运
回答by Micha? Politowski
A config variable that is set to an empty string is not the same as an unset variable. It is not possible to force a variable to be unset in .git/config
when it is already set in ~/.gitconfig
.
设置为空字符串的配置变量与未设置的变量不同。如果.git/config
已经在 中设置了变量,则无法强制取消设置~/.gitconfig
。
Additionally credential.helper
is one of these variables where multiple values are used, and in such cases the values are aggregated from all read config files.
此外credential.helper
是使用多个值的这些变量之一,在这种情况下,这些值是从所有读取的配置文件中聚合的。
So basically your options seem to be:
所以基本上你的选择似乎是:
either do not use
credential.helper
in~/.gitconfig
; set the store helper only for the repositories where you want it, either in their.git/config
, or in~/.gitconfig
by specifying the repo URLs, eg.[credential "https://specific.example.com/repo.git"] helper = store
or implement your own helper that does nothing for a set of configured repositories and delegates to
git credential-store
for the rest.
要么不使用
credential.helper
in~/.gitconfig
; 仅为您想要的存储库设置商店助手,无论是在他们的.git/config
,还是~/.gitconfig
通过指定存储库URL,例如。[credential "https://specific.example.com/repo.git"] helper = store
或者实现你自己的助手,它对一组配置的存储库没有任何作用,并委托给
git credential-store
其余的。
回答by VonC
In addition of 'git config credential.helper=
' that I mention above with Git 2.9, you now (Git 2.13.x/Git 2.14, Q3 2017) can disable the credential helper just for one command(and not just for any command in a given repo)
除了我上面在 Git 2.9 中提到的 ' git config credential.helper=
'之外,您现在(Git 2.13.x/Git 2.14,2017 年第 3 季度)可以仅针对一个命令(而不仅仅是针对给定存储库中的任何命令)禁用凭证帮助程序
That means git -c credential.helper= clone /url/remote/repo
now works.
这意味着git -c credential.helper= clone /url/remote/repo
现在有效。
"git clone --config var=val
" is a way to populate the
per-repository configuration file of the new repository, but it did not work well when val is an empty string.
This has been fixed.
" git clone --config var=val
" 是一种填充新存储库的每个存储库配置文件的方法,但是当 val 为空 string 时它无法正常工作。
这已被修复。
See commit db4eca1(02 May 2017) by Jonathan Nieder (artagnon
).
(Merged by Junio C Hamano -- gitster
--in commit 883247c, 16 May 2017)
请参阅Jonathan Nieder ( )提交的 db4eca1(2017 年 5 月 2 日)。(由Junio C Hamano合并-- --在提交 883247c 中,2017 年 5 月 16 日)artagnon
gitster
clone
: handle empty config values in-c
"
git clone --config
" uses the following incantation to add an item to a config file, instead of replacing an existing value:git_config_set_multivar_gently(key, value, "^$", 0)
As long as no existing value matches the regex
^$
, that works as intended and adds to the config. When a value is empty, though, it replaces the existing value.Noticed while trying to set
credential.helper
during a clone to use a specific helper without inheriting from~/.gitconfig
and/etc/gitconfig
.
That is, I ran:git clone -c credential.helper= \ -c credential.helper=myhelper \ https://example.com/repo
intending to produce the configuration:
[credential] helper = helper = myhelper
Without this patch, the '
helper =
' line is not included and the credential helper from/etc/gitconfig
gets used.
clone
: 处理空的配置值-c
"
git clone --config
" 使用以下咒语将项目添加到配置文件,而不是替换现有值:git_config_set_multivar_gently(key, value, "^$", 0)
只要没有现有值与 regex 匹配
^$
,它就会按预期工作并添加到配置中。但是,当值为空时,它会替换现有值。
credential.helper
在克隆期间尝试设置以使用特定帮助程序而不继承自~/.gitconfig
和 时注意到/etc/gitconfig
。
也就是说,我跑了:git clone -c credential.helper= \ -c credential.helper=myhelper \ https://example.com/repo
打算生成配置:
[credential] helper = helper = myhelper
如果没有此补丁,
helper =
则不包括“ ”行,并且/etc/gitconfig
会使用来自的凭证助手。
Note that the documentation is now clearer with commit 515360f:
请注意,提交 515360f的文档现在更加清晰:
credential doc: make multiple-helper behavior more prominent
凭证文档:使多帮手行为更加突出
Git's configuration system works by reading multiple configuration files in order, from general to specific:
Git 的配置系统的工作原理是按顺序读取多个配置文件,从一般到具体:
- first, the system configuration
/etc/gitconfig
- then the user's configuration (
~/.gitconfig
or~/.config/git/config
) - then the repository configuration (
.git/config
)
- 一、系统配置
/etc/gitconfig
- 然后是用户的配置(
~/.gitconfig
或~/.config/git/config
) - 然后是存储库配置 (
.git/config
)
For single-valued configuration items, the latest value wins.
For multi-valued configuration items, values accumulate in that order.For example, this allows setting a credential helper globally in
~/.gitconfig
that git will try to use in all repositories, regardless of whether they additionally provide another helper.
This is usually a nice thing --- e.g. I can install helpers to use my OS keychain and to cache credentials for a short period of time globally.Sometimes people want to be able to override an inherited setting.
For thecredential.helper
setting, this is done by setting the configuration item to empty before giving it a new value.
对于单值配置项,以最新值为准。
对于多值配置项,值按该顺序累积。例如,这允许全局设置凭证助手,
~/.gitconfig
因为 git 将尝试在所有存储库中使用,无论它们是否额外提供另一个助手。
这通常是一件好事——例如,我可以安装助手来使用我的操作系统钥匙串并在短时间内全局缓存凭据。有时人们希望能够覆盖继承的设置。
对于credential.helper
设置,这是通过在给它一个新值之前将配置项设置为空来完成的。
回答by angusc
I just ran into this problem as well, as the first credential.helper I had configured was always executing when I wanted to test a new one. This is on macOS using git supplied with Apple's dev tools.
我也遇到了这个问题,因为我配置的第一个 credential.helper 总是在我想测试一个新的时执行。这是在 macOS 上使用 Apple 开发工具提供的 git。
Listing the configs with git config --list
showed both helpers.
列出配置git config --list
显示两个助手。
Running git config --global --edit
did not show the setting for the first helper. It's a little strange that --global when configuring the helper is not the same scope as --global while editing the configs.
运行git config --global --edit
没有显示第一个助手的设置。有点奇怪的是,配置帮助程序时的 --global 与编辑配置时的 --global 范围不同。
Also since it I wanted to include a different helper, just blanking it out wasn't going to solve it.
此外,因为我想包括一个不同的助手,只是将其清除并不能解决它。
Finally found the setting in two places:
终于在两个地方找到了设置:
/Library/Developer/CommandLineTools/usr/share/git-core/gitconfig
/Applications/Xcode.app/Contents/Developer/usr/share/git-core/gitconfig
/Library/Developer/CommandLineTools/usr/share/git-core/gitconfig
/Applications/Xcode.app/Contents/Developer/usr/share/git-core/gitconfig
Removed the credential section from the first one, but both settings were still listed using --list
. After rebooting and trying every combo of unset, unset-all --system, --global, et, I found the second file.
从第一个中删除了凭据部分,但两个设置仍使用--list
. 在重新启动并尝试 unset、unset-all --system、--global 等所有组合后,我找到了第二个文件。
Removed the setting from that file (again was only one) and finally --list
does not show any helpers configured.
从该文件中删除了设置(再次只有一个),最后--list
没有显示任何配置的帮助程序。