Git's includeIf directive can automatically switch identities by directory
Developers managing multiple GitHub accounts often accidentally commit under the wrong email, since Git does not warn users about identity mismatches. Git 2.13, released in 2017, introduced the includeIf directive, which applies different configurations automatically based on the working directory. By organizing repositories into separate folders and adding includeIf rules to ~/.gitconfig, each directory can be mapped to a distinct name, email, and SSH key without any per-repo setup. A developer has published a 600-line pure Bash script called git-persona that automates the entire process, including SSH key generation, config file creation, and writing the correct includeIf rules. The tool also provides commands to check the active identity, list all profiles, or manually override the identity for a specific repository.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in