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

GitConfig

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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

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

Solution 2 - Git

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 - Git

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.

Solution 4 - Git

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmstrapView Question on Stackoverflow
Solution 1 - GitberealView Answer on Stackoverflow
Solution 2 - GitVonCView Answer on Stackoverflow
Solution 3 - GitVonCView Answer on Stackoverflow
Solution 4 - GitКонстантин БронштейнView Answer on Stackoverflow