ua

Multiple git accounts

Created: | Updated:


How to manage multiple git accounts

Recently, I’ve reinstalled Linux Kubuntu on my old laptop. And I wanted to set up git in a way that I could manage multiple accounts. Essentially, I aimed to be able to do git pull & push without typing every time username and password.

So, I googled a little bit and found a good starting point - make separation by directories. Example: ~/code/work/ and ~/code/personal/ - for work and personal usage respectively.

It’s okay, but I want to have a default account available from the HOME directory ~/ for .dotfiles and other stuff. And then in ~/code/[USER]/ - switch to another account for specific use.


Let’s have two users: Diogenes (default) and Epicurus (secondary).

After installing git, I set it up as usual.

git config --global user.name "Diogenes the Cynic"
git config --global user.email [email protected]

git config --global credential.helper store

Last line just tells git to store credentials in a default file, ~/.git-credentials. This is a file where I place personal access token (PAT) for default account (Diogenes). Note, it is all in plain text, so not secure. But I am willing to take the risk, besides I am not storing passwords, only PAT. PAT actually is a pretty good idea. I can generate one with limited/minimum rights, just read/write permissions, and use in my laptop (where I may experiment with stuff). So even if PAT is stolen or something, my password is secure. I just revoke that PAT.

The format is https://[USERNAME]:[ACCES_TOKEN]@[gitlab.com | github.com]. So, I go to gitlab, generate PAT, and add following the line.

https://diogenes:[email protected]

So, now I can git pull & push to gitlab without typing credentials in any directory as Diogenes.

Up to this point ~/.gitconfig looks like this:

[user]
  name = Diogenes
  email = [email protected]

[credential]
  helper = store

Now it’s time to add a new account, Epicurus.

First, I make new dir ~/code/epicurus/

Second, I add to ~/.gitconfig lines that tell git to apply different config depending on the directory I am currently in.

[includeIf "gitdir:~/code/epicurus/"]
  path = ~/code/epicurus/.gitconfig

Third, I configure new git user ~/code/epicurus/.gitconfig and specify where to find credentials.

[user]
  name = Epicurus
  email = [email protected]

[credential]
  helper = store --file=/home/diogenes/code/epicurus/.git-credentials

Fourth, I grab PAT and store it in ~/code/epicurus/.git-credentials

https://epicurus:[ACCES_TOKEN]@gitlab.com

And now in the directory tree ~/code/epicurus/ I can git pull & push to gitlab without typing credentials as Epicurus, and everywhere else as Diogenes.

Side note. I find it more convenient to use PATs via HTTPS than SSH keys. Mainly because both Github and Gitlab make it easy to manage personal access tokens.


File structure

.git-credentials
.gitconfig

└─ code
   └─ epicurus
      ├─ .git-credentials
      └─ .gitconfig

Content

~/.gitconfig

[user]
  name = Diogenes
  email = [email protected]

[credential]
  helper = store

[includeIf "gitdir:~/code/epicurus/"]
  path = ~/code/epicurus/.gitconfig

~/.git-credentials

https://diogenes:[ACCES_TOKEN]@gitlab.com

~/code/epicurus/.gitconfig

[user]
  name = Epicurus
  email = [email protected]

[credential]
  helper = store --file=/home/diogenes/code/epicurus/.git-credentials

~/code/epicurus/.git-credentials

https://epicurus:[ACCES_TOKEN]@gitlab.com