How do I override Git configuration options by command line parameters?

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?


Solution 1:

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

git -c http.proxy=someproxy clone https://github.com/user/repo.git

Solution 2:

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.

Solution 3:

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.