Switch Git user configs per directory โ with auto-detection โ work email here, personal email there, OSS identity there.
Ever commit with your work email to a personal repo? Or forget to set your signing key for an open-source project? gitctx lets you define named profiles, apply them to any directory, and auto-detect the right profile based on the repo's remote URL or path.
- ๐ฏ Profile-based โ define named configs (work, personal, oss) once, apply anywhere
- ๐ Directory-scoped โ each project gets the right identity automatically
- ๐ฎ Auto-detect โ rules match profiles by remote URL pattern or directory path
- ๐ Instant switching โ one command to change, no manual
git configedits - ๐ Current status โ see which profile is active at a glance
- โ๏ธ Manage profiles โ add, edit, remove profiles with simple commands
- ๐ช Shell hook โ optional prompt integration to show active profile
- ๐พ Tiny & fast โ pure Python, no external dependencies beyond
rich
pip install gitctx# Create profiles
gitctx add work --name "Jane Doe" --email "jane@company.com" --signingkey ABC123
gitctx add personal --name "Jane Doe" --email "jane@gmail.com"
gitctx add oss --name "JaneDoe" --email "jane@users.noreply.github.com"
# Apply manually
gitctx use work
# Check what's active
gitctx currentDefine rules so gitctx automatically picks the right profile when you enter a repo:
# Match any repo whose remote URL contains "github.com/mycompany"
gitctx rule add --profile work --remote "github.com/mycompany"
# Match repos under a specific directory
gitctx rule add --profile work --path "/home/jane/work/*"
# Use regex for more complex matching
gitctx rule add --profile oss --remote "github\.com/janedoe/"
# Higher priority rules win
gitctx rule add --profile client-a --remote "github.com/client-a" --priority 10
gitctx rule add --profile work --remote "github.com" --priority 1
# Auto-apply the matching profile to the current repo
gitctx auto
# Dry run to see what would be applied
gitctx auto --dry-run
# List rules
gitctx rule list
# Remove a rule
gitctx rule remove 1- Rules are checked in priority order (highest first)
- If a rule's
--remotepattern matches any of the repo's remote URLs (as substring or regex), it's a match - If a rule's
--pathglob matches the repo's directory, it's a match - If both
--remoteand--pathare specified, both must match (AND logic) - The first matching rule wins
Add to your .bashrc / .zshrc to auto-switch profiles when you cd into a repo:
# Auto-apply gitctx profile on directory change
gitctx_auto_cd() {
builtin cd "$@" || return
if git rev-parse --git-dir > /dev/null 2>&1; then
gitctx auto 2>/dev/null
fi
}
alias cd='gitctx_auto_cd'Or just show the active profile in your prompt:
__gitctx_ps1() {
local profile
profile=$(gitctx current --quiet 2>/dev/null)
if [ -n "$profile" ]; then
echo " [$profile]"
fi
}
PS1='$(__gitctx_ps1)'$PS1gitctx stores profiles in ~/.gitctx/profiles.toml and rules in ~/.gitctx/rules.toml. When you run gitctx use <name> or gitctx auto, it writes the matching user.name, user.email, and user.signingKey to the local Git config of the current directory (.git/config).
This means:
- โ No global config changes โ each repo stays independent
- โ Works with any Git workflow
- โ
Survives across clones โ just run
gitctx autoonce per clone
Profiles (~/.gitctx/profiles.toml):
[work]
name = "Jane Doe"
email = "jane@company.com"
signingkey = "ABC123DEF456"
[personal]
name = "Jane Doe"
email = "jane@gmail.com"Rules (~/.gitctx/rules.toml):
[[rules]]
profile = "work"
remote_pattern = "github.com/mycompany"
priority = 5
[[rules]]
profile = "personal"
path_glob = "/home/jane/personal/*"
[[rules]]
profile = "oss"
remote_pattern = "github\\.com/janedoe/"
priority = 10| Command | Description |
|---|---|
gitctx add <alias> -n <name> -e <email> [-s <key>] |
Add a profile |
gitctx remove <alias> |
Remove a profile |
gitctx list |
List all profiles |
gitctx use <alias> [-C <dir>] |
Apply a profile to a directory |
gitctx current [-C <dir>] [-q] |
Show active profile |
gitctx auto [-C <dir>] [-n] |
Auto-detect and apply the right profile |
gitctx rule add -p <profile> [-r <remote>] [-P <path>] [--priority N] |
Add an auto-detection rule |
gitctx rule list |
List all rules |
gitctx rule remove <index> |
Remove a rule |
MIT ยฉ Jonathan La Porte