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
Here’s what I’ve done.
Step 1: add the main github account
$ git config --global user.name "Johnny"
$ git config --global user.email firstname.lastname@example.org
$ 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 = email@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 –
[user] name = Johnny email = firstname.lastname@example.org [credential] helper = /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret [includeIf "gitdir:~/work/"] path = ~/work/.gitconfig
Work specific config
[user] name = John Doe email = email@example.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
[user] name = Johnny email = firstname.lastname@example.org [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 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
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.