Creating ssh key in github - github

I have a question regarding managing two Github accounts in my machine (office and personal). Whenever I create a repo in personal am I supposed to create a new ssh key(As my office git account username and email are global)? Using windows credentials way I'm prompted to enter the username and password for the first time I do remote push then onwards I need not have to do that for that particular repo. I wanted to try by ssh(to avoid entering my git account credentials for the first time) or can I stick with the windows credential manager way?

Whenever I create a repo in personal am I supposed to create a new ssh key(As my office git account username and email are global)?
No: the SSH key remains the same and is used to authenticate you to your personal GitHub account.
You only need to register that key in your ~/.ssh/config file:
Host perso
Hostname github.com
User git
IdentityFile ~/.ssh/myPersoKey
Then, in your local cloned repository:
cd /path/to/local/clone
git remote set-url origin perso:<me>/<myrepo>
Then a git push will use the SSH URL, with the right perso key.

Related

handling multiple git account for different repos [duplicate]

So, I have my work computer and that is connected to my GitHub Enterprise Account (github.company.com) on the terminal. Now I want to set up my personal account (github.com) on here too.
I've been following this tutorial - https://code.tutsplus.com/tutorials/quick-tip-how-to-work-with-github-and-multiple-accounts--net-22574
and on step 3 when I have to create my config file should my HostName be github.company.com or github.com? Can I have any (meaningful) name for Host? Also, what does User here mean?
Also, how do I switch between both these accounts on the terminal - i.e. my personal and my enterprise account? There are certain things I need to commit from my personal account, and use the enterprise account with the rest.
Detailed steps to use two GitHub accounts on the same PC as below:
1. Create SSH key for the github.company.com account
If you already added the SSH key to your github.company.com account, then skip this step.
First, use ssh-keygen to create id_rsa and id_rsa.pub in C:\Users\username\.ssh.
Then, add the content of id_rsa.pub file as a SSH key in github.company.com account.
2. Create SSH Key for your personal account github.com
Use ssh-keygen -t rsa -C "email address for the personal github account", and save the key in /c/Users/username/.ssh/id_rsa_personal.
Now add the content of id_rsa_personal.pub file as a SSH Key in your personal GitHub account.
3. Config the two SSH Keys
In C:\Users\username\.ssh diectory, create a config file with below content:
Host github.company.com
HostName github.company.com
User git
IdentityFile ~/.ssh/id_rsa
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
4. Work with the two GitHub accounts by SSH protocol
You can clone your github.company.com account by:
git clone git#github.company.com:companyaccountname/reponame
And then add the personal account as a remote in the local repo by:
git remote add personal git#github-personal:personalaccountname/reponame
To get the file from personal account to company repo by below commands:
git merge upstream master --allow-unrelated-histories
# make changes
git add .
git commit -m 'add the changes from peronal repo'
git push origin master
To commit/push changes from local company repo by the committer of the personal repo, you just need to re-config the username and email before committing changes in the local repo:
git config user.name <personal github account username>
git config user.email <email for login the personal github account>
When you want to commit changes with the company account, just re-config username and email again before committing changes.

Github: I have to make a new SSH key after every use

I have a school and a personal Github, so I made an SSH key for my personal account and linked it up. It always works only once, then gives me
Push Failed
Git#github.com: Permission denied (publickey). Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
It works fine again after I delete the old SSH and make a new one, but this is obviously a huge hassle. I am on Windows 10, using Git Bash, and IDE is IntelliJ.
Any ideas? Thanks.
I solved this problem in the following way:
1.Create a file named config in the path $USER_HOME/.ssh/, then add following content.
# For school account
Host school_github
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_school
# For personal account
Host personal_github
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_personal
2.Run following command using git bash:
Change your school github repository configuration
git remote rm origin
git remote add origin git#school_github:your_github_username/your_repo_name.git
You can hava a try.
You don't need to create SSH keys for each git service just upload the same public key to both personal and school accounts and it will work.
SSH public key is tied to you local account you have generate SSH keys only if you are using different local(machine) account or different machine
Seems to me like when you try to authenticate, you are probably not using your keys.
If you run the command
ssh-add -l
You (should) see a list of all your keys. Each time you start a new session, make sure to start up the agent and add your keys.
eval `ssh-agent`
ssh-add ~/.ssh/id_rsa

