By now, you’ve invested plenty of time in your local configurations to make yourself as productive as possible. It’s not uncommon for engineers, admins and power users to spend a lot of time in many different contexts, such as a linux server somewhere in the ether, your macOS laptop for work, or even a Windows gaming rig at home.

It’s common for configurations to be stored on https://github.com under what’s known asDotfiles. You can learn more about ~/.gitconfig’s looking at the manpage’s forgit-config. This configuration file allows you to define how you want to interact with git such as how to view diff’s, your author information like name and email, your gpg key for signing and much, much more.

One thing that is only lightly touched upon in the documentation is the use of the includeIf statements. It is a conditional include for additional configuration’s. What is not commonly shared about this feature is the ability for you to define the different path prefixes, and set configurations accordingly. Since each operating system uses their own user directory path, we have an easy way to identify which platform git is running on.

The configuration block you would add is as follows:[includeIf "gitdir:/Users"]
   path = ~/.gitconfig-macos[includeIf "gitdir:C:"]
   path = ~/.gitconfig-windows[includeIf "gitdir:/home"]
   path = ~/.gitconfig-linux

Next, create each one of the included config files references above, with your platform specific configuration file.

Example: Credential Helpers

Instead of storing your credentials in a configuration file for talking to remotes, macOS allows you to piggyback on the keychain using theosxkeychain modules. This is a great module, however it will not bring any value to your Windows experience. On Windows, you might want to check out manager credential helper instead.

macOS configuration:[credential]
   helper = osxkeychain

Windows configuration:[credential]
   helper = manager

Example: Platform-specific Editors

When you’re logged into a Linux server, vim or nano are going to most likely be your methods for completing commits and resolving merge conflicts. However, if you are on macOS, you can take advantage of your GUI and load up your editor in Atom or Visual Studio Code for a better experience (if you prefer that kind of thing).

Linux configuration:[core]
   # Set editor to VIM
   editor = vim

macOS configuration:[core]
   # Set editor to Atom
   editor = atom

Example: Paging with Diff’s

A wonderful tool for highlighting diff’s is diff-highlight. It’s a Python utility that will colorize your git diff's which makes it easier for you to read changes. When you switch to something like a cloud-based Linux instance, installing dependencies like diff-highlight may not be possible. Using a command like less would be more suitable for a CLI-based experience.

macOS configuration:[pager]
   log = diff-highlight | less
   show = diff-highlight | less
   diff = diff-highlight | less

Linux configuration:[pager]
   log = less -X -F
   show = less -X -F
   diff = less -X -F

Example: Work and Personal Authoring

Lastly, you can also use this example to separate out your git-config options depending on the repository you’re in. Below is an example of a go-based project where we can change the [user] configuration and email address depending on which organization the git repo is located within:[includeIf "gitdir:/Users/<username>/go/src/github.com/mikemackintosh"]
   path = ~/.gitconfig-mikemackintosh[includeIf "gitdir:/Users/<username>/go/src/github.com/somecompany"]
   path = ~/.gitconfig-company

Each of the above configs could have their own instances of the following block:[user]
name = Mike Mackintosh
email = m <at> email.tld

Conclusion

Hopefully these tips and tricks help you stay organized and focus on more important challenges. There are so many different configurations you can make by checking which directory you are working out of. Feel free to share some of your shortcuts.

Share this post