242

I uploaded my ~/.ssh/id_rsa.pub to Bitbucket's SSH keys as explained, but Git still asks me for my password at every operation (such as git pull). Did I miss something?

It is a private repository (fork of another person's private repository) and I cloned it like this:

git clone git@bitbucket.org:Nicolas_Raoul/therepo.git

Here is my local .git/config:

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        fetch = +refs/heads/*:refs/remotes/origin/*
        url = https://Nicolas_Raoul@bitbucket.org/Nicolas_Raoul/therepo.git
[branch "master"]
        remote = origin
        merge = refs/heads/master

In the same environment with the same public key, Git on Github works fine.
.ssh is rwx------, .ssh/id_rsa is -rw-------, .ssh/id_rsa.pub is -rw-r--r--

Lemmings19
  • 1,383
  • 3
  • 21
  • 34
Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
  • Maybe [Git keeps prompting me for a password](https://stackoverflow.com/questions/7773181/git-keeps-prompting-me-for-a-password?page=1&tab=trending#tab-top) could be related. Cross site: [How to make git not prompt for passphrase for ssh key?](https://superuser.com/questions/1010542/how-to-make-git-not-prompt-for-passphrase-for-ssh-key) on Superuser or [ssh prompts for password despite .ssh/authorized_keys](https://unix.stackexchange.com/questions/26371/ssh-prompts-for-password-despite-ssh-authorized-keys) on U&L – Cadoiz Sep 14 '22 at 13:58
  • You can simply add password in your config URL, url = https://Nicolas_Raoul:@bitbucket.org/Nicolas_Raoul/therepo.git – Dinesh Patil Feb 01 '23 at 06:56

17 Answers17

355

Are you sure you cloned it using the ssh url?

The url for origin says url = https://Nicolas_Raoul@bitbucket.org/Nicolas_Raoul/therepo.git so if it is using https it will ask for password irrespective of your ssh keys.

So what you want to do is the following:

open your config file in your current repo ..

vim .git/config

and change the line with the url from

[remote "origin"]
        fetch = +refs/heads/*:refs/remotes/origin/*
        url = https://Nicolas_Raoul@bitbucket.org/Nicolas_Raoul/therepo.git

to

[remote "origin"]
        fetch = +refs/heads/*:refs/remotes/origin/*
        url = git@bitbucket.org:Nicolas_Raoul/therepo.git
okandas
  • 11,570
  • 2
  • 15
  • 17
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • 66
    You are right, thanks! I had most probably clone with the wrong URL. I replaced the URL in `.git/config` with `git@bitbucket.org:Nicolas_Raoul/therepo.git` and now it works! – Nicolas Raoul Dec 22 '11 at 07:55
  • 8
    and to change the remote using git http://stackoverflow.com/questions/2432764/how-to-change-a-remote-repository-uri-using-git – Alex Nolasco Sep 12 '12 at 03:41
  • 4
    It was bitbucket who suggested me https url when creating a repository! How come! – Denys Kniazhev-Support Ukraine Aug 25 '13 at 16:03
  • 4
    Keep in mind if your bitbucket is private, if you want to use the git protocol, you need to add you ssh key to BitBuket's website in your settings. – cevaris Jan 23 '14 at 18:37
  • 9
    Had to change the URL to `ssh://git@bitbucket.org/userName/repoName.git` — without `ssh://` prefix it didn't work. – trygub Feb 22 '17 at 20:13
  • manojlds's answer plus NicolasRaoul's comment solved my problem. Thank you! – Coty Embry Aug 20 '17 at 22:10
  • Also, make sure you don't have two `.gitconfig` files. I had one in my home folder and one in my project. I updated the one in my project folder but forgot about the other one in home folder. Deleting the one in home folder and updating the project folder one did the trick. – Guillaume Renoult Feb 20 '18 at 04:30
  • 1
    I know this is a very old question. Does Bitbucket offer a setup somewhere to set ssh as default for clone? `git remote set-url origin ssh://git@bitbucket.org//.git` does solve the problem, but it's very tedious to change every time. – FisNaN Sep 06 '18 at 02:59
  • Still asks me for password each time on my server :( – trainoasis Aug 07 '19 at 10:58
  • Edit using `sudo gedit .git/config` in regular Terminal. – DanielBell99 Jun 08 '22 at 13:41
  • This answer was mega helpful, thank you so much. I had an issue where I had cloned one of my repo's using HTTPS, and since BitBucket has stopped allowing HTTPS connections, I had no idea how to switch that repo over to ssh. – Andrew Fox Sep 12 '22 at 02:18
  • It hit me during an update of a submodule. Then you need to also check the submodule entries in `.git/config` – mkrasowski May 16 '23 at 11:39
47

As explained here, if you clone with SSH url, you don't need to enter username / password each time you push / pull. Check above answer by @manojlds

But if you want to clone with HTTPS and want to avoid entering username / password each time, you can store credentials into cache with below command:

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

where 3600 (seconds) means 1 hour, you may change it as per your requirement.

Community
  • 1
  • 1
Ajeet Shah
  • 18,551
  • 8
  • 57
  • 87
  • @atilkan In that case, can you provide your machine / OS and git version info? And the error message if any when you try to set the cache setting. – Ajeet Shah Mar 20 '18 at 06:55
  • I cloned someone's repository form bitbucket, and here is my remotes. `manecs-MBP:LocationTracker manec$ git remote -v origin https://Vysh1@bitbucket.org/Vysh1/locationtracker.git (fetch) origin https://Vysh1@bitbucket.org/Vysh1/locationtracker.git (push)` – Banee Ishaque K Jun 21 '18 at 07:31
  • @AjeetShah what if I entered the wrong password? would it also cache the wrong password? – anaval Dec 04 '18 at 03:46
  • @anaval Why don't you try that and after that try entering correct password after clearing or disabling the cache? Edit the answer and let us know how you did that :) – Ajeet Shah Dec 05 '18 at 14:34
31

Its already answered above. I will summarise the steps to check above.

run git remote -v in project dir. If the output shows remote url starting with https://abc then you may need username password everytime.

So to change the remote url run git remote set-url origin {ssh remote url address starts with mostly git@bitbucket.org:}.

Now run git remote -v to verify the changed remote url.

Refer : https://help.github.com/articles/changing-a-remote-s-url/

shiva kumar
  • 11,294
  • 4
  • 23
  • 28
20

Hello Googlers from the future.

On MacOS >= High Sierra, the SSH key is no longer saved to the KeyChain because of reasons.

Using ssh-add -K no longer survives restarts as well.

Here are 3 possible solutions.

I've used the first method successfully. I've created a file called config in ~/.ssh:

Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa
Vaiden
  • 15,728
  • 7
  • 61
  • 91
  • 2
    This needs more attention! I spent ages looking for a solution and the first one with the `config` file in `/.ssh` works perfect! – Kitson Oct 24 '19 at 08:38
  • 1
    This answers solved my problem on MacOs : `cd ~/.ssh` + `ls` + `nano config` (the file was already created) + add code in Vaiden's answer. Then control X + Y to save file. – Sébastien Gicquel Mar 24 '20 at 18:48
15

If you are using a Ubuntu system, use the following to Store Password Permanently:

git config --global credential.helper store
Vinodh Ram
  • 709
  • 1
  • 9
  • 22
  • 1
    Thanks man, It solves my problem. – Chi Nov 26 '21 at 12:02
  • This command doesn't do anything on my wsl – Ram Venkat Jul 13 '23 at 06:51
  • This command is for Ubuntu OS. This command doesn't do anything when we execute it. We need to provide the git password when we clone or push any branch, after that it won't ask you again. This means after executing this command, it asks password for the first time only. – Vinodh Ram Jul 27 '23 at 12:13
14

None of these answers helped me, turned out my issue was slightly different. It was ssh that was asking for my password each time, before sending the key. So what I had to do was link my password with this command:

ssh-add -K ~/.ssh/id_rsa

It'll then prompt you for your password and store it. This could be the solution you're looking for if each time your prompted for a password it says

Enter passphrase for key '/Users//.ssh/id_rsa':

More info here

NOTE: I used this on my mac machine successfully, but as @Rob Kwasowski pointed out below, the upper case K option is unique to mac. If not on mac you will need to use lowercase k (which probably works for mac too but I haven't tested).

stackPusher
  • 6,076
  • 3
  • 35
  • 36
  • 1
    That's great, but it should be lowercase `k`: `ssh-add -k ~/.ssh/id_rsa` – Rob Kwasowski Apr 05 '19 at 06:27
  • could you elaborate please? – stackPusher Apr 05 '19 at 16:16
  • As explained here: https://www.ssh.com/ssh/add , the command line option is lowercase `k`, not uppercase as those options are case sensitive. – Rob Kwasowski Apr 05 '19 at 16:28
  • 1
    right but the uppercase K option will add the key to the keychain as well, while lowercase k will only add the key to the agent. so what i'm asking is 'why wouldnt you want the key to also be added to your keychain?' – stackPusher Apr 05 '19 at 16:51
  • 1
    Please be aware of platform differences, `-K` is specific to Mac: https://help.github.com/en/articles/error-ssh-add-illegal-option----k – Rob Kwasowski Apr 05 '19 at 16:56
  • Before entering in the above command (which worked) I first had to run `eval "$(ssh-agent)"`. If I didn't I received the following error: "Could not open a connection to your authentication agent" – WBuck Jan 08 '20 at 12:50
  • Thanks, this was it for me on Linux, just use lower case "-k" argument – James Mar 11 '22 at 14:04
13

In the HTTP request case, it is also and alternatively possible to paste the credentials (with password) directly into the url:

http://username:password@bitbucket.org/...

This will save the pain to give your credentials every times again. Simple modify your .git/config (the url).

Sven W.
  • 335
  • 4
  • 10
  • 10
    But not safe. And not work if the password has some letter like `$%` – Gank May 13 '16 at 15:45
  • 4
    Anyone who reads this comment, please remember: just because it _works_, doesn't mean it's _right_. If you're copying your password anywhere permanently, then you're doing it wrong. Same with those who always set folder permissions to `777`: stop, think, research, understand, and then do it properly. – dKen Mar 21 '18 at 13:28
5
webdev5
  • 467
  • 1
  • 8
  • 22
4

The following assumes command-line access via iTerm / Terminal to bitbucket.

For MacOS Sierra 10.12.5, my system manifested an equivalent problem - asking for my SSH passphrase on each connection to bitbucket.

The issue has to do with OpenSSH updates in macOS 10.12.2, which are described here in Technical Note TN2449.

You very well might want to tailor your solution, but the following will work when added to your ~/.ssh/config file:

Host *
    UseKeychain yes

For more information on ssh configs, take a look at the man pages for ssh_config:

% man ssh_config

One other thing: there is a good write-up on superuser here that discusses this problem and various solutions depending on your needs and setup.

pob
  • 373
  • 3
  • 6
4

I cloned the repository with HTTPS URL instead of SSH URL hence even after adding the SSH Key it was asking me for password on Bash Shell.

I just edited the ./.git/config file and changed the value of url variable by simply replacing the https:// to ssh://

E.g.

[core]
        ...
        ...
        ...
[remote "origin"]
        url = https://<username>@bitbucket.org/<username>/<repository_name>.git
        fetch = +refs/heads/*:refs/remotes/origin/*
        ...
        ...
        ...

Changed to:

[core]
        ...
        ...
        ...
[remote "origin"]
        url = ssh://<username>@bitbucket.org/<username>/<repository_name>.git
        fetch = +refs/heads/*:refs/remotes/origin/*
        ...
        ...
        ...
2

You may need to double-check your SSH identities file. You may be guiding BitBucket to look at a different/incorrect private key to the equivalent public key that you have saved on BitBucket.

Check it with tail ~/.ssh/config - you will see something similar to:

Host bitbucket.org
 HostName bitbucket.org
 IdentityFile ~/.ssh/personal-bitbucket-ssh-key

Remember, that adding additional identities (such as work and home) can be done with the ssh-add command, for example:

ssh-keygen -t rsa -C "companyName" -f "companyName"
ssh-add ~/.ssh/companyName

Once you have confirmed which private key is being looked at locally, you can then take your public equivalent, in this case:

cat ~/.ssh/personal-bitbucket-ssh-key.pub | pbcopy

And paste that cipher onto BitBucket. Your git pushes will now (provided you are using the SSH clone as aforementioned answers have pointed out) be allowed without a password, as your device is a recognised friendly.

Hopefully this helps clear it up for someone.

Grant
  • 5,709
  • 2
  • 38
  • 50
2

Actually, none of these answers reflect current state of the art with Git (v2.29 by time of writing this answer). In the latest versions of Git, cache, winstore, wincred are deprecated.


If you want to clone a Bitbucket repository via HTTPS, e.g.

git clone https://Kutlime@bitbucket.org/SomeOrganization/SomeRepo.git

You have to:

  1. Generate app password at Bitbucket user administration.
  2. Setup your credential method in .gitconfig accordingly (global or local)
[credential]
    helper = manager

You can locate your .gitconfig by executing this command.

git config --list --show-origin
  1. Execute
git clone https://Kutlime@bitbucket.org/SomeOrganization/SomeRepo.git

and wait until log on window appears. Use your user name from the url (kutlime in my case) and your generated app password as a password.

KUTlime
  • 5,889
  • 1
  • 18
  • 29
2

If you still get too many authentication failures errors:

nano ~/.ssh/config

And paste:

Host bitbucket_james
    HostName bitbucket.org
    User james
    Port 22
    IdentitiesOnly=yes
    IdentityFile=~/.ssh/id_rsa_bitbucket_james

And most important - you should bitbucket_james alias instead of bitbucket.org when you set up your remote URL:

git remote set-url origin git@bitbucket_james:repo_owner_user/repo_name.git
James Bond
  • 2,229
  • 1
  • 15
  • 26
2

The keyword here is 'ssh://' instead of https://' :-)

in your .git/config

Be aware! If you have cloned repositories, these can be re-cloned after you added your public key to BitBucket to make it work. This is probably more clean that hacking your config file ;-)

The Schwartz
  • 757
  • 1
  • 8
  • 13
0

With me, although I ran 'git clone ssh://git@stash.xxx.com:7999/projName/projA.git' I was still being prompted for password for this new repo that I cloned, so by comparing its .git/config file to other repos that work, It turned out to be the url under the [remote "origin"] section, it was set to the ssh path above for the new repo, but was set to https:xxx for the working one.

Ninos
  • 224
  • 4
  • 18
0

I was having other weirdness around logging in. I came across something that seemed totally dumb but worked in my case. Simply go to MacOS's keychain. Find the login lock icon in the sidebar. Click it to logout and then click to login. Sounds dumb but it solved my issues. Worth a shot.

SmileBot
  • 19,393
  • 7
  • 65
  • 62
0

I logged in using my username instead of email and it started working.

Hirasawa Yui
  • 1,138
  • 11
  • 29