67

I want to override certain Git configuration options (in my case http.proxy) when calling a Git command directly by using command line parameters. Is this possible?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mstrap
  • 16,808
  • 10
  • 56
  • 86

4 Answers4

91

Yes, you can pass it with -c, like:

git -c http.proxy=someproxy clone https://github.com/user/repo.git
bereal
  • 32,519
  • 6
  • 58
  • 104
  • 1
    Quoting from https://www.kernel.org/pub/software/scm/git/docs/ `-c =` Pass a configuration parameter to the command. **The value given will override values from configuration files.** The `` is expected in the same format as listed by git config (subkeys separated by dots). – yasouser Apr 08 '13 at 16:38
  • Great, thanks! Looks like I haven't googled as thoroughly as I should have done. – mstrap Apr 08 '13 at 16:46
  • I find this useful if I occasionally want to use a different editor with git; e.g. `git -c core.editor=gedit commit` – MattSturgeon Jun 23 '16 at 15:40
  • 5
    I don't suppose there is an environment variable that will do the same, for when git is being called by a script and so adding a `-c` parameter is not practical? – Brian White May 04 '18 at 16:29
  • Is there a way to provide several `-c` options at once? – Miles B Huff Sep 30 '22 at 14:43
  • 1
    @MilesBHuff sure, just add `-c` multiple times, e.g. `git -c http.proxy=localhost:3128 -c http.useragent=test`. – bereal Oct 01 '22 at 12:56
11

Note that there is a new feature regarding the ability to override (with the command git -c) a config:

You couldn't set a config to an empty string (git -c http.proxy= or any other foo.bar=), that is until git 2.1.2 (Sept 30th, 2014), and commit a789ca7 Junio C Hamano (gitster)

config: teach "git -c" to recognize an empty string

In a config file, you can do:

[foo]
bar

to turn the "foo.bar" boolean flag on, and you can do:

[foo]
bar=

to set "foo.bar" to the empty string.
However, git's "-c" parameter treats both:

git -c foo.bar

and

git -c foo.bar=

as the boolean flag, and there is no way to set a variable to the empty string.
This patch enables the latter form to do that.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
4

As documented in Git 2.23 (Q3 2019), but already available before that, another place where you can override Git configuration option is... git aliases!

See commit 459842e, commit 01991ce (05 Jun 2019) by Denton Liu (Denton-L).
(Merged by Junio C Hamano -- gitster -- in commit 71221f2, 09 Jul 2019)

config/alias.txt: document alias accepting non-command first word

One can see that an alias that begins with a non-command first word, such as loud-rebase = -c commit.verbose=true rebase, is permitted.
However, this isn't immediately obvious to users as alias instances typically begin with a command.

Document the fact that an alias can begin with a non-command first word so that users will be able to discover that this is a feature.

The documentation now includes:

Note that the first word of an alias does not necessarily have to be a command. It can be a command-line option that will be passed into the invocation of git.

In particular, this is useful when used with -c to pass in one-time configurations or -p to force pagination.

For example, loud-rebase = -c commit.verbose=true rebase can be defined such that running git loud-rebase would be equivalent to git -c commit.verbose=true rebase.

Also, ps = -p status would be a helpful alias since git ps would paginate the output of git status where the original command does not.

For example, I defined:

vonc@vonvb:~/gits/src/git$ git config alias.loud-commit "-c commit.verbose=true commit"
vonc@vonvb:~/gits/src/git$ git loud-commit -a

That gives me:

git loud commit

The diff (red part) would not be present in a commit message editor with a simple git commit -a.

The alias did not need to start with !git to call the shell command git.
It can directly start with a git command option, like -c.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

Yes it's possible. At first how git stores his configuration? I have working repo localy

cat ./projects/autoopt.ru/.git/config

enter image description here

So git stores settings in file. You may edit this file directly, but git provides CLI to simplify editing config params.

So what is command? May be git config set= Hmm... I need param name url? set url= not working?

So how to find out format of git config command?

  • Open google, yandex in my case
  • "git config spec"
  • Official git documentation
  • git config name [value [value-pattern]] (it means you need to pass name of property and space separated value

Now i need to find out how to write name of param when i need to change my remote repos url by given config file content.

[remote "origin"]
   url=...

Class remote, instance origin, class member url with value.

So answer is

git config remote.origin.url <value>