Manage Git Configurations

Introduce Multiple Git Users

New users of Git typically start with simple setups. But over time, the number of repositories grows. At some point, one will be eventually working with several remote servers and therefore different users. One may even come across repository-specific configurations. For this reason, it is useful to look at the organization of Git's configuration.

Git Configuration Levels

Git uses configuration files of different levels:

This structure can be used place configuration options precisely where there are needed. But there is even another very useful way on top to handle different configurations: The files can be split and included conditionally. This allows to handle some further use cases and it comes handy if your system user has multiple Git users, e.g., per Git remote server.

Example

The example below shows three Git configurations at system user level (global). In listing 1 is shown how the conditional includes work by including configuration files depending on the current working directory. There is a configuration to work with in path/to/my/personal/projects/ (see listing 2) and a second one (see listing 3) for path/to/my/github/projects/.


[includeIf "gitdir:path/to/my/personal/projects/**"]
  path = .gitconfig_personal
[includeIf "gitdir:path/to/my/github/projects/**"]
  path = .gitconfig_github
Listing 1: The global system user configuration .gitconfig

[user]
    name = UserName1
    email = User1Mail@domain.tld
...
Listing 2: Partial configuration for the own Git server .gitconfig_personal

[user]
    name = UserName2
    email = User2Mail@domain.tld
...
Listing 3: Partial configuration for GitHub .gitconfig_github

Note: The two ** include sub-folders recursively.

Published: