Github's password/token stored in keychain and I can't push as another user - github

So I have a personal and a work's Github account.
After some trial and error I found that the password to Github is stored in the macOS keychain.
That's annoying cause I can't define which account to associate the repo with even though I'd configured my user in ./git/config for that repo.
And the reason it seems is that git will start searching for the global config first. So I removed the global config and keychain and pushed again.
It prompts me for the username password the first time, but stores that into the keychain again and uses that as the default user, not respecting the user I set in ./git/config.
My ideal setup is to have the default user as my personal account and to have the work's account for specific work related repos. How do I do that?
I think the keychain is a bottleneck since it seems to be the first place git is using as authentication.

I found the answer, here
git config credential.username 'your username'
This causes the cli to prompt for a password for that user and it will store that as another entry in the keychain. Will still have to configure git users with git config user.name etc so it knows which user to use.

Related

Asking for password when pushing updates to GitHub

I initialized a new repository, and pushed my code to GitHub.
After that, I updated my README file and tried to push on GitHub again, but when I did that, it asks me for password, and it didn't asked me for password when I pushed my code to GitHub for first time.
I also tried entering my GitHub account password, it fails.
I also tried use the key which I generated as a password, but still no luck.
Can anyone please guide me how to solve this problem?
This is the error which I am getting after entering the password:
For HTTPS URL, you need to use a PAT (Personal Access Token) with scope repo as a password.
And you need to cache it using a credential helper, like Git Credential Manager (cross platform). Once that helper is installed:
git config credential.helper manager
Should you switch to an SSH URL (git#github.com:<me>/<myRepo>), you need, if you have protected your private SSH key with a passphrase, to auto-launch ssh-agent when you start a new session. See "Working with SSH key passphrases".

Github login best way from CLI / Git Bash

Ever since Github changed it's policy to not accept username/password anymore, I'm having a difficult time logging in when I push a branch to the remote repo.
I've tried a PKA key, but I forget it sometimes and I have to regenerate. Then I stored it in a plain text file and copy-paste when needed. And this PKA key seems like an API key for one project, instead of a way to log in manually to push changes. This seems to me like not-the-way to go. And I can imagine professionals do this in a better way.
I'm using a Linux machine and git from the terminal. And a Windows 10 machine with Git Bash (Git for Windows). How should I configure my account and computers so that I can easily run git push origin master?
Is it possible to use 2FA in a way where I log in with my account username and password and verify my login with an authenticator app on my phone? And preferably trust my computer from future logins.
It seems like you want to store your credentials globally on your machine. Store your credentials in global git configuration file.
WARNING: Your credentials would be visible to everyone who uses your machine, but the config file is usually hidden /.config.
Store your credentials on your machine globally:
git config --global credential.helper store
Next time you enter your credentials, this will store your credentials. In next consequent times, you won't be prompt to enter credentials anymore.

How to solve an error while cloning a private repository

I have a repository which is private. Before, I was able to clone it from my terminal by giving the username and password. Few days back I got a mail from GitHub to enable 2FA in my account. I did but now I see my account password doesn't seem to work when I give my password in my terminal. I use Google Authenticator as my TOTP app and I also tried giving that password but still it says authentication failure. Please help me out
You need a "Personal Access Token" (PAT).
In order to generate one, go to your GitHub settings and click on the category called "Developer settings". In that section, go to personal tokens.
There you can generate a new access token (make surer to check repo). When you clone your repository, use this token instead of your password. Now you should be able to clone it.
You can read more about this process here: https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token

Owner can't push to organization repository

today I've moved my private github repository to newly created organization (of which I'm an owner). I've changed my remote origin to git#github.com:organization/repo.git. I'm not able to push to remote because I've got an error saying
Error: Permission to organization/repo.git denied to MyGithubUsername.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
I've checked that my account is owner of organization and repository exists.
Disable third-party application access restrictions as suggested in atom/github #1010.422882361.
I ran into this problem today and indeed as Ephellon Dantzler suggests, disabling 3rd-party access restrictions fixed it.
However, I did not really like that solution so I started reading the documentation. On the About OAuth App access reistrictions page, Github mentions that enabling 3rd party restrictions will block:
SSH keys created before February 2014 immediately lose access to the organization's resources (this includes user and deploy keys).
I created my key well before that so I just created a new SSH key and added to Github following their instructions.
After that, I was able to interface with my organization's private repository even when 3rd-party access restrictions was enabled.
I managed to find an answer. Maybe I did something wrong, or maybe it was because of github issues, but even if I was stated as an owner of Organization I had only rights to read repository. I couldn't change permission for myself because "you can't change permission for owner". I changed permissions for all members to admin, and because for now I'm the only one member of organization it's not a big deal.
The solution I found to this problem was creating a P.A.T (Personal Access Token) and using it in the spot of my password when pushing to the repo.
The way you can create a P.A.T is by going to:
Github.com >Login> Settings> Developer Settings> Personal Access Tokens > Generate New Token. I selected all the options for my token and then copied the token that was generated and used it as my password when I was asked to enter my credentials.
This took me a few hours to figure out and was very frustrating. I hope this info can help someone else!
Using GitHub Desktop..I was having a weird bug where I was owner of the organization, and admin of the repo(I had full rights) on my organization's repo. I decided to go into the command line, and push from there (Type "git push") and that fixed it!
It pushed, and I could now push from GitHub Desktop again.
Weird, but hope it helps someone!
For me, everything was ok in the Settings and with my key. Yet Github Desktop could not push (or even fetch) to/from the remote because it didn't trust the IP address. To find and fix I switched to the CLI and ran:
git fetch
which replied with
The authenticity of host 'github.com (140.82.114.4)' can't be established.
RSA key fingerprint is SHA256:<long string>.
Are you sure you want to continue connecting (yes/no)?
therefore revealing the issue. Either Github has changed their IP or I hadn't connected to this repo on my new machine (with the old key). Replying yes added the IP to my .ssh/known_hosts and then Github Desktop worked just fine again.
Warning: Permanently added 'github.com,140.82.114.4' (RSA) to the list of known hosts.
So I ran into this issue recently with an SSH key generated in 2018 and Ephellon Grey's answer pointed me in the right direction. My personal SSH key was added to GitHub by SourceTree and SourceTree was not an authorized third-party app on my organization page. Hence pushes authorized with that SSH key were being rejected. I just had to grant third-party access to SourceTree on the organization and now I can push to repos contained within.

