Multiple git accounts
Dmytro
Posted on January 30, 2022
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
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
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
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 = epicurus@example.com
[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 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
Content
~/.gitconfig
[user]
name = Diogenes
email = diogenes@example.com
[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 = epicurus@example.com
[credential]
helper = store --file=/home/diogenes/code/epicurus/.git-credentials
~/code/epicurus/.git-credentials
https://epicurus:[ACCES_TOKEN]@gitlab.com
Posted on January 30, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024