1

I want to reads git global config file using git config --list, so I can use to read and update the global config file ?

3 Answers 3

2

This will give you the ~/.gitconfig type config:

 globalconfig = git.GitConfigParser([os.path.normpath(os.path.expanduser("~/.gitconfig"))], read_only=True)

That's more or less what gitpython itself does, except it also uses "system" and "repo" level configs (where system is "/etc/gitconfig"), see

 def _get_config_path(self, config_level):

and

def config_reader(self, config_level=None):

in git/base.py in the source of gitpython

Sign up to request clarification or add additional context in comments.

Comments

1

gitconfig may be exactly what you need.

3 Comments

So does .gitconfig always lies in user directory location ? I mean what if its location is different for a different user ?
The "user global" (as opposed to "system global") git config file may be in ~/.gitconfig, but XDG now suggests that it should be in $XDG_CONFIG_HOME/git/config with XDG_CONFIG_HOME defaulting to ~/.config. Insert standard grumble about XDG here. :-)
This library package is an empty package and can not be installed.
0

Since @unhammer posted their answer in 2014 GitPython has continued to evolve. Here is an example of how to read the global user name using Python 3 and GitPython v3.1.11:

def git_user_name() -> str:
    from git import Repo
    reader = Repo(path=None).config_reader()
    field = reader.get_value("user", "name")
    return field

This works from any directory, a local repo is not required.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.