Skip to main content

Manage multiple GitHub accounts with automatic identity switching based on workspace folders

Project description

git-auto-switch

CI License: MIT

A CLI tool for managing multiple GitHub accounts with automatic identity switching based on workspace folders.

Features

  • Manage multiple GitHub accounts with separate SSH keys
  • Multiple workspaces per account - map several folders to the same identity
  • Automatic Git identity switching based on workspace folders
  • Pre-commit hook to prevent wrong-email commits
  • SSH key validation and GitHub authentication check during setup
  • Audit repositories and auto-fix identity issues
  • Automatic remote URL rewriting (HTTPS to SSH)
  • Show current active account for any directory

Requirements

  • Bash 3.2+
  • Git 2.13+ (for conditional includes)
  • jq (JSON processor)

Installation

Quick Install (curl)

curl -fsSL https://raw.githubusercontent.com/luongnv89/git-auto-switch/main/install-curl.sh | bash

To uninstall:

curl -fsSL https://raw.githubusercontent.com/luongnv89/git-auto-switch/main/install-curl.sh | bash -s uninstall

pip / uv

pip install git-auto-switch
# or
uv pip install git-auto-switch

npm

npm install -g git-auto-switch

From Source

git clone https://github.com/luongnv89/git-auto-switch.git
cd git-auto-switch

# Install dependencies
brew install jq  # macOS
# or: sudo apt install jq  # Ubuntu/Debian

# Install CLI globally
make install

Quick Start

# Initialize with your first account
git-auto-switch init
# or use the short alias:
gas init

# Add more accounts
gas add

# Show current active account
gas current
# or:
gas whoami

# List all configured accounts
gas list

# Validate configuration
gas validate

# Audit repositories for identity issues
gas audit

# Audit and auto-fix issues
gas audit --fix

# Apply configuration after manual changes
gas apply

Commands

Command Description
init Initialize configuration (first-time setup)
add Add a new account interactively
remove [id] Remove an account
list List all configured accounts
apply Apply configuration to system
validate Validate configuration and check for issues
audit [--fix] Audit repositories for identity mismatches (--fix to auto-fix)
current Show current active account for this directory (alias: whoami)
help Show help message
version Show version

How It Works

  1. SSH Keys: Uses separate SSH keys for each account (generates ed25519 keys if needed)
  2. SSH Config: Adds host aliases (e.g., gh-work, gh-personal) to ~/.ssh/config
  3. Git Config: Uses includeIf.gitdir: to auto-switch identity based on workspace
  4. Pre-commit Hook: Validates email before each commit to prevent mistakes
  5. Remote Rewriting: Converts git@github.com to git@gh-alias for proper SSH key usage

Workflow

Adding a New Account

When you run gas add, the tool will:

  1. Prompt for account details (name, workspaces, SSH key, Git identity)
  2. Validate SSH key exists and test GitHub authentication
  3. Automatically proceed if validation passes (no manual confirmation needed)
  4. Apply configuration immediately
  5. Ask if you want to add another account

Checking Current Account

$ gas current

========================================
  Current Account: work
========================================

  Account ID:  work
  Git Name:    John Doe
  Git Email:   john@company.com
  SSH Alias:   gh-work

  Directory:   /home/user/workspace/work/project

  Workspaces:
    - ~/workspace/work
    - ~/projects/company

Fixing Issues

The audit --fix command automatically fixes:

  • Email mismatches: Removes local user.email so global includeIf takes over
  • Wrong remotes: Rewrites git@github.com to use the correct SSH alias
$ gas audit --fix

Issues in: /home/user/workspace/work/repo1
  Email:
    Expected: john@company.com
    Actual:   wrong@email.com
  Fixed: Removed local user.email (will use global includeIf)

Configuration

Configuration is stored in ~/.git-auto-switch/config.json:

{
  "version": "1.0.0",
  "accounts": [
    {
      "id": "work",
      "name": "Work Account",
      "ssh_alias": "gh-work",
      "ssh_key_path": "~/workspace/work/.ssh/id_ed25519",
      "workspaces": [
        "~/workspace/work",
        "~/projects/company"
      ],
      "git_name": "John Doe",
      "git_email": "john@company.com"
    }
  ]
}

Multiple Workspaces

Each account can have multiple workspace folders. All repositories within any of these folders will use the same Git identity and SSH key.

Use cases:

  • Separate folders for different projects under the same account
  • Client work spread across multiple directories
  • Open source contributions in a dedicated folder

Adding workspaces during setup:

Workspace folder [~/workspace/work]: ~/workspace/work
Add another workspace? (leave empty to continue): ~/projects/company
Add another workspace? (leave empty to continue):

Managing workspaces in the edit menu:

Options:
  [2]   Manage workspaces (add/remove)

Workspace action: a
New workspace folder: ~/freelance/client-a

Example output from gas list:

[work] Work Account
  Git:    John Doe <john@company.com>
  SSH:    gh-work
  Workspaces:
    - ~/workspace/work
    - ~/projects/company
    - ~/freelance/client-a

Cloning Repositories

Always use the SSH alias when cloning:

# For work account
git clone git@gh-work:org/repo.git

# For personal account
git clone git@gh-personal:user/repo.git

Existing repositories with git@github.com remotes will be automatically rewritten when you run gas apply or gas audit --fix.

Rollback

Backups are created automatically before any changes and stored in ~/.git-auto-switch/backup/<timestamp>/:

# List backups
ls ~/.git-auto-switch/backup/

# Restore SSH config
cp ~/.git-auto-switch/backup/<timestamp>/ssh_config ~/.ssh/config

# Restore Git config
cp ~/.git-auto-switch/backup/<timestamp>/gitconfig ~/.gitconfig

Development

See CONTRIBUTING.md for development setup and guidelines.

# Install development dependencies
brew install shellcheck bats-core jq

# Run linter
make lint

# Run tests (59 tests)
make test

# Run both
make all

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

git_auto_switch-0.1.0.tar.gz (27.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

git_auto_switch-0.1.0-py3-none-any.whl (33.7 kB view details)

Uploaded Python 3

File details

Details for the file git_auto_switch-0.1.0.tar.gz.

File metadata

  • Download URL: git_auto_switch-0.1.0.tar.gz
  • Upload date:
  • Size: 27.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for git_auto_switch-0.1.0.tar.gz
Algorithm Hash digest
SHA256 627f9f5141592b3d832ffe4b7331156cef0f78716caea08e437156923adcbe75
MD5 fc40152c2eda4462849041d9402013a0
BLAKE2b-256 b30dcc694c303f7f6fa3fa328b778086b83a1ea6761663914e8a534bd5ffec4d

See more details on using hashes here.

File details

Details for the file git_auto_switch-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for git_auto_switch-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 57aded1fab0b43b62ff8a45e4198d885f25cd72c8fa23b1e01a9fdf48e345996
MD5 3d2a5dfb7453b24ebf74cfad4f328474
BLAKE2b-256 61cd494ef9a63c3d0f9c91391f375e134a876c2bd20e6f1069e9158be0d6586b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page