git#gitlab.com: Permission denied (publickey). fatal: Could not read from remote repository - gitlab

I am using macOS Catalina. I already have a repository on GitLab and an SSH-key assigned. Now I want to create another repository from the terminal. I do the following:
git config user.name my_name
git config user.email my_email
git init
Then I get this:
Initialized empty Git repository in directory
So far so good.
git remote add origin git#gitlab.com:my_name/repo.git
git add .
git commit -m 'commit'
git push -u origin master
Then I get the following error:
git#gitlab.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Then I go to the repository I already had and try to push there, everything works so I guess I don't have a problem with SSH-key. I know this is a very common question on the internet but none of the answers solved my problem.

First, you should get "Initialized empty Git repository in directory" only after a git init ., not after a git remote add origin ...
Second, with GitLab, you can push to create a new project, as illustrated in this MR, starting with GitLab 10.5 (Q1 2018)
Third, if the error persists, then the key is somehow at fault.
Test it with:
ssh -Tv git#gitlab.com
Also
git -c core.sshCommand="ssh -v" push -u origin master
To generate a valid key:
ssh-keygen -t rsa -P "" -m PEM
And register your new id_rsa.pub to your GitLab profile.

I tried all the above mentioned solutions but none of it worked. I then read the logs and found that it is looking for the key in a specific folder and I created the key and added it to my Gitlab profile too. Then it started working.
Git authentication issue can be solved by reading the logs of the git and creating appropriate SSH keys under appropriate folders.
Steps
Run the following command and it will try to push the code and if it not successful then it will display where the error is
git -c core.sshCommand="ssh -v" push -u origin master
Now, we can generate a new SSH key and the following command will generate a key in the working folder.
ssh-keygen -t rsa -P "" -m PEM
It will ask for key name, you can give id_rsa as the key name or any name which the Bash displays as "Trying private key: c:/Users/Dell/.ssh/".
Once the key is generated in bash, your working directory will have the key.
While running the command in step1, you will see that the folder in which it is looking for a private key. In my case it is "c:/Users/Dell/.ssh/id_rsa"
We should put the generated keys from the working folder into this folder
 
We should also make sure that we add our SSH Key to the Gitlab account.
Click on your Gitlab account MyProfile and select preferences.
Click to see how to add SSH to your Gitlab account
 
Click the SSH keys menu, open the generated key file using notepad and copy the content of the key from notepad and paste it in the SSH key text editor and save it .
Click to see how to add SSH Key to your Gitlab account
Again, run the following command and check now. The code will be pushed.
git -c core.sshCommand="ssh -v" push -u origin master
the code will be pushed.

