如何禁用单个存储库的 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 14:52:03  来源:igfitidea点击:

How do I disable git's credential helper for a single repository?

git

提问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/configfile to blank out the credential.helperproperty like this:

我试过编辑 repo 的.git/config文件来清空这样的credential.helper属性:

[credential]
    helper = 

... but when I do a git pushI get the following error message, and Git uses my default credential helper from ~/.gitconfiganyway:

...但是当我执行 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 storehelper, which stores passwords unencrypted on disk. The cachehelper 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.helperconfiguration 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 list

Since the credential.helperkey is a multi-valued config list, there's no way to "unset" a helper once it's been set. So if your system /etc/gitconfigsets one, you can never avoid running it, but only add your own helpers on top.

Since an empty value for credential.helperis 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设置了一个,您将永远无法避免运行它,而只能在顶部添加您自己的助手。

由于空值 forcredential.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 46fd7b3commit 82eb249commit 588c70ecommit 732f934commit 3fa0e04(2020 年 2 月 20 日)。卡尔森 (``).
(由Junio C gitsterHamano合并-- --commit 2cbb058,2020 年 3 月 5 日)

credential: use the last matching username in the config

Signed-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/configwhen it is already set in ~/.gitconfig.

设置为空字符串的配置变量与未设置的变量不同。如果.git/config已经在 中设置了变量,则无法强制取消设置~/.gitconfig

Additionally credential.helperis 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.helperin ~/.gitconfig; set the store helper only for the repositories where you want it, either in their .git/config, or in ~/.gitconfigby 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-storefor the rest.

  • 要么不使用credential.helperin ~/.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/reponow 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.helperduring a clone to use a specific helper without inheriting from ~/.gitconfigand /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/gitconfiggets 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 (~/.gitconfigor ~/.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 ~/.gitconfigthat 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 the credential.helpersetting, 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 --listshowed both helpers.

列出配置git config --list显示两个助手。

Running git config --global --editdid 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 --listdoes not show any helpers configured.

从该文件中删除了设置(再次只有一个),最后--list没有显示任何配置的帮助程序。