DEV Community

Dmytro
Dmytro

Posted on • Originally published at delphic.top

3 2

Multiple git accounts

Recently, I've reinstalled linux 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 diogenes@example.com

git config --global credential.helper store
Enter fullscreen mode Exit fullscreen mode

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:super-secure-PAT@gitlab.com
Enter fullscreen mode Exit fullscreen mode

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 = diogenes@example.com

[credential]
  helper = store
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

[user]
  name = Epicurus
  email = epicurus@example.com

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

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

https://epicurus:[ACCES_TOKEN]@gitlab.com
Enter fullscreen mode Exit fullscreen mode

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


File structure

.git-credentials
.gitconfig

└─ code
   └─ epicurus
      ├─ .git-credentials
      └─ .gitconfig
Enter fullscreen mode Exit fullscreen mode

Content

~/.gitconfig

[user]
  name = Diogenes
  email = diogenes@example.com

[credential]
  helper = store

[includeIf "gitdir:~/code/epicurus/"]
  path = ~/code/epicurus/.gitconfig
Enter fullscreen mode Exit fullscreen mode

~/.git-credentials

https://diogenes:[ACCES_TOKEN]@gitlab.com
Enter fullscreen mode Exit fullscreen mode

~/code/epicurus/.gitconfig

[user]
  name = Epicurus
  email = epicurus@example.com

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

~/code/epicurus/.git-credentials

https://epicurus:[ACCES_TOKEN]@gitlab.com
Enter fullscreen mode Exit fullscreen mode

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay