Reputation: 14000
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?
Upvotes: 853
Views: 602205
Reputation: 455
In case you want to use OAuth and git is asking you for credentials instead (which is deprecated) even after updating git, you have to set the credential manager:
git config --system credential.helper manager
Upvotes: 0
Reputation: 1
I'm sorry but all answers are a long way around to a simple solution. You want to update your netrc file
nano ~/.netrc machine github.com login yourusername password ghp_yourtoken
Upvotes: 0
Reputation: 3526
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.
SSH_ENV="$HOME/.ssh/environment"
# 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
Upvotes: 31
Reputation: 718
Here is the solution to this issue:
git remote -v
This will display the URL for origin. If it starts with https
, we need to remove this URL and replace with URL for ssh
git remote remove origin
git remote add origin [email protected]:PrestaShop/PrestaShop.git
After you have removed and added the remote repository again, if you're still unable to push your local branch to GitHub, then the command listed below should help to resolve this issue.
xpetta@host:~/home/xpetta$ git push
fatal: The current branch ABC-123 has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin ABC-123
Alternatively you can open the .git/config
and find the [remote "origin"]
section and make sure the URL which is being used doesn't begins with https
if it begins with https
replace it with the ssh
URL from the remote GitHub repo. Same applies to other services listed within .git/config
such as fetch and likewise, need to be replace with the ssh
URL.
Upvotes: 4
Reputation: 431
Git will prompt you for a password if you are using the https
protocol. If you use ssh
, it will do the authentication using your private key instead of asking for password.
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 here)
Now, you will have to just remove this first and add the url with ssh
git remote remove origin
git remote add origin [email protected]:PrestaShop/PrestaShop.git
Upvotes: 9
Reputation: 841
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.
Update: The -K
flag has since been deprecated. Use the following instead:
ssh-add --apple-use-keychain ~/.ssh/id_ed25519 # path to private key
Upvotes: 5
Reputation: 8086
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.
Upvotes: 9
Reputation: 453
In windows machine: Install GitHub CLI
choco install gh
Then run the command:
gh auth login
and follow the Instruction
Also you can follow this link:
https://cli.github.com/manual/gh_auth_login
Upvotes: 1
Reputation: 1
Faced same kind of issue.
Please make sure you have properly set the remote origin:
$git remote add origin master "https://...git "
Upvotes: 0
Reputation: 703
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.
Upvotes: 12
Reputation: 19996
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.
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.
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.
Upvotes: 680
Reputation: 3914
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 ...
Upvotes: 0
Reputation: 1
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://[email protected]/myaccount/myproject.git
So adding the "myaccount@" and ".git" resolved the issue in my case.
Upvotes: 0
Reputation:
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://[email protected]/"]
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).
Upvotes: 1
Reputation: 588
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:[email protected]/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!
Upvotes: 4
Reputation: 17085
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://[email protected]/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.
Upvotes: 0
Reputation: 1026
If you want to stop Git from always asking you for the login credentials of your GitHub repository this can be easily done.
You can update the origin remote using SSH instead of HTTPS"
git remote set-url origin [email protected]:username/your-repo.git
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
Upvotes: 8
Reputation: 56262
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://[email protected]/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://[email protected]/mystore/myproject/
fetch = +refs/heads/*:refs/remotes/origin/*
Upvotes: 955
Reputation: 1922
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
Upvotes: 6
Reputation: 322
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
Upvotes: 7
Reputation: 7065
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.
Upvotes: 1
Reputation: 802
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.
Upvotes: 11
Reputation: 2678
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.
Upvotes: 164
Reputation: 2333
Before you can use your key with GitHub, follow this step in the tutorial, Testing your SSH connection:
$ ssh -T [email protected]
# Attempts to ssh to GitHub
Upvotes: 1
Reputation: 166399
There are different kind of authentications depending on your configuration. Here are a few:
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
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.
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.
Upvotes: 1
Reputation: 87
As static_rtti said above, change
https://github.com/username/repo.git
git://github.com/username/repo.git
to
ssh://[email protected]/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 [email protected]
. 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.
Upvotes: 3
Reputation: 12965
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:[email protected]/path_to_your_git.git
Upvotes: 0
Reputation: 9376
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)
Upvotes: 80
Reputation: 10350
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.
Upvotes: 3
Reputation: 1575
Guide to Git on Windows and GitHub using SSH to push/pull: An Illustrated Guide to Git on Windows
Upvotes: 11