I'm trying to use 2 github accounts on Ubuntu 18.04. I'm also using personal-access-tokens (PAT). Following the setup I'm trying to achieve.
- Have 2 github accounts - main and work on the same pc.
- All repos created inside the
~/work/
directory must automatically use the work name and email for commits while all repos outside this directory must use the main name and email for commits. - Use libresecret to remember the personal-access-tokens for both accounts (main and work) to push data to github from local.
- Use personal-access-tokens instead of ssh.
- Save credentials per account as opposed to per repository (as is the case with using
useHttpPath
.
Here's what I've done.
Step 1: add the main github account
$ git config --global user.name "Johnny"
$ git config --global user.email johnny@example.com
$ sudo apt install libsecret-1-0 libsecret-1-dev
$ cd /usr/share/doc/git/contrib/credential/libsecret/
$ sudo make
$ git config --global credential.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret
The above allows me to push code to my main github account without typing my username and password again and again. I only need to type it once and then libsecret takes over.
~/.gitconfig
up to this point.
[user]
name = Johnny
email = johnny@example.com
[credential]
helper = /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret
Step 2: add the work account
I want all repos inside the ~/work/
directory to be associated with the second github account. I make the following changes -
Global config ~/.gitconfig
[user]
name = Johnny
email = johnny@example.com
[credential]
helper = /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret
[includeIf "gitdir:~/work/"]
path = ~/work/.gitconfig
Work specific config ~/work/.gitconfig
[user]
name = John Doe
email = johndoe@company.com
Now when I try to push my work code, I get the following error.
remote: Permission to work_username/mywork.git denied to main_username.
fatal: unable to access 'https://github.com/work_username/mywork.git/': The requested URL returned error: 403
I solved it by splitting credential management for multiple repositories run by the same host.
$ git config --global credential.github.com.useHttpPath true
But now I have to retype the password once for every new repository I create.
Final ~/.gitconfig
:
[user]
name = Johnny
email = johnny@example.com
[credential]
helper = /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret
[includeIf "gitdir:~/work/"]
path = ~/work/.gitconfig
[credential "github.com"]
useHttpPath = true
The Issue
The issue is, I have to setup the username and password for every new repository I create once. Post that I can use it without having to type in the password everytime.
How can I let libsecret
and git
know to use different passwords for authentication based on directory in which the repository exists? This already happens for the commit name/email using IncludeIf
. I want to somehow extend this to password authentication too.