JK
»

Manage Git configurations

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 uses configuration files of different levels:

  • system is located accessible for all users of the system.
  • global is a configuration per system user.
  • local is located in a specific repository.

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.

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/.

Listing 1 – The global system user configuration .gitconfig

[includeIf "gitdir:path/to/my/personal/projects/**"]
  path = .gitconfig_personal
[includeIf "gitdir:path/to/my/github/projects/**"]
  path = .gitconfig_github

Listing 2 – Partial configuration for the own Git server .gitconfig_personal

[user]
    name = UserName1
    email = User1Mail@domain.tld
  ...

Listing 3 – Partial configuration for GitHub .gitconfig_github

[user]
  name = UserName2
  email = User2Mail@domain.tld
...

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