Can't push to git

Once I had an old github account, and I'm trying to create a new one and push to that one, but I keep running into permission issues:
remote: Permission to <new account>/<new account>.github.io.git denied to <old username>.
I've tried setting the user name + password, going through different procedures to add ssh keys, deleting my .ssh folder, deleting my old repos, everything I can think of, but I can't fix this.
Check first if your URL is actually an SSH one:
git remote -v
If it is an https,... no amount of SSH setting will allow you to authenticate properly.
If it is an https (again), do check your credential helper with:
git config credential.helper
If you see manager, it is possible the wrong credentials are cached.
In that case, you need to remove them.
See "Github remote permission denied".
The other possibility is the presence of 2FA (2 factor Authentication), which would require a PTA (Personnal Access Token) in place of the regular account password.
But again, you can also switch to an SSH URL:
git remote set-url origin git#github.com:auser/arepo.git

GitHub: invalid username or password

I have a project hosted on GitHub. I fail when trying to push my modifications on the master. I always get the following error message
Password for 'https://git#github.com':
remote: Invalid username or password.
fatal: Authentication failed for 'https://git#github.com/eurydyce/MDANSE.git/'
However, setting my ssh key to github seems ok. Indeed, when I do a ssh -T git#github.com I get
Hi eurydyce! You've successfully authenticated, but GitHub does not provide shell access.
Which seems to indicate that everything is OK from that side (eurydyce being my github username). I strictly followed the instructions given on github and the recommendations of many stack discussion but no way. Would you have any idea of what I may have done wrong?
After enabling Two Factor Authentication (2FA), you may see something like this when attempting to use git clone, git fetch, git pull or git push:
$ git push origin master
Username for 'https://github.com': your_user_name
Password for 'https://your_user_name#github.com':
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/your_user_name/repo_name.git/'
Why this is happening
From the GitHub Help documentation:
After 2FA is enabled you will need to enter a personal access token instead of a 2FA code and your GitHub password.
...
For example, when you access a repository using Git on the command line using commands like git clone, git fetch, git pull or git push with HTTPS URLs, you must provide your GitHub username and your personal access token when prompted for a username and password. The command line prompt won't specify that you should enter your personal access token when it asks for your password.
How to fix it
Generate a Personal Access Token. (Detailed guide on Creating a personal access token for the command line.)
Copy the Personal Access Token.
Re-attempt the command you were trying and use Personal Access Token in the place of your password.
Related question:
https://stackoverflow.com/a/21374369/101662
https://git#github.com/eurydyce/MDANSE.git is not an ssh url, it is an https one (which would require your GitHub account name, instead of 'git').
Try to use ssh://git#github.com:eurydyce/MDANSE.git or just git#github.com:eurydyce/MDANSE.git
git remote set-url origin git#github.com:eurydyce/MDANSE.git
The OP Pellegrini Eric adds:
That's what I did in my ~/.gitconfig file that contains currently the following entries [remote "origin"] url=git#github.com:eurydyce/MDANSE.git
This should not be in your global config (the one in ~/).
You could check git config -l in your repo: that url should be declared in the local config: <yourrepo>/.git/config.
So make sure you are in the repo path when doing the git remote set-url command.
As noted in Oliver's answer, an HTTPS URL would not use username/password if two-factor authentication (2FA) is activated.
In that case, the password should be a PAT (personal access token) as seen in "Using a token on the command line".
That applies only for HTTPS URLS, SSH is not affected by this limitation.
Solution steps for Windows users:
Control Panel
Credential Manager
Click Windows Credentials
In Generic Credential section ,there would be git url, update username and password
Restart Git Bash and try for clone
Note:
If you didn't find git url in Generic Credential section then follow below answer
https://stackoverflow.com/a/55858690/7372432
If like me you just updated your password and ran git push to run into this issue, then there's a super easy fix.
For Mac users only. You need to delete your OSX Keychain access entries for GitHub. You can do it via terminal by running the following commands.
Deleting your credentials via the command line
Through the command line, you can use the credential helper directly to erase the keychain entry.
To do this, type the following command:
git credential-osxkeychain erase
host=github.com
protocol=https
# [Now Press Return]
If it's successful, nothing will print out. To test that it works, try and clone a repository from GitHub or run your previous action again like in my case git push. If you are prompted for a password, the keychain entry was deleted.
When using the https:// URL to connect to your remote repository, then Git will not use SSH as authentication but will instead try a basic authentication over HTTPS. Usually, you would just use the URL without a username, e.g. https://github.com/username/repository.git, and Git would then prompt you to enter both a username (your GitHub username) and your password.
If you use https://something#github.com/username/repository.git, then you have preset the username Git will use for authentication: something. Since you used https://git#github.com, Git will try to log in using the git username for which your password of course doesn’t work. So you will have to use your username instead.
The alternative is actually to use SSH for authentication. That way you will avoid having to type your password all the time; and since it already seems to work, that’s what you should be using.
To do that, you need to change your remote URL though, so Git knows that it needs to connect via SSH. The format is then this: git#github.com:username/repository. To update your URL use this command:
git remote set-url origin git#github.com:username/repository
Instead of git pull also try git pull origin master
I changed password, and the first command gave error:
$ git pull
remote: Invalid username or password.
fatal: Authentication failed for ...
After git pull origin master, it asked for password and seemed to update itself
2FA is enabled and getting error remote: Invalid username or password.
fatal: Authentication failed for
If you set 2FA is enabled in GitHub you will need to enter a personal access token instead of a 2FA code and your GitHub password.
How to fix it
https://github.com/settings/tokens generated token
Copy the Personal Access Token
Now enter Personal Access Token in the place of your password during git operation
just try to push it to your branch again. This will ask your username and password again, so you can feed in the changed password. So that your new password will be stored again in the cache.
This is the answer.
Set the github token:
https://github.com/settings/tokens
And then:
git remote set-url origin https://[token]#github.com/your_repository
I am getting this while cloning app from bitbucket:
Cloning into 'YourAppName'...
Password for 'https://youruser id':
remote: Invalid username or password
I solved it. Here you need to create password for your userid
Click on Your profile and settings
Then Create app password choose your name password will generated ,paste that password to terminal
That problem happens sometimes due to wrong password. Please check if you are linked with AD password (Active Directory Password) and you recently changed you AD password but still trying git command with old password or not.
Update old AD password
Control Panel > Credential Manager > Windows Credential > change github password with my new AD password
I have got the success using the following commands.
git config --unset-all credential.helper
git config --global --unset-all credential.helper
git config --system --unset-all credential.helper
Try and let me know if these are working for you.
No need to rely on Generating a Personal Access Token and then trying and use Personal Access Token in the place of your password.
Quick fix is to set your remote URL to point to ssh not https.
Do this git remote set-url origin git#github.com:username/repository
I did:
$git pull origin master
Then it asked for the [Username] & [Password] and it seems to be working fine now.
If you have just enabled 2FA :
Modify hidden config file in ./git hidden folder as follow :
[remote "origin"]
url = https://username:PUT_YOUR_2FA_TOKEN_HERE#github.com/project/project.git
Try this:
# git remote set-url origin git#github.com:username/repository
Run Below command, and after than on every push and pull it will ask you to enter the username and password.
git config credential.helper ""
now when you pull/push you will be asked for git credentials. weather you are running through command prompt or Intellij Git.
Disabling 2 factor authentication at github worked for me.
I see that there is a deleted answer that says this, with the deletion reason as "does not answer the question". If it works, then I think it answers the question...
You might be getting this error because you have updated your password. So on Terminal first make sure you clear your GitHub credentials from the keychain and then push your changes to your repo, terminal will ask for your username and password.
In case you get this error message in this situation:
using github for entreprise
using credential.helper=wincred in git config
using your windows credentials which you changed recently
Then look at this answer:
https://stackoverflow.com/a/39608906/521257
Windows stores credentials in a credentials manager, clear it or update it.
Control panel
Credential manager
Look for options webcredentials and windows credentials
in either one you will find github credentials fix it with correct credentials
open new instance of git bash you should be able to perform your git commands.
This worked for me, I was able to pull and push into my remote repo.
I had the same issue. And I solved it by changing the remote branch's path from https://github.com/YourName/RepoName to git#github.com:YourName/RepoName.git in the repo's settings of the client app.
I'm constantly running into this problem.
Make sure you set git --config user.name "" and not your real name, which I've done a few times..
I just disable the Two-factor authentication and try again. It works for me.
Since you probably want to keep 2FA enabled for your account, you can set up a ssh key and that way you won't need to type your Github credentials every time you want to push work to Github.
You can find all the ssh setup steps in the documentation. First, make sure you don't currently have any ssh keys (id_rsa.pub, etc.) with $ ls -al ~/.ssh
I fixed my issue by installing GitHub CLI and running gh auth login
See:
https://docs.github.com/en/get-started/getting-started-with-git/caching-your-github-credentials-in-git#github-cli
I had the same issue
$ git clone https://github.com/sample-url.git
Cloning into 'Project'...
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/sample-url.git/'
I just git init first and then git clone <clone-url>
git init
git clone https://github.com/your-clone-Url
It worked for me.
There is a issue on Windows using cmd-Greetings
There is a issue on Windows using cmd-Greetings who will not let you clone private repositories. Remove that cmd-greeting described in this documentation (keyword Command Processor):
Known-Issues
I can confirm that other clients like SourceTree, GitKraken, Tower and TortoiseGit affected to this issue too.
There are many reasons why this might happen. In my case, none of the solutions worked. In particular, git pull origin master did not ask me for my username and password.
I was on Windows with a github password recently changed. I was using the credential manager to manage my password. Here is what worked for me:
Confirm you are using the credential manager for git:
git config --list
…
credential.helper=manager
Run a new command prompt as administrator
List all stored credential with cmdkey /list from C:\WINDOWS\system32>
Remove the github target with cmdkey /delete:<target name>. In my case, the target name was github.<companyname>.com
Open a new prompt and run a git command. You should get a popup asking for your usernmame and password. After providing the new credentials, it won't ask you for it again.
When I faced this issue all I did to resolve it was to Generate new token from my github dashboard and paste the following code in my terminal
$ git remote set-url origin https://your-github-username:your-github-token#github.com/your-github-username/your-github-repo.git

