Siv Scripts

Solving Problems Using Code

Sat 27 June 2020

Customize git for all projects in a directory

Posted by Aly Sivji in Quick Hits   

Yesterday I gave an abridged version of my Data Science Workflows using Docker Containers talk at my employer's Biweekly Engineering Lunch session.

It had been a minute since I last gave this talk so I ran through the examples to make sure everything worked. Having all my examples containerized is a bit of a cheat code, but I did modify a couple of Dockerfiles to make the demo function. Pushing my changes back to GitHub required me to change my local git repo config:

git config user.name "Aly Sivji"
git config user.email "alysivji@gmail.com"

This is great, but it only works for that one project. And what if I forgot to change this setting?

I decided to overengineer this solution by creating a .gitconfig that only applies in certain folders. This post documents how to enable multiple .gitconfig files to control how Git looks and operates in different directories.


Git Config

From git-scm:

Git comes with a tool called git config that lets you get and set configuration variables that control all aspects of how Git looks and operates.

First, a quick review: Git uses a series of configuration files to determine non-default behavior that you may want. The first place Git looks for these values is in the system-wide /etc/gitconfig file, which contains settings that are applied to every user on the system and all of their repositories. If you pass the option --system to git config, it reads and writes from this file specifically.

The next place Git looks is the ~/.gitconfig (or ~/.config/git/config) file, which is specific to each user. You can make Git read and write to this file by passing the --global option.

Process

Let's say that we want to have all folders inside of ~/siv-dev/client-work to have a different Name and Email in the commit logs.

Create a .gitconfig File

This configuration file will contain settings we want to apply to all subdirectories. Create the following files and save to ~/siv-dev/client-world/.gitconfig

[user]
    name = Aly Sivji
    email = client-work@alysivji.com

Update Global .gitconfig

We will need to add the following lines to our global ~/.gitconfig:

[includeIf "gitdir:~/siv-dev/client-work/"]
    path = ~/siv-dev/client-work/.gitconfig

That's it. Now all repos inside of ~/siv-dev/client-work/ will use the custom .gitconfig file.

Happy Gitting!


 
    
 
 

Comments