In a previous post I wrote about using different SSH keys for different Git accounts. Let's supose we have configured our keys, but what about the config parameters for each one? I mean: name, username and email.
Keeping track of 3 different Git accounts (e.g. personal, work and client) could be really a pain, especially when you commit with the unproper account and you realize about it 5 pushed commits later. This post will show you how to set different Git configurations for different directories.
Before start we need to know that are different levels of Git settings and local config overwrites global one. For example, a user Git config will overwrite the system one, and local project config overwrites the user's one. In this tutorial we'll base ourselves on the local user configuration.
First we need to locate the user's Git config file that is usually on
$HOME/.gitconfig. If the file doesn't exist create it and set the proper configuration:
[user] name = Abraham Matus email = firstname.lastname@example.org [includeIf "gitdir:~/Projects/work/"] path = .gitconfig-work [includeIf "gitdir:~/Projects/personal/"] path = .gitconfig-personal [includeIf "gitdir:~/Projects/client/"] path = .gitconfig-client
Let me explain what we're doing: the
[user] block contains the generic information for every project; the
[includeIf ""] allow us to set the directory we want to customize its settings, and the internal
path contains the file with the settings for that directory.
Each custom directory settings file should contain minimal configuration as this:
# .gitconfig-personal file [user] name = Abraham Matus username = aimatus email = email@example.com
And for this example these files shiuld be located at the same level as the user's
And that's all. With this configuration now we would not worry about commiting with a wrong account.