Github wrong username

I have setup with guthub with a previous github account on this computer called myaccount when i run this command
ssh -T git#github.com
Hi myaccount! You've successfully authenticated, but GitHub does not provide shell access.
Ok but i have now setup a new github account called samaccount but i cant run this command i get this error.
git push -u origin master
i get this error
ERROR: Permission to samueleast/Samuel-East-S3-Audio-Playlist-Player.git denied to myaccount.
I dont want it to use myaccount it should be using samaccount !!!
i have followed all the steps here http://help.github.com/mac-set-up-git/
setup brand new ssh key.
and also run this command many times
git config --global user.name "samaccount"
but it still uses myaccount its sooo annoying where am i going wrong???
GitHub recognizes you as myaccount because SSH is using a key that corresponds to a public key that has been added to the myaccount account on the GitHub website.
If you want to switch entirely to using the sameueleast account instead of myaccount, you can do the following:
Login to GitHub's website as myaccount
Go to "Account Settings"
Go to "SSH Public Keys"
Delete your public key from that list
Logout
Login to GitHub's website as samueleast
Go to "Account Settings" -> "SSH Public Keys"
Select "Add another public key" and paste in the contents of your public key. You public key will (probably) be called id_rsa.pub or id_dsa.pub and be in your .ssh directory.
On the other hand, if you want to be able to use either myaccount or samueleast when pushing to GitHub, you will need to create aliases for git#github.com in ~/.ssh/config, such as:
Host github-myaccount
User git
Hostname github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_dsa-myaccount.pub
Host github-samueleast
User git
Hostname github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_dsa-samueleast.pub
That assumes you've generated a second SSH keypair and given them names as above. Then you need to make sure that you are using the appropriate alias in the URLs of your git remotes. For example, if you want to change your origin remote so that pushing to origin means "pushing to origin as samueleast", you could do:
git remote set-url origin \
git#github-samueleast:samueleast/Samuel-East-S3-Audio-Playlist-Player.git