The same issue happened.
I used HTTPS instead of SSH
(I followed the instruction steps after creating repo in GitLab but that cause a Permission issue. It's is because of ssh pub key to upload)
These steps work without using SSH
Create a repository/project in GitLab
I removed .git (that caused permission issue in previous. For to start with fresh)
git config --global user.name "user_name"
git config --global user.email "user.email#gmail.com"
git init .
git remote add origin https://gitlab.com/user.account/user_project.git
git add . and git commit -m "initial commit"
git push -u origin master
It will ask username and password. Then fixed.

Related

Can't interact with github

My laptop runs arch linux. I wanted to use github to backup my project. In the beginning everything worked fine, I was able to push the first two commits.
Then I wanted to push the third commit but it took forever. From then on every communication (push, pull, clone etc. on multiple repositories) with the remote repository took forever and ended with the error: fatal: could not read from remote repository. There was one exception where i was able to push. Afterwards it was the same again.
The ssh authentication is working. ssh git#github.com delivers the expected response. (ssh -T git#github.com takes forever and is not successful - is this relevant?)
There are a few similar threads but I haven't found a working answer.
What i have already tried:
Checked remote repository (git remote -v): -- is correct
Started ssh-agent and added key (eval `ssh-agent`, ssh-add)
Forcing ssh to use IPv4 (in ~/.ssh/config: AddressFamily inet)
Removed git enviroment variable (unset GIT_SSH)
Tried multiple keys (rsa, ed25519) with/without password
Used the url wiht ssh:// scheme (ssh://git#yourhost:port/path/repo.git)
Updated git
Complete update of the os
Credential helper disabled (git config --global --unset credential.helper)
Checked credentials (git config --global user.name , git config --global user.email)
I even tried to trace the git pull:
GIT_TRACE=2 GIT_TRACE_PACK_ACCESS=2 GIT_TRACE_PACKET=2 GIT_TRACE_PERFORMANCE=2 GIT_TRACE_SETUP=2 git push -u origin main --verbose
With the result:
14:10:30.980851 trace.c:312 setup: git_common_dir: .git
14:10:30.980857 trace.c:313 setup: worktree: /home/alex/Entwicklung/NetCore/ActivityTrackerPC
14:10:30.980863 trace.c:314 setup: cwd: /home/alex/Entwicklung/NetCore/ActivityTrackerPC
14:10:30.980868 trace.c:315 setup: prefix: (null)
14:10:30.980872 git.c:460 trace: built-in: git push -u origin main --verbose
Push nach github.com:2kHammer/ActivityTracker.git
14:10:30.984208 run-command.c:655 trace: run_command: unset GIT_PREFIX; ssh git#github.com 'git-receive-pack '\''2kHammer/ActivityTracker.git'\'''
Then it hangs until the error occures.
For now i copied the project to my raspberry und pushed from there. There it worked without problems.
I am really desperate, git is an essential tool.
Try to re add your ssh key to your ssh agent
ssh-add ~/.ssh/id
where id is the ssh key is the key associated with your github repository

Git clone with ssh works in Linux but not in Windows PowerShell

I have a repo at GitHub. When cloning it in Linux using ssh it works fine:
> git clone git#github.com:henrikppersson74/frokenjennnie.git
Cloning into 'frokenjennnie'...
Enter passphrase for key '/home/----/.ssh/id_rsa':
.
.
Reinitialized existing Git repository in /home/*
When doing the same thing in Windows PowerShell it doesn't work:
> git clone git#github.com:henrikppersson74/frokenjennnie.git
Cloning into 'frokenjennnie'...
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 have copied my private and public SSH keys from Linux to my Windows machine and they are stored in my ~/.ssh/ as id_rsa and id_rsa.pub.
It seems to work when I try to access github.com with ssh from PowerShell:
> ssh git#github.com
Enter passphrase for key 'C:\Users\-----/\.ssh\id_rsa':
PTY allocation request failed on channel 0
Hi henrikppersson74! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.
This is the same answer I get when doing this in Linux. I guess this means the my SSH-key par is ok?
When working in Eclipse in Windows, using the "Git Repositories" view it works fine to clone the same repo.
Previously I created new SSH keys in the PowerShell and copied the public one to GitHub, but with the same result.
My ~/.ssh/config file lookes like this:
Host github.com
HostName github.com
IdentityFile ~\.ssh\id_rsa
User git
ForwardAgent yes
I am using Git version:
> git --version
git version 2.28.0.windows.1
Unfortunately I get no extra information from using the --verbose flag:
> git clone git#github.com:henrikppersson74/frokenjennnie.git --verbose
Cloning into 'frokenjennnie'...
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.
It doesn't help to add the key to the ssh-agent:
> ssh-add C:\Users\-----\.ssh\id_rsa
Enter passphrase for C:\Users\-----\.ssh\id_rsa:
Identity added: C:\Users\------\.ssh\id_rsa (C:\Users\-----\.ssh\id_rsa)
> git clone git#github.com:henrikppersson74/frokenjennnie.git --verbose
Cloning into 'frokenjennnie'...
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.
Could my old keys be cashed somewhere?
Does anyone have any idea about why I am not able to clone my repo in Windows PowerShell? I would be so grateful for some help.
The problem was that my %HOME% environment variable was set to the wrong location. When I changed it back to C:\Users\<userid> it worked like charm. Apparently SSH first looks for a key in %HOME%\.ssh\, then for en entry in %HOME%\.ssh\config\ and last it uses the keys added to the ssh-agent.

Gitlab pushing first project failed due to could not read from remote repository

I was new to Gitlab was trying to push my project from local machine to Gitlab.
Have done the SSH key and followed the instructions at Gitlab. Done the Git global setup. Was trying to add an existing folder , so i followed the instructions listed
cd existing_folder
git init
git remote add origin https://gitlab.com/sss/testnode.git
git add .
git commit -m "Initial commit"
git push -u origin master
but failed at the last step at the git push. The error message was
Tried adding the remote origin, but it was told it already exists. So not sure where it went wrong. Please help, much appreciated :)
Have done the SSH key
The problem is that you have defined your origin as HTTPS, not SSH.
Try:
git remote set-url origin git#gitlab.com:sss/testnode.git
That will override origin URL.
Independently, make sure your SSH key does work and allows GitLab to authenticate you as your GitLab account with:
ssh -T git#gitlab.com
Check out your credentials, if they are invalid, it wont give u to upload changes.
For Windows check this: https://www.digitalcitizen.life/credential-manager-where-windows-stores-passwords-other-login-details
For Linux check this: https://askubuntu.com/questions/30907/password-management-applications (if you do not know how to change credentials via terminal)

Git remote pull using GitHub deployment keys - Permission Denied

I have done the following steps to setup ssh deployment keys with our git repo for it to be able to git pull without a username and password:
Note: I am on AWS EC2 / Ubuntu 14.04.3
Run ssh-keygen -t rsa -b 4096 -C "ownersEmail#gmail.com" these are then saved as id_rsa and id_rsa.pub in ~/.ssh/
The deployment public key (id_rsa.pub) is added on the GitHub online UI in the deployment keys section
The directory is already cloned in /var/www/ directory, this is working all good via HTTPS for pulling
Try sudo git pull git#github.com:ownersUsername/OurRepo.git and get the following error
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Another Note: This repository is private under another users account.
Also, when I try ssh git#github.com I get:
Hi userName/Repo! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.
And the deployment key comes up as being used. Have been on this issue for greater than 4 hours now and any would would be very much appreciated, thanks.
The problem is you're using sudo, which runs the command as root, and it will try to use the root's keys not your user's keys.
What you want to do is:
give your user/group write access to /var/www
run the pull/clone as the user, not the root user.
When you do a git pull you don't need the link.
git pull <remote> <branch>
You need the full url for the clone command
sudo git clone git#github.com:ownersUsername/OurRepo.git
To test if your ssh key is good use this:
git fetch --all --prune

Dummy questions about setting up git on amazon cloud ec2

first of all, apologize for dummy questions that I might throw here. It would be nice if you could point the directions where should I go from here.
I'm totally new to version control(as well as git) and cloud system. However, it came to the point that I have to develop php web based application on AWS EC2 instance and make codes contributable for future developers.
I did successfully create EC2 instance that run PHP/MySQL and map the domain with Elastic IP. So the website is now publicly accessible via port 80.
I also installed git using $sudo yum install git and configed user.name and user.email
I then, go to root folder of the website (e.g. public_html) and run ‘git init’ which create the fold “.git” and I then add file using “git add .” and commit “git commit -m ‘initial upload’”
Is that the right way to go? Would it be ok to have the project folder sitting on /public_html (where accessible from anyone).
If above is ok, then where should I go from here?
I would like to have git server running on EC2 that allow developers to connect from their local machines (e.g. Eclipse) while being able to keep the backup and compare the different between codes.
What detail do I suppose to give developers so that they can connect to git server and working on project?
I quick direction or few keywords to do more research would help.
look here for more information on setting up git on amazon ec2
to allow developers to use you git, you just need to give them the git server url.
Direct quote from the site i'm linking to.
"First and foremost, you need to add your EC2 identity to the ssh
authentication agent. This prevents problems with git later, namely
getting the error “Permission denied (publickey).” when trying to do a
git push to the EC2 repository.
ssh-add path/to/privateEC2key.pem
Now you can go ahead and create the git repository on the EC2
instance.
ssh username#hostname.com
mkdir the_project.git
cd the_project.git
git init --bare
So not much going on here, all we do is create an empty repository and
then leave. Now, on the local machine, you do something like the
following:
cd the_project
git init
git add .
git commit -m "Initial git commit message"
git remote add origin username#hostname.com:the_project.git
git config --global remote.origin.receivepack "git receive-pack"
git push origin master
The ‘git config’ command is a fix that I found necessary to be able to
push to the EC2 repository."
The mentioned link by Alex gives a good starting point to setup git on ec2. But I followed a little different approach as mentioned here. link. Direct Quotes from the page:
"Connecting with SSH without a PEM key" : So either you add the ec2 private key and add it as a entity in your ssh authentication agent or create a new ssh key for your user and use that. Steps to be followed are:
Create SSH Key
First up you will need to navigate to your .ssh folder on your local machine :
cd
cd .ssh
if this folder doesn’t exist use mkdir to make it.
Once in your ssh folder on your local machine which should be in /Users/yourusername/.ssh generate your key by executing the following.
ssh-keygen -t rsa -b 1024
When prompted enter the file name to save the key enter id_rsa_aws, when prompted to enter a password leave blank.
In your .ssh directory execute the following command and copy the output to paste later.
cat id_rsa_aws.pub
Now connect to you AWS instance using you PEM key
ssh -i path/to/yourkeyname.pem ubuntu#xx.xxx.xxx.xxx
Once in
echo 'the key you copied from id_rsa_aws.pub' >> .ssh/authorized_keys
chmod 640 .ssh/authorized_keys
chmod 750 .ssh
Now you go to your machine and type
cd desired directory
git clone ubuntu#xx.xxx.xxx.xxx:<path_to_your_just_created_git_server>
If you did all the above mentioned steps correct, the only warning you might get is
warning: You appear to have cloned an empty repository.
That's ok. Now you can copy all your code into the clone directory, and follow the following steps:
git add .
git commit -m "Initial commit"
git push origin master // If working on master branch
i created a GitHub gist with all the details hope it helps
https://gist.github.com/eslam-mahmoud/35777e4382599438023abefc9786a382
//add your EC2 .pem file to ssh kys
ssh-add ~/aws/mypemfile.pem
//create bare repo on AWS EC2 webserver and deploy on demand
mkdir ~/git/the_project
cd ~/git/the_project
git init --bare
//create local repo and track remote one
cd ~/git/the_project
git init
git add .
git commit -m "Initial git commit message"
git remote add aws ubuntu#1.1.1.1:~/git/the_project
git config --global remote.origin.receivepack "git receive-pack"
git push aws master
//create tag
git tag -a v0.1 -m "my version 0.1"
//push tags
git push aws --tags
//Or you have one so you push your updates
git remote add aws ubuntu#1.1.1.1:~/git/the_project
git config --global remote.origin.receivepack "git receive-pack"
git push aws master
//create tag
git tag -a v0.1 -m "my version 0.1"
//push tags
git push aws --tags
//on server create another local repo that track the bare one to deploy
git clone ~/git/the_project
cd ./the_project
//checkout tag
git checkout v0.1
//install clear cache ...
npm install

Resources