Home > Software engineering >  Git prompts for username and password for git push origin master command
Git prompts for username and password for git push origin master command

Time:02-10

enter image description here

Every time I was trying to push the new commits to the master branch through git push origin master command, git was prompting me with credentials. I wanted to convert the url from HTTPS to SSH through these commands:

$git remote add origin https://github.com/Ananya2001-an/FirstRepository
$git config --global credential.helper store
$git config --global credential.helper cache

But now though it is not asking for the credentials, it's still not pushing the commits and instead showing me that Permission to access is denied and I can't read from my remote repository. Kindly help me with this issue that I am facing right now.

CodePudding user response:

First of all, get git repository link from your GitHub account

e.g. https://github.com/imrankabir/my-react-app.git

git remote set-url origin https://[email protected]/imrankabir/my-react-app.git

Notice the username added after https://{imrankabir@}

now run the following command

git config credential.helper store

git pull origin master

it will ask for your GitHub password just for once same it on the machine.

if the future it will not ask for the password again.

CodePudding user response:

I wanted to convert the url from HTTPS to SSH ...

OK:

... through these commands:

$ git remote add origin https://github.com/Ananya2001-an/FirstRepository
$ git config --global credential.helper store
$ git config --global credential.helper cache

That won't work at all. The URL for the remote origin is the last argument to git remote add origin or git remote set-url origin, and for it to be an ssh remote, it must begin with ssh:// rather than https://.1

The credential.helper setting exists specifically for https:// URLs. The git config --global command you're using will remove the old global setting and replace it with a new global setting, so the effect of the first such command is completely wiped away by the second one.

Wherever you got these commands, then, has done you a big disservice: they gave you three wrong commands instead of one right one. On the other hand, if you meant to change the URL from ssh to https—the exact opposite of what you said—they gave you ... well, three wrong commands, still.

Your image (side note: use text, not images; see how to ask) shows that you already have an ssh URL. We can tell because this:

git remote add origin https://github.com/Ananya2001-an/FirstRepository

gives you an error:

error: remote origin already exists.

Meanwhile, your git push says:

[email protected]: Permission denied (publickey).

and this error occurs only for ssh URLs.

If you wish to change the URL of the existing remote origin, use:

git remote set-url origin ...

(filling in the ... part with the desired URL).

To get your existing ssh URL to work, you should:

  1. verify that you have the correct URL; and
  2. debug your ssh connection to GitHub using ssh -Tv [email protected].

You can do these steps in either order.

The credential.helper setting (store or cache) is used only for the https:// URLs; you can leave this set to cache if you like, regardless of what you do for the URL. However, on Windows systems, you might want to use the third-party Git Credential Manager ("GCM"). (I don't use Windows myself and have never used GCM; I use ssh.)


Technically, there are other ways to spell ssh remote URLs for Git, but "what the URL starts with" is a good start. URLs in general take the form https://host.name/path/to/resource or ftp://host.name/path/to/resource or mailto:[email protected] or ssh://host.name/path/to/resource. Note the pattern here: we start with the type, like https or ftp or ssh or mailto, then we have a colon :, then we have //host.name for most cases. Sometimes we have [email protected], and you might use this with ssh:// URLs.

  • Related