1

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.

  1. $ 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.

theairbend3r
  • 679
  • 9
  • 26
  • I believe this is a repeat of this https://stackoverflow.com/questions/3860112/multiple-github-accounts-on-the-same-computer – Sean Dillon Jul 29 '21 at 14:50
  • If I'm not mistaken, the main focus in the attached link seems to be on using ssh. I'm trying to avoid that and instead use the personal-access-tokens instead. – theairbend3r Jul 29 '21 at 15:13
  • Does [this Git FAQ entry](https://git-scm.com/docs/gitfaq#multiple-accounts-http) answer your question? – bk2204 Jul 29 '21 at 22:00
  • 1
    @torek, that's not the case in any way, shape, or form. Git access over HTTPS with PATs will continue to be supported, as will SSH. Only using the account's password over HTTPS is going away. – bk2204 Jul 31 '21 at 15:10
  • @bk2204: Aha. I had the impression that all https access was to go away! Thanks. – torek Aug 01 '21 at 01:37

0 Answers0