Pushing to git with Personal Access Token is asking for password - linux

I have a shell script file which will push the files to empty git repo.
git init
git add .
git commit -m "first commit"
git remote add origin https://<my-personal-access-token>#github.com/<organization-name>/<repo-name>.git
git push -u origin master
This was pushing files to repo without asking for password, but surprisingly now it is asking for password too.
Why is it asking for password even though I used personal access token? What is going wrong here?
Even if I input correct password manually, it says as below :

You can try below line in your code for git remote step by adding username to it:
git remote add origin https://username:<my-personal-access-token>#github.com/<organization-name>/<repo-name>.git

Related

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

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.

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)

Gitlab http basics access denied

I tried using this commands before getting errors
git remote add name url
git add .
git commit -m 'message'
git push -u name master
The question should be more clear. Error messages should be posted.
If you logged in GitLab with GitHub account, you'll have to set up a password for GitLab in order to log into GitLab account.
The fastest way to do so is to change your password in GitLab. You should see only new password fields.
After that, you'll have no trouble pushing.

Bitbucket git issue on Linux

I am very new to bitbucket and linux. I am trying to upload files from my local to bitbucket. I tried the following command:
git commit -m 'commit to master'
git push -u origin 'master'
Then I got an error like
fatal: 'origin' does not appear to be a git repository fatal: Could
not read from remote repository.
Please make sure you have the correct access rights and the repository
exists.
Then I tried
git remote add origin ssh://mjsofttechindia#bitbucket.org:mjsofttechindia/project.git
git push origin master
Then I got an error like
ssh: Could not resolve hostname
bitbucket.org:yourname: Name or service not known fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
Please help. I don't know if this is correct or not. Please help me.
Change remote url to
ssh://mjsofttechindia#bitbucket.org/mjsofttechindia/project.git
git remote set-url origin ssh://mjsofttechindia#bitbucket.org/mjsofttechindia/project.git
Then you have to configure your name and email address (the one that you created your Bitbucket account with) in your local repository
git config user.name "Your Name"
git config user.email "you#email.com"
Then do a push again
git push -u origin master

Git requires username and password for git push, git pull using HTTPS method

So, when I use HTTPS method for cloning git repository and do some changes and push or pull to git it will always be prompting me for username and password. what's the solution for that?
Apart from changing to SSH, you can also keep using HTTPS, if you don't mind to put your password in clear text. Put this in your ~/.netrc and it won't ask for your username/password (at least on Linux and Mac)
Make one .netrc file in your home directory.
machine github.com
login <user>
password <password
The solution to your problem is this git command which is kind of remember me of GIT.
git config credential.helper store
Read this for details:
GIT credentials store

Resources