Git configuration across multiple repositories

git-logo

Often you need to push across many git repositories with different credentials. The simplest way is to edit “$REPO/.git/config” file to setup environment. But may be more convenient is to keep configuration in a few files behind the project source.

How to manage multiple git repositories in one place is described below.

According to git documentation it is possible to make conditional includes. Follow the next steps to make a solution based on it.

1. Add to “~/.gitconfig” file:

[includeIf "gitdir:~/work/project1/.git"]
    path = .gitconfig-project1

[includeIf "gitdir:~/work/project2/.git"]
    path = .gitconfig-project2

2. Create “~/.gitconfig-project1″ file with content:

[core]
    sshCommand = "ssh -i ~/.ssh/project1 -F /dev/null"

[user]
    name = user1
    email = user1@email.com

3. Create “~/.gitconfig-project2″ file with content:

[core]
    sshCommand = "ssh -i ~/.ssh/project2 -F /dev/null"

[user]
    name = user2
    email = user2@email.com

And that’s it! We configured two projects to use different settings.

The big advantage here we use native git way to configure each repository, which will work in both terminal and UI.

Inspired by this thread.