How do I commit changes to a repository while I am 1 behind and 2 ahead? - github

So I am 1 behind and 2 ahead on this GitHub repository.
When I got to pull to solve the 1 behind problem it gives me this output:
git --no-optional-locks -c color.branch=false -c color.diff=false -c color.status=false -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree fetch origin
git#github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I'm not really sure what this means and would like to solve this issue as soon as possible and upload some files onto the repository.
I'm a beginner when it comes to GitHub and SourceTree.

It means the way SourceTree uses SSH fails to authenticate you on GitHub.
Check first if, in command line, ssh -T git#github.com works: see "Testing your SSH connection".
Then check sourceTree settings: as shown here, I would recommend using the OpenSSH client, not the putty one (assuming you have used ssh-keygen -t rsa -P "" -m PEM to generate your ssh private/public key pair, and not putty, or that you are not on Windows)

Related

Messed up with deploy key

When I try to push my files to the git server I faced this problem.
`~ git push
fatal: The current branch main has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin main
So, I've entered git push --set-upstream origin main in the terminal. It was showing up the following error.
ERROR: Permission to adivenkat05/C-Assignments.git denied to deploy key
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
So, after searching for the solution, it was that we need to upload our ssh key in the git server (Deploy Key).
I've uploaded that same key to another repository but it ended up showing me this "So, after searching for the solution, it was that we need to upload our ssh key in the git server (Deploy Key).
I've uploaded that same key to another repository but it ended up showing me this "Key is already in use".
So, it's not possible to have multiple ssh keys in a particular machine, right?
How do I tackle this problem?".
The second error is different, and refer to an authentication problem.
You can have multiple keys, a deploy one and one associated with your user account.
Create a new dedicated key, and add it to your account:
ssh-keygen -t rsa -P "" -f ~/.ssh/me
Reference it in a ~/.ssh/config file:
Host gh
Hostname github.com
User git
IdentityFile ~/.ssh/me
IdentitiesOnly yes
And your SSH URL becomes gh:me/myRepository. (no more git#github.com:...)
You can test your SSH key with ssh -Tv gh.
It it works:
cd /path/to/my/local/repo
git remote set-url origin gh:adivenkat05/C-Assignments.git
git push -u origin main

vscode not connecting to multiple gitlab accounts using their respective ssh keys

So for windows, I see the default ssh client config supposed to be placed at C:\Users\Admin\.ssh\config and I use the same, also I am the Admin user.
I added the following ssh client side config:
PS C:\Users\Admin> type .\.ssh\config
Host gc
HostName gitlab.com
User git
IdentityFile ~/.ssh/company_gitlab
Host gp
HostName gitlab.com
User git
IdentityFile ~/.ssh/gitlab_personal
I tested it from Powershell and it is working for both my company and personal GitLab accounts using their respective ssh keys.
PS C:\Users\Admin> ssh -T git#gc
Enter passphrase for key 'C:\Users\Admin/.ssh/company_gitlab':
Welcome to GitLab, #johnwilson!
PS C:\Users\Admin> ssh -T git#gp
Enter passphrase for key 'C:\Users\Admin/.ssh/gitlab_personal':
Welcome to GitLab, #jwilson!
But when it comes to the vscode, when I try to push or pull changes to the remote repository, the following error shows up for both the company and personal GitLab accounts.
> git push -u gc master
git#gitlab.com: Permission denied (publickey,keyboard-interactive).
fatal: Could not read from remote repository.
> git push -u gp master
git#gitlab.com: Permission denied (publickey,keyboard-interactive).
fatal: Could not read from remote repository.
I tried the suggestion from here as well
PS C:\Users\Admin\ownCloud\Company\GitLab\userlist> git remote add origin git#gc:company/infra_code/userlist.git
PS C:\Users\Admin\ownCloud\Company\GitLab\userlist> git remote -v
origin git#gc:company/infra_code/userlist.git (fetch)
origin git#gc:company/infra_code/userlist.git (push)
PS C:\Users\Admin\ownCloud\Company\GitLab\userlist> git ls-remote origin
Enter passphrase for key 'C:\Users\Admin/.ssh/company_gitlab':
611b36ef47056773c288499cb6974d8671196d78 HEAD
611b36ef47056773c288499cb6974d8671196d78 refs/heads/master
I am confused, so vscode does not pick the default ssh client config C:\Users\Admin\.ssh\config on windows ?.
Or am I missing something?.
Check the remote URL associated with the remote named 'gc'
cd C:\path\to\gc\repo
git remote -v
If the URL starts with git#gitlab.com:..., it won't use the ~/.ssh/config file, and its gc entry.
Change it with the proper URL
git remote set-url gc gc:<me>/<myrepo>
Then VSCode will pick up the right entry, assuming is is running as User Admin.
The OP JohnW confirms in the discussion:
I added an SSH key without a passphrase, which is working for vscode. So this is bug with VSCode cannot connect with ssh key with passphrase?
You need to make sure ssh-agent is activated first.
See "Auto-launching ssh-agent on Git for Windows"
Once the SSH agent is launched (and your private SSH keys passphrase cached in it), you can launch VSCode. It should work then.

git push to github in a batch job sumitted to a server using qsub

I set a ssh-agent and gave it an id_rsa key, so I don't have problem using git command on a terminal of a linux server to update to github, but when I try to do this by a bash script using qsub to submit the job to the server, I got the following error messages,
On branch master
nothing to commit, working directory clean
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
my shell script is,
#!/bin/bash
git add *.png
git commit -m "upload_fig"
git push
I also set ~/.ssh/config like the following,
Host remoteHost
ForwardAgent yes
Host github
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
Anyone knows how to solve this issue?
Thanks!
In addition: I have set id_rsa key to github and I can use git command on the local terminal.
What you are looking for might be in what you are looking for might be in this. I'm guessing you didn't add your public key, ~/.ssh/id_rsa, to your github account.

Error "The authenticity of host 'github.com' can't be established. RSA key fingerprint "

I use my project at work, but I would like to work with him from home as I can log into my home machine to work with my project.
However, from home, I see the following message:
The authenticity of host 'github.com (ip)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)?
How can I get past it?
You should simply be able to answer 'yes', which will update your ~/.ssh/known_hosts file.
A better approach, to avoid any MITM (Man-In-The-Middle) attack, would be (as commented below by Mamsds) to verify Github's public key first (see "GitHub's SSH key fingerprints") and, if you find a match, then you can answer 'yes'.
Example:
ssh-keyscan -t ecdsa github.com 2>&1 |ssh-keygen -lf -
256 SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM github.com (ECDSA)
After that, you can use a GitHub SSH URL (provided you have generated the SSH public/private keys, and registered the public one to your GitHub profile)
Note: the ssh key generation should use the base64 old PEM format (option -m PEM), rather than the new current 70 chars OpenSSH one.
See "What is the correct format for private key in Credentials":
ssh-keygen -m PEM -t rsa -P "" -f afile
That or you can switch to an HTTPS URL.
As you are attempting to connect to Github using SSH for the first time (no existing entry for Github in ~/.ssh/known_hosts yet), you are being asked to verify the key fingerprint of the remote host. Because, if an intruder host represents itself as a Github server, it's RSA fingerprint will be different from that of a GitHub server fingerprint.
You have two options.
You may just accept, considering you don't care about the authenticity of the remote host (Github in this case), or,
You may verify that you are actually getting connected to a Github server, by matching the RSA fingerprint you are presented to (in the prompt), with GitHub's SSH key fingerprints in base64 format.
The latter option is usually more preferable.
Just add Github fingerprint to known hosts this way:
mkdir -p ~/.ssh
ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts
Use one of the following two solutions:
1) Set up the SSH key
Follow the steps discussed on this GitHub help page.
https://help.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh
2) Clone using git with HTTPS
Type (copy/paste) the following commands in a terminal on the machine where you would like to clone the repository
git config --global url."https://github.com/".insteadOf git#github.com:
git config --global url."https://".insteadOf git://
You can revert this change using the following commands
git config --global url."git#github.com:".insteadOf https://github.com/
git config --global url."git://".insteadOf https://
Try these steps:
Open Git Bash
Check for existing SSH keys:
$ ls -al ~/.ssh
If you already have them, you will see:
id_rsa.pub
id_ecdsa.pub
id_ed25519.pub
If you don't, generate one (Press Enter to accept the default file location):
$ ssh-keygen -t rsa -b 4096 -C "your_email#example.com"
To copy the key to clipboard:
$ clip < ~/.ssh/id_rsa.pub
Go to your account on Github/Settings/SSH and GPG keys/New SSH key
Paste your key there
Next, type:
$ git remote
If you see origin, remove it:
$ git remote remove origin
Continue with the last 2 steps provided on GitHub repo page...
$ git remote add origin git#github.com:USERNAME/REPONAME.git
$ git push -u origin master
Refresh your GitHub repo page
Voila!
1- Create SSH key
if you don't have the ssh-key create it like this:
ssh-keygen -t rsa -b 4096 -C "youremail#example.com"
2- Check your SSH key
For this go to your folder ssh, example:
cd ~/.ssh
after that, run this command line : ls
if you have the files: id_rsa, id_rsa.pub so the all is good
now you need to copy your id_rsa.pub ! (⚠️ NOT the id_rsa)
for this run cat id_rsa.pub and copy the result
3- Github / Gitlab
Go to your github / gitlab.
Follow the step by your hosting:
github: Click on your profile -> Settings -> SSH and GPG Keys -> New SSH key
gitlab: Click on your profile -> preferences -> SSH keys
past your id_rsa.pub that you have already copied !
4- If you have already did it and it's already not working ??
When your enter your ssh-key in github / gitlab your key have a Expiration date so you just need to change it
That it ! Have a good work 😀
You just need to type yes and it will work, for more information you can refer to the Official Github documentation. This will give an output saying
Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.

Permission denied (public key) during fetch from GitHub with Jenkins user on Ubuntu

Here is my setup:
Jenkins is running on my linux machine as 'jenkins' user.
I have generated a ssh key-pair as described in Linux - Setup Git, for the 'jenkins' user.
When I sudo su jenkins and try ssh -vT git#github.com, I am always asked my passphrase, but I am always eventually authenicated. (the verbose option shows which key is used, among others).
I could clone my repo from GitHub using jenkins:
Thusly:
jenkins#alpm:~/jobs/test git/workspace$ git pull
Enter passphrase for key '/var/lib/jenkins/.ssh/id*_rsa':
Already up-to-date.
Up to this point I have followed the instructions to the letter. The problem is that the Jenkins job fails with the following error:
status code 128:
stdout:
stderr: Permission denied (publickey).
fatal: The remote end hung up unexpectedly
This is same error as I get when I typo the passphrase (but of course, Jenkins does not ask me for the passphrase). The following pages:
GitHub - SSH Issues
Using SSH Agent Forwarding
indicate to me that ssh-agent could help remember the passphrase, which it does when I am using my own user, but not the jenkins id. Note that while running as my normal user yields:
echo "$SSH_AUTH_SOCK"
/tmp/keyring-nQlwf9/ssh
While running the same command as my 'jenkins' yields nothing (not even permission denied)
My understanding of the problem is that the passphrase is not remembered.
Do you have any idea?
Shall I start a ssh-agent or key ring manager for the jenkins user? How?
Or is ssh forwarding suitable when forwarding to the same machine?
Any brighter idea?
ps: I never sudo gitted, I always used jenkins or my user account (as mentioned in this SO post - Ubuntu/GitHub SSH Key Issue)
Since nobody wrote the answer from the comments for several months, I will quickly do so.
There are 2 possible problems/solutions:
id_rsa created with wrong user
Create id_rsa as the jenkins user (see hudson cannot fetch from git repository)
Leave passphrase empty
To summarise what must be done on the Jenkins server:
# 1. Create the folder containing the SSH keys if necessary
if [ ! -e ~jenkins/.ssh ]; then mkdir ~jenkins/.ssh; fi
cd ~jenkins/.ssh/
# 2. Create the SSH pair of keys
# The comment will help to identify the SSH key on target systems
ssh-keygen -C "jenkins" -f ~jenkins/.ssh/id_rsa -P ""
# 3. Assign the proper access rights
chown -R jenkins ~jenkins/.ssh/
chmod 700 ~jenkins/.ssh
chmod 600 ~jenkins/.ssh/*
Remember:
Please keep the default "id_rsa" name when generating the keys, as other such as "id_rsa_jenkins" won't work, even if correctly set up.
Do not use a passphrase for your key
Check that the public key (id_rsa.pub) has been uploaded on the git server (GitHub, Bitbucket, etc). Once done, test your SSH key by running: ssh -vvv git#github.com (change address according to your git server)
I got around this problem by simply leaving the passphrase empty when creating the keys.
I would add that if you created the keys by hand, they might still be owned by you and not readable by jenkins, try:
sudo chown jenkins -R /var/lib/jenkins/.ssh/*
To check are the following:
if the right public key (id_rsa.pub) is uploaded to the git-server.
jenkins user will access to github -> to CHECK if the right private key (id_rsa) is copied to /var/lib/jenkins/.ssh/
if the known_hosts file is created inside ~/.ssh folder. Try ssh -vvv git#github.com to see debug logs. If thing goes well, github.com will be added to known_hosts.
if the permission of id_rsa is set to 755 (chmod 755 id_rsa)
After all checks -> try ssh -vvv git#github.com
Dont try to do config in jenkins until ssh works!
If you are running jenkins as a service in windows, you need to verify the user running the service. If you created the keys using the user "MACHINENAME\user", change the service so the user running it can match
For Mac users, the issue can be solved by removing the existing keys and creating new Private and Public Keys by following these steps:
1.Remove all Public and Private keys located at /Users/Username/.ssh
2.Remove all the credentials saved under the Credentials tab in Jenkins.
3.Remove the existing Public SSH keys defined in the Github Repository Settings.
4.Create new SSH keys (private and public: id_rsa and id_rsa.pub) by following the steps from https://confluence.atlassian.com/bitbucketserver/creating-ssh-keys-776639788.html#CreatingSSHkeys-CreatinganSSHkeyonLinux&MacOSX
5.Set the newly created public SSH key (id_rsa.pub) in Github or an equivalent Repository Settings.
6.In Jenkins,create new credentials by adding the private SSH key(id_rsa) for your Github username.
7.The Error should be removed now.
keys need to generated from jenkins user.
sudo su jenkins
ssh-keygen
once the key is generated, it should be added as ssh key in bitbucket or github.