Published September 15, 2020
Updated September 17, 2020
Git Config Include
Demonstrates configuring Git to include project specific configurations.
Transcript
# Hello and welcome to the Alchemists Screencasts! # Today, we'll look at Git Config Include usage. # Custom configurations allow you to augment your global configuration. # This is handy when managing different profiles across multiple projects. # For instance, I use a different configuration for paid versus open source work. # To understand this better, let's study the following repositories: ls -alhT cd examples # Now we can inspect my global Git Hooks setting: git config --global --get core.hooksPath # To change this setting, we can update the global configuration directly: git config --global --replace-all core.hooksPath /dev/null git config --global --get core.hooksPath # In this case, I've essentially disabled my Git Hooks by pointing to a null path. # This isn't ideal as this change effects *all* projects. # For now, I'll revert the change: git config --global --replace-all core.hooksPath "~/.config/git/hooks" git config --global --get core.hooksPath # We could update the local configuration instead: git config --local --replace-all core.hooksPath /dev/null git config --local --get core.hooksPath # Unfortunately, this is cumbersome because *each* project would need updating. # I'll revert this change too: git config --local --unset core.hooksPath git config --local --get core.hooksPath # Luckily, Git provides a way to include additional configurations. # My favorite is `includeIf`. # To start we'll need a shell script to print the Git Hooks of each project: .. vi hooks
chmod 755 hooks cat hooks # This script iterates over each project in this directory. # Then it prints the project name and Git Hook used for the repository: ./hooks # We'll use this script again shortly! # Next, let's create a custom Git configuration: vi ~/.config/git/profiles/tutorial
cat ~/.config/git/profiles/tutorial # The `tutorial` syntax is the same as used for `~/.gitconfig`. # I'm using a XDG configuration to organize my `tutorial` profile: # https://alchemists.io/projects/xdg # ...but your custom configuration can be located anywhere. # Now we just need to teach Git to include this configuration: git config --global includeIf.gitdir:~/Downloads/tutorial/.path ~/.config/git/profiles/tutorial # If it helps, here is what the new entry looks like in `~/.gitconfig`: tail -n 2 ~/.gitconfig # The `includeIf` entry is powerful in that it accomplishes the following: # 1. Checks if any repository is in the `~/Downloads/tutorial` directory structure. # 2. When the above is true, it merges the `~/.config/git/profiles/tutorial` configuration. # OK, let's see these changes in action by running our `hooks` script: ./hooks # Voilà, our custom configuration took precedence over our global configuration! # Much easier than editing each project's configuration directly. # To restore the global configuration, we can remove the include and profile: git config --global --remove-section includeIf.gitdir:~/Downloads/tutorial/ rm -f ~/.config/git/profiles/tutorial # Once again, our global configuration is restored: ./hooks # You can use this feature to customize more than just Git Hooks. # Anything in your global Git configuration will work for these custom profiles. # Enjoy! # https://alchemists.io # ☿ 🜔 🜍 🜂 🜃 🜁 🜄