Why does github keep asking me for repo credentials?

We recently moved our github from one account owner to another, and now all of the sudden when I do a git pull or any git command on the remote repo, it asks me for github username and password.
My git-config says:
[github]
user = kamilski81
token = *********
Any idea how I can stop it asking me for credentials and remember everything, does the new owner have to setup my ssh keys or something of that sort?
Following this article sorted it out for me:
https://help.github.com/articles/why-is-git-always-asking-for-my-password
git remote set-url origin git#github.com:user/repo.git
The github username and token in the gitconfig is only used for interactions with the GitHub API (for instance the hub gem and the github tool).
If you are pushing and pulling from a GitHub repository over https, then you need to specify your credentials every time, or hook into a password manager to remember the credentials for you. How to do this depends on your platform.
The easiest thing to do is to go to your github account and submit your SSH public key to your account, and then switch your github remote to push/pull over ssh instead of https.
[edit] After re-reading the question I noticed that you mentioned it was previously a GitHub repo that was just moved from one owner to another. If that's the case, and you ARE able to push and pull by specifying your credentials, it sounds to me to be one of two scenarios:
The previous remote used ssh, and GitHub has your SSH key; but when the owner changed and you updated the remote, you updated it to go over https.
The previous remote used https, but you had a password manager setup correctly to deal with your credentials, which no longer works.
After doing "remote -v"....the issue was that my remote was using an "https" remote rather than "git" remote. Changing the remote to use git rather than https solved everything.
If you execute only once git pull and Git client still asking you the credential without do the git pull for you, the problem should be because your credential is incorrect or not setup yet.
but if you get the dialog ask for credential one for each operation, it mean that your credential isn't remembered. To do that, you may use Pageant to store your SSH private key, so, everytime you do something, it will retrieve your credential from Pageant instead to asking you.