727

I've been using Git for a while now, but the constant requests for a password are starting to drive me up the wall.

I'm using Mac OS X and GitHub, and I set up Git and my SSH keys as instructed by GitHub's Set Up Git page.

I've also added the github SSH key to my Mac OS X keychain, as mentioned on GitHub's SSH key passphrases page. My public key is registered with Git.

Nevertheless, every time I try to Git pull, I have to enter my username and password. Is there something other than an SSH key that I need to set up for this?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Catherine
  • 12,548
  • 9
  • 34
  • 58
  • 1
    Silly question, but have you verified that the SSH key works when simply using ssh to the machine with git? – Kurt Stutsman Oct 14 '11 at 20:27
  • 4
    You mean something like ssh -T git@github.com ? Yeah, that works just fine (if a little slow). – Catherine Oct 14 '11 at 20:34
  • 2
    See also [Git push requires username and password](http://stackoverflow.com/questions/6565357/git-push-requires-username-and-password). –  Aug 22 '13 at 13:06
  • For https url, you can use (with git1.8.3+) a git credential helper '`netrc`'. See a [full example here](http://stackoverflow.com/a/18362082/6309). – VonC Aug 22 '13 at 13:14
  • 1
    I am a Windows user, and I was also facing password request issue even after adding my public key to authorized_keys file of server. What really the problem was that I was not keeping my public/private keys under .ssh folder of c:\program files\git folder. If anyone is facing such issue, please copy your keys in this folder and try pushing/pulling. – Raja Amer Khan Nov 28 '14 at 20:28
  • Set the remote url value in the `.git/config` file of the repository to `git@github.com:{username}/{repo}.git` where you replace with your own username and the repo name, obviously. The selected answer is flat-out incorrect. – Benjamin R Sep 26 '17 at 07:05
  • What helped me: `ssh -Tv git@example.com` (replacing example.com with your GitLab domain) Try to push your changes. If it still doesn't work try this: `git remote rm origin; git remote add origin git@git.:/.git; git push -u origin --all; git push -u origin --tags;` If it still doesn't work, try to re add the private key in the website. Last thing to try, add the key from bash: `ssh-add id_rsa` – E235 Nov 29 '17 at 13:29
  • You might need to configure your computer with a SSH key and you won't need to constantly submit your password every-time you push the code. – Muema Mar 09 '18 at 19:41
  • for people having trouble cloning with an ssh key, make sure you use the ssh clone url as well – otaku Aug 04 '20 at 02:36
  • `git config --global credential.helper cache` – Peter Krauss Oct 09 '20 at 21:31

31 Answers31

844

I think you may have the wrong Git repository URL.

Open .git/config and find the [remote "origin"] section. Make sure you're using the SSH one:

ssh://git@github.com/username/repo.git

You can see the SSH URL in the main page of your repository if you click Clone or download and choose ssh.

And NOT the https or git one:

https://github.com/username/repo.git
git://github.com/username/repo.git

You can now validate with just the SSH key instead of the username and password.

If Git complains that 'origin' has already been added, open the .config file and edit the url = "..." part after [remote origin] as url = ssh://github/username/repo.git


The same goes for other services. Make sure the address looks like: protocol://something@url

E.g. .git/config for Azure DevOps:

[remote "origin"]
    url = https://mystore@dev.azure.com/mystore/myproject/
    fetch = +refs/heads/*:refs/remotes/origin/*
Bizhan
  • 13,495
  • 9
  • 53
  • 83
static_rtti
  • 46,349
  • 44
  • 123
  • 180
  • 5
    This may be it. When I call git remote -v I get: origin https://github.com/Foo/Bar.git (fetch) origin https://github.com/Foo/Bar.git (push) whereas to work with SSH it seems that it should be: origin git@github.com:Foo/Bar.git (fetch) origin git@github.com:Foo/Bar.git (push) This may be because I originally checked out my project using GitHub's Mac application (http://mac.github.com/). Any idea how I can fix it? – Catherine Oct 14 '11 at 22:38
  • 8
    Either fix the url in the .git/config file, use git-remote to fix it, or delete your local repo and clone it again with the correct URL. – static_rtti Oct 14 '11 at 22:49
  • 94
    Just to spell this out (as I needed it): open .git/config and in the `[remote "origin"]` section set `url = ssh://git@github.com/username/Repo.git`. That worked for me. – Greg K Jul 01 '12 at 23:22
  • 13
    Odd, @GregK's solution didn't work for me, but `url = git@github.com:organization/Repo.git` did work. +1 for leading me down the right path though! Success! – jmort253 Nov 19 '12 at 21:52
  • I'm curious why you describe the https url as the "wrong" one, when the git documentation describes it as the recommended method: https://help.github.com/articles/generating-ssh-keys ? Don't get me wrong, I prefer SSH connection myself, but there seems to be a disparity in recommendations. – scubbo May 23 '13 at 11:09
  • 3
    I can't stress enough how important it is to CLONE using ssh://git@github/[username]/[reponame].git is you want to use an key to access GIT not have to type your username and password each time. I found the only way to fix it was to remove the local repo and clone again $ git clone ssh://git@github/[username]/[reponame].git – Gene Myers Aug 19 '13 at 17:03
  • 2
    @GeneM. I'm pretty sure you can simply change the origin url of your clone in .git/gitrc to avoid re-cloning. – static_rtti Aug 24 '13 at 19:20
  • https URLs work just fine in MacOSX if you use @orkoden's osxkeychain solution – neu242 May 09 '14 at 06:25
  • 1
    Editing `.git/config` directly is not recommended when there is a git command for doing the same thing: `git remote set-url origin git@github.com:account/repository`. The reason is you may accidentally corrupt the contents of `.git/config`. – Petros Aug 09 '16 at 07:39
  • 2
    Just adding that this worked for me, but I did NOT include the `ssh://` prefix to the ssh github url. That is, I simply set the url to `git@github.com:username/repo.git` – LyteSpeed Mar 14 '17 at 04:50
  • 1
    Remote url should be `git@github.com:username/repo.git` - not this nonsense. – Benjamin R Sep 26 '17 at 07:03
  • 2
    `Permission denied (publickey). fatal: Could not read from remote repository. ` – user25 May 13 '18 at 15:49
574

Configuring credential.helper

On OS X (now macOS), run this in Terminal:

git config --global credential.helper osxkeychain

It enables Git to use file Keychain.app to store username and password and to retrieve the passphrase to your private SSH key from the keychain.

For Windows use:

git config --global credential.helper wincred

For Linux use:

git config --global credential.helper cache // If you want to cache the credentials for some time (default 15 minutes)

OR

git config --global credential.helper store // if you want to store the credentials for ever (considered unsafe)

Note: The first method will cache the credentials in memory, whereas the second will store them in ~/.git-credentials in plain text format.

Check here for more info about Linux method.
Check here for more info about all three.

Troubleshooting

If the Git credential helper is configured correctly macOS saves the passphrase in the keychain. Sometimes the connection between SSH and the passphrases stored in the keychain can break. Run ssh-add -K or ssh-add ~/.ssh/id_rsa to add the key to keychain again.

macOS v10.12 (Sierra) changes to ssh

For macOS v10.12 (Sierra), ssh-add -K needs to be run after every reboot. To avoid this, create ~/.ssh/config with this content.

Host *
    AddKeysToAgent yes
    UseKeychain yes
    IdentityFile ~/.ssh/id_rsa

From the ssh_config man page on 10.12.2:

UseKeychain

On macOS, specifies whether the system should search for passphrases in the user's keychain when attempting to use a particular key. When the passphrase is provided by the user, this option also specifies whether the passphrase should be stored into the keychain once it has been verified to be correct. The argument must be 'yes' or 'no'. The default is 'no'.

Apple has added Technote 2449 which explains what happened.

Prior to macOS Sierra, ssh would present a dialog asking for your passphrase and would offer the option to store it into the keychain. This UI was deprecated some time ago and has been removed.

Community
  • 1
  • 1
orkoden
  • 16,170
  • 3
  • 55
  • 49
  • 31
    This is by far the best way to do this since the Github application for OSX (maybe Windows as well) uses the https path for git repos by default. There is also a good reason for using https rather than ssh/git since many corporate networks only allow traffic on port **80** and **443** for security reasons. – codehugger Sep 18 '12 at 02:11
  • 2
    You need git 1.7.10 or newer to use the credential helper. – jbandi Dec 27 '12 at 23:34
  • 3
    +1 for keychain.app solution. This is **GREAT** for me because my employer's git server only supports http served via apache and they strongly discourage using the `.netrc` file method which puts your password in plaintext. – Josh Mar 01 '13 at 21:12
  • 3
    Note, I got the error `'credential-osxkeychain' is not a git command.` as I didn't have the credential helper installed. I followed the instructions here to install it: https://help.github.com/articles/set-up-git#password-caching – Hugh Mar 11 '13 at 13:00
  • 1
    Yes you need to install git with the credential helper. Depending on how you install it (homebrew, macports, source, etc.) you might need to pass some compile flags. For macports, install it with port install git-core +credential_osxkeychain – orkoden Mar 20 '13 at 17:12
  • 1
    For windows use: `git config --global credential.helper wincred` – Andrey Luiz Jan 06 '15 at 00:07
  • Is it still using the git ssh key? I want to make sure I am not bypassing the security and using a less secure the username and password method. – Christian Juth Apr 16 '15 at 18:32
  • Yes it is. The password you save in Keychain.app is the one that unlocks the private SSH key on your machine. Your username and password for the git server you use should be different. – orkoden Apr 17 '15 at 09:57
  • **Error** : git: 'credential-osxkeychain' is not a git command. See 'git --help'. – cyb3rZ Nov 03 '15 at 15:53
  • 2
    Thanks for the Sierra update, I just updated and couldn't figure out why github kept asking for pw when I added the `git config --global credential.helper osxkeychain` The config file in my `.ssh` directory fixed me up. – pixel 67 Dec 22 '16 at 13:07
  • 1
    Mac Sierra 10.2 was the reason this keeps prompting me. `ssh-add -K` doing this fixed the problem and added the missing Identity File back to mac. – newdark-it Jan 03 '17 at 15:57
  • I've been pulling my hair out over the Sierra change. Modifying ~/.ssh/config did the trick! – Peter Hinson Jan 07 '17 at 04:20
  • 1
    Great solution. Though note, you don't even need the `Host *` bit, if you put the rest at the top-level. – Noldorin Jan 31 '17 at 03:48
  • 2
    @AndreyLuiz Hey do you know how to do it in linux? – Tommaso Thea Cioni May 14 '17 at 19:07
  • This answer must be marked as correct one. Came here after installing Sierra, and `ssh-add -K` together with creating `.ssh/config` file did the trick. – jeron-diovis Dec 07 '17 at 15:09
  • For *Linux* use `git config --global credential.helper cache` – hashlock Sep 15 '19 at 05:41
148

This happened to me when I upgraded to macOS v10.12 (Sierra). Looks like the SSH agent got cleared upon upgrade.

$ ssh-add -L
The agent has no identities.

Simply running ssh-add located my existing identity. I entered the password and was good to go again.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
amcc
  • 2,459
  • 1
  • 17
  • 27
70

Use this: Replace github.com with the appropriate hostname

git remote set-url origin git@github.com:user/repo.git
marteljn
  • 6,140
  • 2
  • 28
  • 42
Darrell Duane
  • 3,785
  • 1
  • 16
  • 8
  • I already have that set when viewing `git remote -v`. Yet, I am still being prompted for password. – IgorGanapolsky Apr 20 '15 at 19:18
  • Same, I have set as what he suggested, but I still get prompt for password, how do I prevent that ? – cyb3rZ Mar 06 '17 at 16:59
  • @IgorGanapolsky : Did you ever found a solution for you ? – cyb3rZ Mar 06 '17 at 16:59
  • 1
    This works properly. This is weird that remote URL with `git@**` doesn't prompt credentials and URL with `https@**` does :). Thus I found [this doc](https://help.github.com/articles/why-is-git-always-asking-for-my-password/). – Sujit Kumar Singh Nov 15 '18 at 06:02
70

As others have said, you can install a password cache helper. I mostly just wanted to post the link for other platforms, and not just Mac. I'm running a Linux server and this was helpful: Caching your GitHub password in Git

For Mac:

git credential-osxkeychain

Windows:

git config --global credential.helper wincred

Linux:

git config --global credential.helper cache
git config --global credential.helper 'cache --timeout=3600'
# Set the cache to timeout after 1 hour (setting is in seconds)
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Chase Roberts
  • 8,262
  • 10
  • 64
  • 118
  • 9
    I had to scroll down to the third answer to see the Linux instructions, these should be in the primary answer since it is a commonly referenced issue. – KoldBane May 29 '16 at 23:17
23

Also look for who is asking you for the passphrase. Is it Git or your SSH agent?

In my case, every time I did git pull it was asking me:

Enter passphrase for key '/work/username/.ssh/id_rsa':

So I assumed it was Git asking for a passphrase. So I kept hunting for solutions, only to realize later that my SSH agent had shut down. Which can be fixed using eval $(ssh-agent) and ssh-add as given here.

Also am pasting below a little snippet you can add to your ~/.bashrc file (or the equivalent) to ensure that your SSH agent is started on your login.

In any case this was a pretty silly mistake I made, but posting it here, just in case it helps someone save some time from barking up the wrong tree, like I did.

# Start the ssh-agent
function start_agent {
    echo "Initializing new SSH agent..."

    # Spawn ssh-agent
    /usr/bin/ssh-agent | sed 's/^echo/#echo/' > ${SSH_ENV}
    echo succeeded
    chmod 600 ${SSH_ENV}
    . ${SSH_ENV} > /dev/null
    /usr/bin/ssh-add
}

if [ -f "${SSH_ENV}" ]; then
     . ${SSH_ENV} > /dev/null
     ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
        start_agent;
    }
else
    start_agent;
fi

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
mithunpaul
  • 2,559
  • 17
  • 18
  • Pretty sure you are missing a line in the code above - as per https://stackoverflow.com/a/18915067/277023 you need to have `SSH_ENV="$HOME/.ssh/environment"` – kellyfj Apr 07 '20 at 12:32
20
git config credential.helper store

Note: While this is convenient, Git will store your credentials in clear text in a local file (.git-credentials) under your project directory (see below for the "home" directory). If you don't like this, delete this file and switch to using the cache option.

If you want Git to resume to asking you for credentials every time it needs to connect to the remote repository, you can run this command:

git config --unset credential.helper

To store the passwords in .git-credentials in your %HOME% directory as opposed to the project directory: use the --global flag

git config --global credential.helper store
Lokesh S
  • 386
  • 3
  • 13
10

In Windows for Git 1.7.9+, run the following command on the command prompt to open the configuration file in a text editor:

git config --global --edit

Then in the file, add the following block if not present or edit it accordingly:

[credential "https://giturl.com"]
username = <user id>
helper = wincred

Save and close the file. You will need to provide the credentials only once after the above change.

Community
  • 1
  • 1
Rohan
  • 599
  • 5
  • 16
9

Guide to Git on Windows and GitHub using SSH to push/pull: An Illustrated Guide to Git on Windows

  1. Download and install PuTTY
  2. Set environment variable 'GIT_SSH' = 'path\to\plink.exe' (in installed putty folder) - very important!!!
  3. Restart Windows Explorer for environment variables to take effect (cannot only restart command prompt)
  4. Run puttygen.exe to generate new key, copy the public key to the GitHub site
  5. Save this new private key somewhere safe on the disk (preferable not Dropbox)
  6. Run putty.exe and connect SSH to github.co
  7. Quickly get to startup folder by running "shell:startup".
  8. Make your private key startup with Windows via pageant. Create a shortcut in Startup folder with syntax "path\to\pageant.exe" "path\to\privatekey"
  9. We do not need to set the 'puttykeyfile' setting inside .git/config of our repositories
  10. Very important is that the "SSH clone URL" of GitHub is used and not HTTPS.
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Francois
  • 1,521
  • 2
  • 13
  • 8
  • Nobody should be using putty anymore. You can do everything now with built in Windows 10 utilities. Let putty, just be putty. – Ian Smith May 23 '20 at 22:11
9

Use the following command to increase the timeout period so that you could retype password for a while

git config --global credential.helper 'cache --timeout 3600'

I used it for Bitbucket and GitHub it works for both. The only thing you need to do is 3600 is in seconds. Increase it to whatever extent you want. I changed it to 259200 which is about 30 days. This way I re-enter my password for every 30 days or so.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Ravi Kumar
  • 492
  • 5
  • 7
7

I feel like the answer provided by static_rtti is hacky in some sense. I don't know if this was available earlier, but Git tools now provide credential storage.

Cache Mode

$ git config --global credential.helper cache

Use the “cache” mode to keep credentials in memory for a certain period of time. None of the passwords are ever stored on disk, and they are purged from the cache after 15 minutes.

Store Mode

$ git config --global credential.helper 'store --file ~/.my-credentials'

Use the “store” mode to save the credentials to a plain-text file on disk, and they never expire.

I personally used the store mode. I deleted my repository, cloned it, and then had to enter my credentials once.

Reference: 7.14 Git Tools - Credential Storage

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Ducktales
  • 194
  • 3
  • 11
  • For Windows `credential.helper cache` does NOT work. It should be `git config --global credential.helper wincred`. – Paulo Merson Jul 17 '19 at 16:36
  • Storing it worked and now it allows me to commit my changes without prompting for a password all the time, you're a lifesaver! – g4ost Sep 06 '19 at 15:49
  • 1
    In Linux this did the job for me `git config --local credential.helper store`. In my case, I'm over HTTPS, in this way I only type username/password one time and after that, my credentials are reused from ~/.git-credentials – CCarlos Jan 07 '20 at 14:39
7

If you want to stop Git from always asking you for the login credentials of your GitHub repository this can be easily done.

Using SSH instead of HTTPS

You can update the origin remote using SSH instead of HTTPS"

git remote set-url origin git@github.com:username/your-repo.git

Configure Git to Store your Password and Username

Here’s how you can make Git store the username and password:

git config --global credential.helper store

Next, save the username and password for a session:

git config --global credential.helper cache
5

I figure you fixed your problem, but I don't see the solution here that helped me, so here it is.

Type in terminal:

echo "" > ~/.ssh/known_hosts

That will empty your known_hosts file, and you'll have to add every host you used and have connected to, but it solved the problem.

Marko
  • 19,347
  • 13
  • 45
  • 61
papan
  • 75
  • 1
  • 1
  • 1
    `cat /dev/null > ~/.ssh/known_hosts` will do the same. – the Tin Man Sep 28 '12 at 01:03
  • 5
    `> ~/.ssh/known_hosts` is even shorter :) – Collin Allen Oct 17 '12 at 15:25
  • rm ~/.ssh/known_hosts should do the job too. I would advise against this though. – orkoden Mar 20 '13 at 17:13
  • 3
    If you need to remove a host from ~/.ssh/known_hosts, there is a less impactful way than wiping out the file. ~/.ssh/known_hosts is just a text file and if you can find the offending hosts in the file, you can just delete their lines. You may want to back up the file before editing it. If you only have a few entries in the file, then wiping it out might not be a bad idea. I work on lots of servers so my ~/.ssh/known_hosts has hundreds of entries in it and I'm not quite ready to delete them all to remove a few entries. – Tim Stewart Sep 18 '14 at 15:46
  • 3
    @papan This does not prevent git from asking me for my password! – IgorGanapolsky Apr 20 '15 at 19:19
  • Try that, still prompt for password ... :( – cyb3rZ Mar 06 '17 at 17:02
5

On Windows Subsystem for Linux (WSL) this was the only solution that I found to work:

eval `ssh-agent` ; ssh-add ~/.ssh/id_rsa

It was a problem with the ssh-agent not being properly registered in WSL.

James Gentes
  • 5,882
  • 4
  • 36
  • 52
4

I had the same problem. MacOS Mojave keychain keeps asking for the passphrase. Your id_rsa should be encrypted with a passphrase for security. Then try adding it to the keychain ssh-add -K ~/.ssh/id_rsa

If your key is in another folder than ~/.ssh then substitute with the correct folder.

Keychain now knows your ssh key, hopefully, all works now.

If you are still facing the issue then try

1. brew install keychain

2. echo '/usr/local/bin/keychain $HOME/.ssh/id_rsa' >> ~/.bash_profile
   echo 'source $HOME/.keychain/$HOSTNAME-sh' ~/.bash_profile

3. ssh-add -K ~/.ssh/id_rsa

Hopefully, it should work now.

raghu
  • 444
  • 3
  • 10
  • 1
    Thank you! The top solution didn't work for me but using `ssh-add` like you said finally resolved the issue for me. – JPProgrammer Jul 03 '20 at 11:28
3

orkoden's answer on using the keychain with Git in your terminal was incomplete and raises errors. This is what you have to do to save the username and password you enter in the the terminal in your keychain:

curl http://github-media-downloads.s3.amazonaws.com/osx/git-credential-osxkeychain -o git-credential-osxkeychain
sudo mv git-credential-osxkeychain /usr/local/bin
sudo chmod u+x /usr/local/bin/git-credential-osxkeychain

Then enter

git config --global credential.helper osxkeychain

If you have already done the part with Git configuration before the curl stuff, it's no problem; it'll work.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Lukas
  • 7,340
  • 10
  • 51
  • 94
3

As static_rtti said above, change

https://github.com/username/repo.git
git://github.com/username/repo.git

to

ssh://git@github.com/username/repo.git

I myself changed the https in the .git/config file to ssh, but it still wasn't working. Then I saw that you must change github.com to git@github.com. A good way to get the actual correct URL is to go to your project page and click this:

Change HTTPS to SSH to get the right URL

Then add this URL to the configuration file.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
3

Running macOS Cataline 10.15, the keychain caching method was not working for me. And I wanted to use https:// not ssh

Here is what worked for me:

git remote rm origin

git remote add origin https://your_git_username:your_git_password@github.com/path_to_your_git.git

This should work on GitLab too

Make sure if the username contains an email address, to remove the @email part or you'll get an error stating URL using bad/illegal format or missing URL.

Hope this helps!

ICeZer0
  • 149
  • 5
  • 1
    This is the ONLY solution that worked for me. your_git_username was critical. It was actually not necessary to put :your_git_password. At this point, even though it still asked for a "passphrase", it would now accept the account password. – kbulgrien May 25 '20 at 01:48
3

Git will prompt you for a passowrd if you are using https protocol. If you use ssh, it will do the authentication using your private key instead of asking for password. (https://help.github.com/en/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account)

Here is how to fix this:

git remote -v

This will show the url for origin. And you will notice https in this url. (Example : https://github.com/PrestaShop/PrestaShop.git)

Now, you will have to just remove this first and add the url with ssh

git remote remove origin

git remote add origin git@github.com:PrestaShop/PrestaShop.git

Snoi Singla
  • 121
  • 1
  • 3
2

I agree with "codehugger" and using the instruction of "orkoden" it worked for me - on NetBeans 7.3 - when you right-click on the file and select context menu - push - a 'push to remote' window opened - there are two options here:

  1. origin:https://github.com/myaccount/myproject.git/

  2. https://github.com/myaccount/myproject.git/

As you can see, the difference is the origin parameter in the URL - you do not want to choose this option (1) you want to check option (2), and that works just fine for me.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Dung
  • 15,167
  • 6
  • 51
  • 49
2

Step 1: check your current configuration

cat .git/config

You will get:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true
[remote "origin"]
    url = https://github.com/path_to_your_git.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[user]
    name = your_username
    email = your_email
[branch "master-staging"]
    remote = origin
    merge = refs/heads/master-staging

Step 2: remove your remote origin

git remote rm origin

Step 3: add remote origin back with your username and password

git remote add origin https://your_git_username:your_git_password@github.com/path_to_your_git.git
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Haimei
  • 11,243
  • 3
  • 44
  • 34
  • After you've added the remote origin again you also need to align your branches remote and local - something like this: `git branch --set-upstream master origin/master` – Richard Bown Sep 27 '16 at 19:59
2

If Git prompts you for a username and password every time you try to interact with GitHub, you're probably using the HTTPS clone URL for your repository.

Using an HTTPS remote URL has some advantages: it's easier to set up than SSH, and usually works through strict firewalls and proxies. However, it also prompts you to enter your GitHub credentials every time you pull or push a repository.

You can configure Git to store your password for you. For Windows:

git config --global credential.helper wincred
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Emiliano Schiano
  • 1,657
  • 1
  • 18
  • 28
1

There are different kind of authentications depending on your configuration. Here are a few:

  1. git credential-osxkeychain.

    If your credential is invalid, remove it by:

      git credential-osxkeychain erase
    

    or:

      printf "protocol=https\nhost=github.com\n" | git credential-osxkeychain erase
    

    So Git won't ask you for the keychain permission again. Then configure it again.

    See: Updating credentials from the OS X Keychain at GitHub

  2. Your SSH RSA key.

    For this, you need to compare your SSH key with what you've added, check by ssh-add -L/ssh-add -l if you're using the right identity.

  3. Your HTTPS authentication (if you're using https instead of ssh protocol).

    Use ~/.netrc (%HOME%/_netrc on Windows), to provide your credentials, e.g.

      machine stash1.mycompany.com
      login myusername
      password mypassword
    

Learn more: Syncing with GitHub at Stack Overflow.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
kenorb
  • 118,428
  • 63
  • 588
  • 624
1

Before you can use your key with GitHub, follow this step in the tutorial, Testing your SSH connection:

$ ssh -T git@github.com
# Attempts to ssh to GitHub
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Jonny
  • 1,973
  • 20
  • 30
1

Having a typo in the URL will make Git asking you for username and password, stupid Git.

It was tested on Kali Linux, Git version 2.7.0,

Try:

git clone https://github.com/thisrepodoesntexists/doesntexists.git

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Gianluca Ghettini
  • 9,354
  • 14
  • 63
  • 132
1

If you're using Windows and this has suddenly started happening on out of the blue on GitHub, it's probably due to GitHub's recent disabling support for deprecated cryptographic algorithms on 2018-02-22, in which case the solution is simply to download and install the latest version of either the full Git for Windows or just the Git Credential Manager for Windows.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
stovroz
  • 6,315
  • 2
  • 42
  • 57
0

Microsoft Stack solution (Windows and Azure DevOps)

First open the .git/config file to make sure the address looks like:

protocol://something@url

E.g. .git/config for Azure DevOps:

[remote "origin"]
    url = https://mystore@dev.azure.com/mystore/myproject/
    fetch = +refs/heads/*:refs/remotes/origin/*

If the problem still persists, open Windows Credential Manager, click on the safebox named Windows Credentials and remove all the git related credentials.

Now the next time you log into git, it won't go away anymore.

Bizhan
  • 13,495
  • 9
  • 53
  • 83
0

If you have SSH agent set up, you can also add this to your ~/.gitconfig to force git to use SSH for all GitHub repos rather than HTTPS:

[url "ssh://git@github.com/"]
    insteadOf = git://github.com/
    insteadOf = https://github.com/

(If you're mostly working with public repos, you can also use pushInsteadOf rather than insteadOf, as reading from a public repo can be done without authentication).

nyuszika7h
  • 12,020
  • 5
  • 40
  • 49
0

I had this issue. A repo was requiring me to input credentials every time. All my other repos were not asking for my credentials. They were even set up to track GitLab using HTTPS under the same account credentials, so it was weird that they all worked fine except one.

Comparing the .git/config files, I found that there were 2 key differences in the URLs which was causing the issue:

Does ask for credentials:

https://gitlab.com/myaccount/myproject

Does not ask for credentials:

https://myaccount@gitlab.com/myaccount/myproject.git 

So adding the "myaccount@" and ".git" resolved the issue in my case.

0

In my case, I was always getting a password prompt when I tried to cherrypick a URL like below:

git fetch http://username@gerrit-domainname/repositoryname refs/changes/1/12345/1 && git cherry-pick FETCH_HEAD

This URL worked well when cherrypicked on a different machine. However, At my end when I try to cherrypick with correct password abc@12345 I used to get below error:

remote: Unauthorized
remote: Authentication failed for http://username@gerrit-domainname  

In my git config file the remote URL was like below:

url = ssh://gerrit-domainname:8080/wnc

Solution:
I resolved the authentication failure issue by providing the HTTP Password which I found at
My gerrit account -> Settings -> HTTP Password.

The HTTP Password was something like Th+IsAduMMy+PaSS+WordT+RY+Your+OwNPaSs which was way different than my actual password abc@12345

Note: My URL to cherrpick starts with git fetch ... So, this solution might work on git checkout/download where the URL starts with git fetch ...

Rohit Gaikwad
  • 2,992
  • 3
  • 12
  • 32
0

IF you are mandated to go through a jumpbox and only if that is the case, try the following:

ssh-copy-id <username>@<jumpbox-host>

This is very much an edge case and it is assuming you have a jumbox configured in your ~/.ssh/config file.

thoroc
  • 2,152
  • 2
  • 22
  • 28