[Solved] How to store credentials for multiple Github accounts (not individual repos) using libsecret

theairbend3r Asks: How to store credentials for multiple Github accounts (not individual repos) using libsecret
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​

  1. $ git config --global user.name "Johnny"
  2. $ git config --global user.email johnny@example.com
  3. $ sudo apt install libsecret-1-0 libsecret-1-dev
  4. $ cd /usr/share/doc/git/contrib/credential/libsecret/
  5. $ sudo make
  6. $ 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.

Code:
[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

Code:
[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

Code:
[user]
    name = John Doe
    email = johndoe@company.com

Now when I try to push my work code, I get the following error.

Code:
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.

  1. $ git config --global credential.github.com.useHttpPath true

Final ~/.gitconfig:

Code:
[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 every time.

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.

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.