Skip to main content
  1. Posts/

.gitconfig and includeIf Statements

··3 mins
a robot wearing a shirt with the text GIT on it hiking on a road and looking at an old wooden sign for directions, photo realistic

Over the day I was using different machines to work on some git files. One machine was running Ubuntu, the other one Windows. Since I started using GPG keys to sign my commits a few weeks ago, I wondered how I could streamline my config files. A dotfile repository seemed to be the answer.

While I did some research on that, I stumbled upon the nifty feature of IncludeIf in .gitconfig. I could use this to split up the configuration files to match the different Operating Systems.

~
├───.gitconfig
├───.gitconfig-linux
├───.gitconfig-windows
├───.gitconfig-github
└───.gitconfig-gitlab

While this sounds almost too good to be true, I’m currently struggeling to get it to work. It did for a minute, but then stopped again. I believe I have some kind of typo in one of the files, but haven’t found it yet.

VSCode on Windows seems to require the gitdir/i parameter to work (Stackoverflow, Github).

.gitconfig #

[includeIf "gitdir/i:c:/"]
path = ~/.gitconfig-windows
	
[includeIf "gitdir:/home"]
path = ~/.gitconfig-linux

[includeIf "gitdir:~/Git/"]
path = ~/.gitconfig-github

[includeIf "gitdir:~/Gitlab/"]
path = ~/.gitconfig-gitlab

The tilde “~” is nowadays an alias for the HOME variable on all major systems. On Windows it will show C:\Users\$username, on Linux it will be /home/$username. (And on macOS probably something like /Users/$username, but I didn’t check.)

First, includeIf checks if the path of the git directory we are currently working in, starts with a C:\. If so, we are apparently on a Windows machine and include the configuration from C:\Users\$username\.gitconfig-windows.

The next check is for Linux systems. If the path starts with /home we include the linux config from /home/$username/.gitconfig-linux.

Once these are finished, we check if the path has /Git/ or /Gitlab/ in it. This is a a personal preference of mine. I like to sort the repositories I work on into folders based on the provider.

Git = Github
Gitlab = Gitlab

.gitconfig-linux // .gitconfig-windows #

These files only show the path to the gpg binary, to assist in signing the commits.

# .gitconfig-linux
[gpg]
program = /usr/bin/gpg2
# .gitconfig-windows
[gpg]
program = c:/Program Files (x86)/GnuPG/bin/gpg.exe

.gitconfig-github // .gitconfig-gitlab #

Again, the files are pretty similar and will share the same configuration items. The content, however, is different.

# .gitconfig-github
[user]
email = 19500486-josephbadow@users.noreply.gitlab.com
name = josephbadow
signingkey = 962215312718E4709B8F48240A05C906BDF59822 

[commit]
gpgsign = true
# .gitconfig-gitlab
[user]
email = 19500486-josephbadow@users.noreply.gitlab.com
name = josephbadow
signingkey = 962215312718E4709B8F48240A05C906BDF59822 

[commit]
gpgsign = true

Graph it! #

graph TD A(".gitconfig") A -->|"C:\"| B[".gitconfig-windows"] A -->|"/home"| C[".gitconfig-linux"] B -->|"~/Git/"| D[".gitconfig-github"] B -->|"~/Gitlab/"| E[".gitconfig-gitlab"] C -->|"~/Git/"| D[".gitconfig-github"] C -->|"~/Gitlab/"| E[".gitconfig-gitlab"]

I thought a flowchart might make it easier to understand, but my head hurts from looking at it. You can actually use git config --show-origin --get gpg.program to let git show you the value of the parameter (gpg.program in this case) and the config file where it was found.


All this allows me to use one set of configuration files on all my machines. My commits automagically use the proper gpg binary and git credentials, as long as I stay true to my folder structure with Git for Github and Gitlab for, well, Gitlab.

I like it!

jwb
Author
jwb
Something-ops during the day, nowadays mostly asleep during the night.