Sample Header Ad - 728x90

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

1 vote
0 answers
1134 views
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.
[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. 7. $ git config --global credential.github.com.useHttpPath true 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 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.
Asked by theairbend3r (111 rep)
Jul 31, 2021, 11:31 AM