Introduction

When working with Git across different contexts (personal projects, work repositories), it's crucial to maintain separate configurations to ensure commits are made with the correct identity and settings. This guide demonstrates how to set up and manage multiple Git configurations effectively.

"My dotfiles"

Warning

Using incorrect Git credentials can lead to commits being associated with the wrong identity, which may expose personal email addresses in work repositories or vice versa.

Configuration Structure

~/.gitconfig
~/.gitconfig-personal
~/.gitconfig-work
Personal Repos
Work Repos

Primary Configuration

The main ~/.gitconfig file serves as the entry point for Git's configuration:

[includeIf "gitdir:~/src/*"]
  path = ~/.gitconfig-work

[includeIf "gitdir:~/src/sandbox/*"]
  path = ~/.gitconfig-personal

[includeIf "gitdir:~/Documents/*"]
  path = ~/.gitconfig-personal

[includeIf "gitdir:~/src/"]
  path = ~/Documents/git-work-precommit

[commit]
  gpgsign = true

[alias]
  lg1 = log \
          --graph \
          --abbrev-commit \
          --decorate \
          --format=format:'%C(bold blue)%h%C(reset) - \
              %C(bold green)(%ar)%C(reset) \
              %C(white)%s%C(reset) %C(dim white)- \
              %an%C(reset)%C(bold yellow)%d%C(reset)' \
          --all
  lg2 = log \
          --graph \
          --abbrev-commit \
          --decorate \
          --format=format:'%C(bold blue)%h%C(reset) - \
              %C(bold cyan)%aD%C(reset) \
              %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n''\
              %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' \
          --all

  lg = !"git lg1"
  diffc = diff --color-words=.

  meld = difftool --tool=meld -y
  meldd = difftool --dir-diff --tool=meld
  meldbase = !git meld $(git merge-base origin/master HEAD)
  review = !git fetch $1 $2 && git checkout FETCH_HEAD && git meldbase && true

[core]
  editor = vim

Personal Git Configuration

Create ~/.gitconfig-personal for personal projects:

[user]
    name = Nuno Leitao
    email = example@example.com
    signingkey = 1234ABCD

Work Git Configuration

Create ~/.gitconfig-work for work-related projects:

[user]
    name = Nuno Leitao
    email = example@acme.com
    signingkey = 1234ABCD

GPG Key Configuration

The same GPG key can be used for both configurations:

 $ gpg -K
 /home/nuno/.gnupg/pubring.kbx
 -----------------------------
 sec   rsa4096 2018-05-09 [SC] [expires: 2022-05-09]
       123456789ABCDEFG56780000123456781234ABCD
 uid           [ultimate] Nuno Leitao <example@example.com>
 uid           [ultimate] Nuno Leitao <example@acme.com>
 uid           [ultimate] [jpeg image of size 10099]
 ssb   rsa4096 2018-05-09 [E] [expires: 2022-05-09]

Note

The GPG key is associated with multiple email addresses, allowing it to sign commits for both personal and work accounts.

Key Considerations

Tip

  • Always verify the active Git configuration before starting work on a new repository
  • Use git config --list to check current settings
  • Consider adding repository-specific configurations for special cases

Common Issues

  1. Wrong Email in Commits

    If you notice commits with incorrect email:

    $ git commit --amend --author="Nuno Leitao <correct@email.com>"
    $ git push --force  # Use with caution!
    
  2. Verifying Configuration

    Check active configuration:

    $ git config --list --show-origin