1

So i have a mac given to me by organistaion for company work. My mac os verison is macOS Monterey version 12.6 Now i have one github account setup here for my company (2FA enabled and using HTTPS with Personal access token) git version 2.37.0 (Apple Git-136) Adding .git/config file output here (local file inside repo path).

[core] 
            repositoryformatversion = 0
            filemode = true
            bare = false
            logallrefupdates = true
            ignorecase = true
            precomposeunicode = true
    [remote "origin"]
            url = https://github.com/<<company_name>>/<<repo_name>>.git
            fetch = +refs/heads/*:refs/remotes/origin/*`

Adding global config file output (~/.gitconfig)

[credential]
        helper = osxkeychain
[url "https://company_username@github.com"]

and inside keychain there are two enteries for github.com

Internet password
application password

Both having my personal access token for the company id. This all works fine as PAT is saved in keychain.

Now i have created a new github account under my name (for personal projects) and following is the local .git/config file output there.

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = true
[user]
        name = <<personal_username>>
        email = <<personal_email_id>>
[remote "origin"]
        url = https://<<PAT>>@github.com/<<personal_username>>/<<repo_name>>.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[credential]
        helper =

i have added PAT here so that it does not copies the one from keychain (as i tried different solutions online)

However, when i try to clone or push in this repo it gives error.

remote: Repository not found.
fatal: repository 'https://github.com/<<personal_user>>/<<repo>>.git/' not found

what i understand is it is taking PAT from keychain and that one is for my company's PAT so giving this error. However i might be wrong. Adding output for my .netrc

machine github.com login <<company_username>> password <<company_PAT>>

I am able to think of two ways to do this

  1. enable keychain for only company's repo and not for personal repo so that i can add PAT personal each time
  2. Add different PAT for different account in keychain

However i am not able to come up with any solution after trying everything online. Again, i am using HTTPS (most of solution online are .SSH) Thank you in advance.

rsajdak
  • 93
  • 12
nitin
  • 80
  • 4
  • 14

2 Answers2

2

https://<<PAT>>@github.com: you do not put the token when you work with credential helper.

You put your GitHub user account, the one you need to access that repository.
And you record in the credential helper the right PAT for that user.

I would recommend installing the Microsoft cross-platform GCM (Git Credential Manager), which will update osxkeychain.

Then record your personal user account and associated PAT:

printf "host=github.com\nprotocol=https\nusername=you\npassword=PAT" | git credential-manager store
# replace 'you' and 'PAT' with your own GitHub account and token

Using https://you@github.com/... (instead of https://company_username@github.com)will force Git to extract the right token from the credential helper.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • hi @VonC as instructed by you i installed git credential manager on mac. Now my global git config file looks like. `[credential] helper = osxkeychain helper = helper = /usr/local/share/gcm-core/git-credential-manager [url "https://<>@github.com"] [credential "https://dev.azure.com"] useHttpPath = true` (sorry for formatting) however where should i run above command ? – nitin Jan 30 '23 at 06:27
  • @nitin I would keep one helper: manager. As for the `printf` I mention in the answer, you can run it in any folder you want, as long as your `${PATH}` includes `/usr/local/share/gcm-core/` – VonC Jan 30 '23 at 06:43
  • Hi @VonC i added requested path to PATH and ran printf command with changes for my personal username and PAT. Still no success on clone. There is also no change in my global config file. – nitin Jan 30 '23 at 07:22
  • i did not understood this :- Using https://you@github.com/... (instead of https://company_username@github.com)will force Git to extract the right token from the credential helper. – nitin Jan 30 '23 at 07:22
  • where should I make this change? shall I add my global and config files again for better understanding? Thank you so much for help – nitin Jan 30 '23 at 07:23
  • That particular change is to be done in the local clone of your personal repository, the one which requires your personal github account, as opposed to your org user. In that repo folder, type `git remote set-url origin https://<>@github.com/<>/<>.git` – VonC Jan 30 '23 at 07:27
  • @nitin Check that for `<>` you have registered the right PAT: `printf "host=github.com\nprotocol=https\nusername=you" | git credential-manager get`: you should see your PAT associated to your personal account. – VonC Jan 30 '23 at 07:28
  • hi VonC `remote set-url origin https://<>@github.com/<>/<>.git` here <> means PAT ? – nitin Jan 30 '23 at 07:38
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/251486/discussion-between-vonc-and-nitin). – VonC Jan 30 '23 at 07:39
0

@VonC Thanks for your answer above which helped me in step 2.

Finally, the below steps worked for me.

Step 1: git remote remove origin

Step 2: git remote add origin https://<<Personal-GitHub-Account>>@github.com/<<personal_username>>/<<repo_name>>.git

Step 3: git push

Step 4: git push --set-upstream origin main

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138