Skip to content

Instantly share code, notes, and snippets.

@esgn
Created December 23, 2021 09:55
Show Gist options
  • Save esgn/b93ff9f1302bbd5ffcbe97e14d33b3fc to your computer and use it in GitHub Desktop.
Save esgn/b93ff9f1302bbd5ffcbe97e14d33b3fc to your computer and use it in GitHub Desktop.
Handle multiple git identities using .gitconfig files

How to use .gitconfig files to handle multiple identities / git providers (github, gitlab, ...)

In the user home, define a parent .gitconfig file for your main account (eg: work account)

[user]
        name = John Doe
        email = john.doe@work.com
[credential]
        helper = store --file /home/johndoe/.git-credentials
        helper = cache --timeout 30000

Then add an [includeIf] section at the end of this main .gitconfig file. This section allows including another git configuration file for all repositories inside a given directory.

[user]
        name = John Doe
        email = john.doe@work.com
[credential]
        helper = store --file /home/johndoe/.git-credentials
        helper = cache --timeout 30000
; include for all repositories inside ~/git-clones/github.com/
[includeIf "gitdir:~/git-clones/github.com/"]
        path = .gitconfig-github

Now create in user home (relative paths are always relative to the including file) the .gitconfig-github file with the necessary options.

[user]
        name = jdoe
        email = 5409148+jdoe@users.noreply.github.com
[credential]
        helper = store --file /home/johndoe/.git-credentials-github
        helper = cache --timeout 30000

You can check the configuration is defined correctly by using a git config command inside one of the cloned repository in a group directory.

johndoe@COMPUTER:~/git-clones/github.com/my-project$ git config user.name
jdoe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment