1846

I cloned a Git repository from my GitHub account to my PC.

I want to work with both my PC and laptop, but with one GitHub account.

When I try to push to or pull from GitHub using my PC, it requires a username and password, but not when I'm using the laptop!

I don't want to type my username and password every time I interact with origin. What am I missing here?

Braiam
  • 1
  • 11
  • 47
  • 78
TooCooL
  • 20,356
  • 6
  • 30
  • 49
  • 3
    You need to register the pubkey with your Github account (https://github.com/account/ssh) and configure your SSH client to use the right username. – jwodder Jul 03 '11 at 20:33
  • 2
    I have done all of that but still it requires username and password! is it possible to use one account with two PCs? – TooCooL Jul 03 '11 at 20:46
  • 1
    This question covers all your options for this quite well: http://stackoverflow.com/questions/5343068/is-there-a-way-to-skip-password-typing-when-using-https-github – ford Apr 05 '13 at 06:18
  • 3
    No need to switch over to ssh anymore. It's possible with HTTPS too. Check my answer. – Varun Achar Apr 05 '13 at 06:19
  • 1
    I prefer using an [encrypted netrc.gpg](http://stackoverflow.com/a/18362082/6309) in which I can store all my credentials for https remote repo. And that works well with the new [GitHub two-factor authentication](http://stackoverflow.com/a/18607931/6309)! – VonC Sep 11 '13 at 21:23
  • I dont know how often I ended up in this thread because I took the https url instead of the ssh... – Denny Mueller Jun 18 '15 at 13:53
  • Note for those using two factor authorization: you will need to use an access token instead of your password. See [here](https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/) – Josh Desmond Jun 23 '17 at 18:38
  • Does this answer your question? [Is there a way to cache https credentials for pushing commits?](https://stackoverflow.com/questions/5343068/is-there-a-way-to-cache-https-credentials-for-pushing-commits) – Braiam Jun 02 '21 at 14:26
  • You can also consider [this cross-site Q/A](https://unix.stackexchange.com/questions/26371/ssh-prompts-for-password-despite-ssh-authorized-keys#comment35605_26371) or [SSH Key - Still asking for password and passphrase](https://stackoverflow.com/q/21095054/4575793) (possible duplicate) – Cadoiz Oct 07 '22 at 08:55

30 Answers30

2482

A common cause is cloning using the default (HTTPS) instead of SSH. You can correct this by going to your repository, clicking "Clone or download", then clicking the "Use SSH" button above the URL field and updating the URL of your origin remote like this:

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

You can check if you have added the remote as HTTPS or SSH using:

git remote -v

This is documented at GitHub: Switching remote URLs from HTTPS to SSH.

2240
  • 1,547
  • 2
  • 12
  • 30
Tekkub
  • 30,739
  • 2
  • 30
  • 20
  • 4
    Then as I said, your ssh keys never come into play. If you want to use ssh, you need to use the ssh clone url instead – Tekkub Jul 05 '11 at 07:45
  • 63
    And to figure out how to change the URL, go here: http://stackoverflow.com/a/2432799/60488 (spoiler: `git remote set-url origin git://new.url.here`) – Johan Kool Nov 30 '11 at 03:32
  • 4
    You can check your remotes with the command: git remote -v and then if it's not correct, you can use: git remote rm aliasname (like "origin" for ex.) and re-add using: git remote add git@github.com:xxxxx again like mentioned here, the https style remote will not use your SSH keys. – Tom Jan 06 '12 at 17:34
  • 1
    another way to change this is to edit the `.git/config` in your repo. edit the url under `[remote "origin"]` to use the `git://` url instead of `https://`. Thanks @Tekkub – Shreyans Jul 15 '12 at 15:11
  • This worked for me, having teh same username/password prompting problem. Note that "username" is your github username and "repo.git" is the name of your repo (I did not include the ".git" on the end, and that still worked for me). The remainder was entered verbatim. – Jason Jul 24 '12 at 15:12
  • 2
    Issuing `git remote -v` with the same issue returned `origin https://github.com/myusername/my-repo-name.git` After fixing the problem it returns `origin git@github.com:myusername/my-repo-name` (both for fetch and push). Hopefully that will be useful in trying to understand the difference. – Jason Jul 24 '12 at 15:15
  • 168
    If you can't user ssh for security restrictions (like me) you can do:`git remote set-url origin https://name:password@github.com/repo.git` [(extracted from a comment here)](http://stackoverflow.com/questions/8588768/git-push-username-password-how-to-avoid) – Bruno Berisso Jan 31 '13 at 17:23
  • Works! Thanks. You can re-check using the command `git remote -v` – Abhishek Biswal Jun 23 '13 at 09:23
  • 162
    Why is cloning with HTTPS a common mistake? GitHub now recommends using HTTPS. – Dennis Jul 14 '13 at 02:02
  • 8
    @smftre by default that's the case, but you can use a [helper to cache your credentials](http://git-scm.com/docs/git-credential-cache). – Dennis Oct 18 '13 at 20:06
  • 1
    I've been using HTTPS for months now (with Git v1.8ish) without having to enter my username/password. Then I upgraded to Git v2.0.1 two days ago and I have to enter my username/password every single time I do `git push`. Please explain? – chharvey Jul 26 '14 at 05:08
  • 32
    Fixed my `Permission denied (publickey)` using this guide: https://help.github.com/articles/generating-ssh-keys/ . – voltrevo Oct 18 '14 at 06:54
  • 1
    Using SSH doesn't work when cloning submodules recursively AFAIK. Unless these submodules have an ssh url, but that is deprecated since it disallows users without a ssh setup to clone the repository recursively. – chtenb Dec 06 '14 at 13:44
  • 5
    @chharvey And now GitHub recommends HTTPS again ! See https://help.github.com/articles/set-up-git/#next-steps-authenticating-to-github-from-git – bela83 May 11 '15 at 15:04
  • 3
    git can also cache your username and password. It takes 30 seconds to set up. https://help.github.com/articles/caching-your-github-password-in-git/#platform-linux – user137717 Aug 13 '15 at 21:01
  • As per K M Rakibul Islam's answer below, If you are using Mac OSX, you can store you Github password in the keychain, so that do not need to enter you password each time, even over HTTPS. https://help.github.com/articles/caching-your-github-password-in-git/ – WillJones Jun 22 '16 at 13:47
  • Maybe it's because I'm using a Stash server, but I had to put in the protocol ("ssh://") portion of the url for it to work for me: `git remote set-url origin ssh://git@stash-server/my/app/repo.git` – CFL_Jeff Feb 03 '17 at 18:05
  • 1
    Thanks to @BrunoBerisso for the comment on prepending credentials to the http(s) url. If you still want some security and also a sanity check before pushing, you can omit your password from the url: `git remote set-url origin https://name@github.com/repo.git`. – Akin Williams May 31 '17 at 18:40
  • 1
    I get `fatal: No such remote 'origin'` – Soerendip Jan 28 '18 at 09:48
  • Just thought I'd point out you can set ssh to use port 443 in various ways including your `~/.ssh/config` file in which case AFAIK anyone that can use https can use ssh with github at least as they support ssh on port 443. [example ~/.ssh/config](https://pastebin.com/0AThjxxV) – gman Mar 14 '18 at 07:15
  • 1
    Note that to use the SSH method you will also need to set up an SSH key on your machine and associate it with your Github account. – Woodchuck May 06 '18 at 16:35
  • 1
    I fixed the permission denied problem by renaming my private SSH key into `id_rsa`. – mommi84 Nov 20 '18 at 16:54
  • 2
    I also think that cloning with https is a bad idea because you are adding an extra mechanism that must be secured to allow pulling/pushing without enter the pass every time. With SSH you have the public key infrastructure that takes care of it. I don't know how secure is the caching mechanism but why to use it at all if ssh cloning gives you superpowers? – Gonzalo Aguilar Delgado Mar 11 '19 at 16:58
  • 1
    ```git remote set-url origin https://name@github.com/name/repo.git``` – FantomX1 Sep 07 '20 at 18:11
  • git remote set-url origin https://name:password@github.com/repo.git , If @ is present in password, you need to encode that with %40 like, pass@word -> pass%40word. – jkr Dec 17 '20 at 05:20
  • `git remote set-url origin ssh://git@host:port/user/repo.git` worked for me. – sci9 Mar 14 '22 at 09:32
599

Permanently authenticating with Git repositories

Run the following command to enable credential caching:

$ git config credential.helper store
$ git push https://github.com/owner/repo.git

Username for 'https://github.com': <USERNAME>
Password for 'https://USERNAME@github.com': <PASSWORD>

You should also specify caching expire,

git config credential.helper 'cache --timeout 7200'

After enabling credential caching, it will be cached for 7200 seconds (2 hour). 43,200 s = 12 h (login once per day) might also be a reasonable choice for some.


As SebastianH commented, can also use similar commands to configure this not per repository but globally:

git config --global credential.helper store
git config --global credential.helper 'cache --timeout 7200'
jan
  • 5
  • 4
Jaykumar Patel
  • 26,836
  • 12
  • 74
  • 76
  • 1
    you may also have to change your remote url with `git remote set-url origin https://git@github.com//.git` This also works with 2FA – Bron Davies Oct 24 '18 at 21:17
  • 35
    This should be the accepted answer. It answer exactly the question. – Abraham Toledo Jun 24 '19 at 02:24
  • will it be deleted automatically from the ubuntu files to after 7200 for security purpose ? – Muhammad Usama Mashkoor Feb 27 '20 at 16:57
  • Just tried this - got: `fatal: protocol 'https' is not supported` – Joel Apr 01 '20 at 09:53
  • 1
    Does caching actually override anything from the store command? I'd think this answer is misleading as setting the cache options would be moot if you already have things stored permanently. No? – topher217 Sep 01 '20 at 10:35
  • unfortunately i don't think this persist through reboots or after the expiry period thus if using 2FA one has to type the personal access token OVER and OVER, something i'd prefer to not have to do. – ipatch Dec 23 '21 at 21:08
  • You can consider [this answer](https://stackoverflow.com/a/62184716/4575793) which explaines that "You can also use `store` to store in a file on your local disk, which is available on all platforms, but less secure." There are different other **credential stores** as for example inbuilt for Linux and OS X [`git config --global credential.helper libsecret`](https://stackoverflow.com/a/32470658/4575793), for Windows `wincred` or the [CredentialManager](https://github.com/GitCredentialManager/git-credential-manager#macos-homebrew) - which btw is available for both MacOS and Linux now. – Cadoiz Oct 10 '22 at 07:57
  • I used git push and then wincred as authenticator and it worked! – Smart Coder Mar 02 '23 at 20:15
161

I just came across the same problem, and the simplest solution I found was to use SSH URL instead of HTTPS one:

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

And not this:

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

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anton Salikhmetov
  • 1,643
  • 1
  • 10
  • 2
141

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):

machine github.com
       login <user>
       password <password>

Addition (see VonC's second comment): on Windows the file name is %HOME%\_netrc.

Also read VonC's first comment in case you want to encrypt.

Another addition (see user137717's comment) which you can use if you have Git 1.7.10 or newer.

Cache your GitHub password in Git using a credential helper:

If you're cloning GitHub repositories using HTTPS, you can use a credential helper to tell Git to remember your GitHub username and password every time it talks to GitHub.

This also works on Linux, Mac, and Windows.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rintcius
  • 3,283
  • 2
  • 19
  • 16
  • 3
    Yeah, it works on Linux, but doesn't work for gitbash on Windows. – Dielson Sales Apr 12 '13 at 16:56
  • 4
    @dolmen I see what you mean, but if you mind putting your password in a clear text, you can... encrypt it ;) See http://stackoverflow.com/a/18362082/6309. and that is even compatible with the 2-factor authentication (2FA) of Github: http://stackoverflow.com/a/18607931/6309 – VonC Nov 21 '13 at 08:29
  • 3
    @Sales it works perfectly from a DOS session or a git bash on Windows, provided you call your file `%HOME%\_netrc` (instead of `~/.netrc`). See also http://stackoverflow.com/a/18362082/6309 to encrypt that file. – VonC Nov 21 '13 at 08:30
  • 2
    This works very well in Linux, especially using git over VPN. – Evan Hu Dec 27 '14 at 08:39
  • 3
    you don't need to put it in clear text or encrypt it. the helper utility will cache it for you and takes 30 seconds to set up. https://help.github.com/articles/caching-your-github-password-in-git/#platform-linux – user137717 Aug 13 '15 at 21:02
  • 1
    Why would you do this instead of just adding an SSH key? – Banjocat Jan 16 '16 at 15:18
  • This helped me. We can only connect to outside only via https, all other outgoing ports are blocked. – Jerald Sabu M Mar 29 '18 at 12:59
  • `%HOME%` is only in Git Bash and is an absolute path. It does not exist in a CMD session. It seems to be a combination of %HOMEDRIVE% and %HOMEPATH% (but in Linux/Unix path format). – Peter Mortensen Jun 17 '19 at 08:44
  • @Banjocat some companies (such as mine sadly), do not allow external outbound connections on port 22. So to access sites such as github.com passwordless, ssh keys are not useful as https must be used instead. – ericcurtin Aug 22 '19 at 09:24
  • After hours surfing the net looking for answer this saved the day! Works very well for linux (Ubuntu) Thank you – João Vieira May 04 '23 at 23:10
103

For the uninitiated who are confused by the previous answers, you can do:

git remote -v

Which will respond with something like

origin    https://yourname@github.com/yourname/yourrepo.git (fetch)
origin    https://yourname@github.com/yourname/yourrepo.git (push)

Then you can run the command many other have suggested, but now you know yourname and yourrepo from above, so you can just cut and paste yourname/yourrepo.git from the above into:

git remote set-url origin git@github.com:yourname/yourrepo.git
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Davide
  • 17,098
  • 11
  • 52
  • 68
66

If you're using SSH and your private key is encrypted with a passphrase, then you'll still be prompted to enter the passphrase/password for the private key when you do network operations with Git like push, pull, and fetch.

Use ssh-agent to save the private key passphrase/password credentials

If you want to avoid having to enter your passphrase every time, you can use ssh-agent to store your private key passphrase credentials once per terminal session, as I explain in my answer to Could not open a connection to your authentication agent:

$ eval `ssh-agent -s`
$ ssh-add

In a Windows msysgit Bash, you need to evaluate the output of ssh-agent, but I'm not sure if you need to do the same in other development environments and operating systems.

ssh-add looks for a private key in your home .ssh folder called id_rsa, which is the default name, but you can pass a filepath to a key with a different name.

Killing the agent

When you're done with your terminal session, you can shutdown ssh-agent with the kill flag -k:

$ ssh-agent -k

As explained in the ssh-agent manual:

-k

Kill the current agent (given by the SSH_AGENT_PID environment variable).

Optional timeout

Also, it can take an optional timeout parameter like so:

$ ssh-add -t <timeout>

where <timeout> is of the format <n>h for <n> hours, <n>m for <n> minutes, and so on.

According to the ssh-agent manual:

-t life

Set a default value for the maximum lifetime of identities added to the agent. The lifetime may be specified in seconds or in a time format specified in sshd_config(5). A lifetime specified for an identity with ssh-add(1) overrides this value. Without this option the default maximum lifetime is forever.

See this page for more time formats.

Security warning for Cygwin users

Cygwin users should be aware of a potential security risk with using ssh-agent in Cygwin:

people should be cognizant of the potential dangers of ssh-agent under Cygwin 1, though under a local netstat and remote portscan it does not appear that the port specified in /tmp/ssh-foo is accessible to anyone ...?

[1]: http://www.cygwin.com/ml/cygwin/2001-01/msg00063.html

And at the cited link:

however, note that Cygwin's Unix domain sockets are FUNDAMENTALLY INSECURE and so I strongly DISCOURAGE usage of ssh-agent under Cygwin.

when you run ssh-agent under Cygwin it creates AF_UNIX socket in /tmp/ssh-$USERNAME/ directory. Under Cygwin AF_UNIX sockets are emulated via AF_INET sockets. You can easily see that if you'll look into /tmp/ssh-$USERNAME/agent-socket-* file via Notepad. You'll see something like

!<socket >2080

then run netstat -a and surprise! You have some program listening to port 2080. It's ssh-agent. When ssh receives an RSA challenge from the server, it refers to corresponding /tmp/ssh-$USERNAME/agent-socket-* (under Cygwin, in our case, that means it'll open connection to localhost:2080) and asks ssh-agent to process the RSA challenge with the private key it has, and then it simply passes the response received from the ssh-agent to the server.

Under Unix, such a scenario works without problems, because the Unix kernel checks permissions when the program tries to access an AF_UNIX socket. For AF_INET sockets, however, connections are anonymous (read "insecure"). Imagine, that you have the Cygwin ssh-agent running. A malicious hacker may portscan your box, locate open port used by ssh-agent, open a connection to your SSH server, receive the RSA challenge from it, send it to your ssh-agent via an open port he/she found, receive the RSA response, send it to the SSH server and voila, he/she successfully logged in to your server as you.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Sounds nice and detailed. I took care of [https credential helper](http://stackoverflow.com/a/18362082/6309), and you took care of ssh connections! +1 – VonC Aug 22 '13 at 13:24
  • fantastic. This has actually helped me because my situation was a bit different, as even though my remote was set to 'ssh' instead of 'https' it was still asking for a password to be entered every time when I issued `git push` , `git pull` etc, and I was not liking the suggestions of storing the creds though it is my personal machine (as a best practice). This suggestion of `ssh-add` really helped. Thank you :) – itsraghz Oct 22 '20 at 02:20
58

Source: Set Up Git

The following command will save your password in memory for some time (for Git 1.7.10 or newer).

$ git config --global credential.helper cache
# Set git to use the credential memory cache

$ git config --global credential.helper 'cache --timeout=3600'
# Set the cache to timeout after one hour (setting is in seconds)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sagar Rakshe
  • 2,682
  • 1
  • 20
  • 25
  • 2
    I prefer the 'netrc' credential help (http://stackoverflow.com/a/18362082/6309) for caching *multiple* credentials (without having to remember every passwords). But if you are on Windows and want to use the memory cache, you need winstore (http://stackoverflow.com/a/15310274/6309) – VonC Sep 01 '13 at 08:25
  • 9
    This is is the best answer thus far IMHO. – chtenb Dec 06 '14 at 13:48
42

When you use https for Git pull & push, just configure remote.origin.url for your project, to avoid input username (or/and password) every time you push.

How to configure remote.origin.url:

URL format:
    https://{username:password@}github.com/{owner}/{repo}

Parameters in URL:

* username 
Optional, the username to use when needed.
authentication, if specified, no need to enter username again when need authentication. Don't use email; use your username that has no "@", otherwise the URL can't be parsed correctly, * password optional, the password to use when need authentication. If specified, there isn't any need to enter the password again when needing authentication. Tip: this value is stored as plain text, so for security concerns, don't specify this parameter, * e.g git config remote.origin.url https://eric@github.com/eric/myproject

@Update - using ssh

I think using ssh protocol is a better solution than https, even though the setup step is a little more complex.

Rough steps:

  • Create ssh keys using command, e.g ssh-keygen on Linux, on windows msysgit provide similar commands.
  • Keep the private key on the local machine at a proper location, e.g., ~/.ssh. And add it to the ssh agent via ssh-add command.
  • Upload the public key to the Git server.
  • Change remote.origin.url of the Git repository to ssh style, e.g., git@gitlab.com:myaccount/myrepo.git
  • Then when pull or push, there isn't any need to enter the username or password ever.

Tips:

  • If your ssh key has a passphrase, then you need to input it on first use of the key after each restart of your machine, by default.

@Update - Switch between https and ssh protocol.

Simply changing remote.origin.url will be enough, or you can edit repo_home/.git/config directly to change the value (e.g using vi on Linux).

Usually I add a line for each protocol, and comment out one of them using #.

E.g.

[remote "origin"]
        url = git@gitlab.com:myaccount/myrepo.git
        # url = https://myaccount@gitlab.com/myaccount/myrepo.git
        fetch = +refs/heads/*:refs/remotes/origin/*
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eric
  • 22,183
  • 20
  • 145
  • 196
  • Are you sure the **http[s]**-based URL support username expansion? The manual `git-fetch(1)` mentions that only for `git`/`ssh`-based URLs. – day Jan 08 '14 at 18:59
  • @plmday yes, I am using it, my git version is 1.8.2.3 and 1.8.4, I am not sure if higher verion do change about this. – Eric Jan 09 '14 at 13:42
  • 1
    NB providing your password in the URL (even when using HTTPS) means that it is visible to everything between you and your repository. – William Aug 01 '14 at 00:53
  • 3
    No, providing the password in `https://username:password@github.com/` is safe. See http://stackoverflow.com/questions/4980912/username-and-password-in-https-url – slowhand Jun 10 '15 at 09:17
  • 1
    And this was what I was looking for (for full automation) – Joshua Apr 18 '16 at 21:01
  • @Joshua The answer is old, `ssh` is actually a better solution, it's full automation too, I use `ssh` now unless when it's not working due to network issue. – Eric Apr 25 '16 at 16:25
  • @flybird: Can't use ssh because the server doesn't support it. – Joshua Apr 25 '16 at 16:27
  • This https technique worked for me on Github. However, remember that username on Github is not your email address. Yes, on Github, you can login with either in the username field. However, for git commits, you must specifically use your username. – Volomike May 29 '20 at 19:30
  • Thanks, the last hack - editing `repo_home/.git/config` did the job for me. I'd already setup ssh keys before that. – Nikhil VJ Jun 18 '21 at 02:49
18

If you've got 2FA enabled on your Github account, your regular password won't work for this purpose, but you can generate a Personal Access Token and use that in its place instead.

Visit the Settings -> Developer Settings -> Personal Access Tokens page in GitHub (https://github.com/settings/tokens/new), and generate a new Token with all Repo permissions:

generate GitHub personal access token

The page will then display the new token value. Save this value and use it in place of your password when pushing to your repository on GitHub:

> git push origin develop
Username for 'https://github.com': <your username>
Password for 'https://<your username>@github.com': <your personal access token>
Adil B
  • 14,635
  • 11
  • 60
  • 78
  • 1
    For security purposes, I find this to be the best https option. Storing a key seems to be a much better idea than storing your master password in plain text somewhere. The cache options provide additional layers of protection, by only allowing someone to interact if the current user on your machine, but if someone were to have access to your computer in the time before the cache dropped, there is still the potential risk. With this, even if the worst case scenario above occurred, you could just go remove the key from github from another computer. – topher217 Sep 01 '20 at 10:54
  • 2
    I think you'd still need to add `git config credential.helper store` prior to pushing or otherwise interacting with the remote, otherwise this token would be no longer be present next time you went to interact with the remote. Right? – topher217 Sep 01 '20 at 10:55
  • 1
    @topher217: that's correct. You can use the Git credential helper or even something like Mac OSX's Keychain to store these tokens for repeated use. – Adil B Sep 01 '20 at 13:27
  • @topher you can consider [this answer and my respective comment *(scroll up from there)*](https://stackoverflow.com/questions/6565357/git-push-requires-username-and-password/28562712#comment130679949_28562712) about the security of different credential helpers. `store` is the most insecure one, as it just plainly saves (stores) the content. There are more secure ones available. – Cadoiz Oct 10 '22 at 08:08
17

You can cache your GitHub password in Git:

Just follow the instructions from GitHub's official documentation.

After following the instructions from the above link, you should be able to push/pull to/from your repository without typing your username/password every time.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110
  • 5
    this is the best answer here and should be the accepted one IMO – TheZuck Jan 18 '16 at 10:45
  • 1
    Agreed, an answer citing the official documentation should take precedence. I gladly followed their instructions and prefer to use standard conventions. – johnnieb Oct 15 '17 at 18:53
14

What worked for me was to edit .git/config and use

[remote "origin"]
        url = https://<login>:<password>@gitlab.com(...).git

It goes without saying that this is an insecure way of storing your password but there are environments/cases where this may not be a problem.

WoJ
  • 27,165
  • 48
  • 180
  • 345
14

Here's another option:

Instead of writing

git push origin HEAD

You could write:

git push https://user:pass@yourrepo.com/path HEAD

Obviously, with most shells this will result in the password getting cached in history, so keep that in mind.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
LachoTomov
  • 3,312
  • 30
  • 42
  • 1
    In Bash, adding one or more *leading* spaces will usually keep it out of history. But not in [Git Bash](https://superuser.com/questions/1053633/what-is-git-bash-for-windows-anyway), though. – Peter Mortensen Jun 17 '19 at 13:00
11

If the SSH key or .netrc file did not work for you, then another simple, but less secure solution, that could work for you is git-credential-store - Helper to store credentials on disk:

git config --global credential.helper store

By default, credentials will be saved in file ~/.git-credentials. It will be created and written to.

Please note using this helper will store your passwords unencrypted on disk, protected only by filesystem permissions. If this may not be an acceptable security tradeoff.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rumman0786
  • 1,150
  • 10
  • 20
11

For Mac OS

  1. Go to your Github Settings -> Developer Settings -> Personal Access Tokens page in GitHub (https://github.com/settings/tokens/new), and generate a new Token with all Repo permissions
  2. Search Keychain Access in your mac -> search for github.com -> click Show password then paste the token you just copied.
  3. Go to the CLI, it will again ask for your username and password, enter your Github username and paste the token as password, and you should be good to go for the rest of the times you are using the CLI.
Orif Milod
  • 465
  • 1
  • 7
  • 17
  • Thanks, this worked for mac users – roy Dec 08 '21 at 23:29
  • Thanks, that helps. One thing to add, if `github.com` is not found in Keychain Access, that's ok, just do step 3 and then it will show in the keychain access. – Deqing Feb 05 '22 at 04:52
7

I had the same issue.

So I changed the .git/config file from my project,

url = https://github.com/<your-user-here>/<your-repo-here>

to

url = git@github.com:<your-user-here>/<your-repo-here>

and added the SSH public key to the Git profile which is in setting.

For the SSH public key:

cat ~/.ssh/id_rsa.pub
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shrikant
  • 393
  • 6
  • 7
7

If you have cloned HTTPS instead of SSH and facing issue with username and password prompt on pull, push and fetch. You can solve this problem simply for UBUNTU

Step 1: move to root directory

cd ~/

create a file .git-credentials

Add this content to that file with you usename password and githosting URL

https://user:pass@example.com

Then execute the command

git config --global credential.helper store

Now you will be able to pull push and fetch all details from your repo without any hassle.

Akhilraj N S
  • 9,049
  • 5
  • 36
  • 42
6

Updating your Git configuration file directly (if you do not want to memorize fancy commands):

Open your .git/config file in your favorite text editor. It will be in the folder that you cloned or in the repository that you performed git init in. Go into that repository. .git is a hidden folder, and pressing Ctrl + H should show the hidden folder, (ls -a in terminal).

Below is a sample of the .git/config file. Copy and paste these lines and be sure to update those lines with your Git information.

[user]
        name = Tux
        email = tux@gmail.com
        username = happy_feet

[remote "origin"]
        url = https://github.com/happy_feet/my_code.git
        fetch = +refs/heads/*:refs/remotes/origin/*

Change the URL part with the following format for SSH:

url = git@github.com:happy_feet/my_code.git

(The above formats do not change with various Git remote servers like GitHub or Bitbucket. It's the same if you are using Git for version control):

Note: The SSH way of connecting to a remote Git repository will require you to add your public SSH key to your Git remote server (like GitHub or Bitbucket. Search the settings page for SSH keys).

To know how to generate your SSH keys, refer to: Creating SSH keys

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
123survesh
  • 561
  • 3
  • 13
  • The good thing about setting [user] is that if you have a master git credential even then the commits will be registered by the [user] in the config. – Ayush Mandowara Jul 03 '20 at 17:10
5

For Windows Git users, after running git config --global credential.helper store, if it still prompts for a password, you'd better check where the configuration file is written to, using this command

git config --list --show-origin

In my case, after manually editing configuration file 'C:\Program Files\Git\mingw64\etc\gitconfig', and adding the following text, it worked.

[credential]
    helper = store
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Liang
  • 867
  • 11
  • 13
5

This is what worked for me:

git remote set-url origin https://username@github.com/username/reponame.git

Example:

git remote set-url origin https://jsmith@github.com/jsmith/master.git
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
bennie1
  • 167
  • 2
  • 3
5

You basically have two options.

If you use the same user on both machines you need to copy the .pub key to your PC, so GitHub knows that you are the same user.

If you have created a new .pub file for your PC and want to treat the machines as different users, you need to register the new .pub file on the GitHub website.

If this still doesn't work it might be because ssh is not configured correctly and that ssh fail to find the location of your keys. Try

ssh -vv username@github.com

To get more information why SSH fails.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rtn
  • 127,556
  • 20
  • 111
  • 121
4

Update for HTTPS:

GitHub has launched a new program for Windows that stores your credentials when you're using HTTPS:

To use:

  • Download the program from here

  • Once you run the program, it will edit your .gitconfig file. Recheck if it edited the correct .gitconfig in case you have several of them. If it didn't edit the correct one, add the following to your .gitconfig

    [credential]
        helper = !'C:\\Path\\To\\Your\\Downloaded\\File\\git-credential-winstore.exe'
    

    NOTE the line break after [credential]. It is required.

  • Open up your command line client and try git push origin master once. If it asks you for a password, enter it and you're through. Password saved!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Varun Achar
  • 14,781
  • 7
  • 57
  • 74
4

You need to perform two steps -

  1. git remote remove origin
  2. git remote add origin git@github.com:NuggetAI/nugget.git

Notice the Git URL is a SSH URL and not an HTTPS URL... Which you can select from here:

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sapy
  • 8,952
  • 7
  • 49
  • 60
4

List your current SSH keys:

ls -l ~/.ssh

Generate a new SSH key:

ssh-keygen -t ed25519 -C "your_email@example.com"

where you should replace your_email@example.com with your GitHub email address.
When prompted to Enter a file in which to save the key, press Enter.
Upon Enter passphrase (empty for no passphrase) - just press Enter (for an empty passphrase).
List the your SSH keys again:

ls -l ~/.ssh

The files id_ed25519 and id_ed25519.pub should now have been added.
Start the ssh-agent in the background:

eval $(ssh-agent -s)

Add your SSH private key to the ssh-agent:

ssh-add ~/.ssh/id_ed25519

Next output the public key to the terminal screen:

cat ~/.ssh/id_ed25519.pub

Copy the output to the clipboard (Ctrl + Insert).
Go to https://github.com/<your-github-username> and sign in with your username and password.
Click your GitHub avatar in the upper-right corner, and then Settings. In the left pane click SSH and GPG keys. Click the green-colored button New SSH key and paste the public SSH key into the textarea labeled Key. Use a descriptive Title that tells from what computer you will use this SSH key. Click Add SSH key.


If your current local repository was created with http and username, it needs to be recreated it so as to become SSH compatible.
First check to make sure that you have a clean working tree so that you don't lose any work:

git status

Then cd .. to the parent directory and rm -fr <name-of-your-repo>.
Finally clone a fresh copy that uses SSH instead of username/password:

git clone git@github.com:[your-github-username]/[repository-name].git

References:
https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account

Henke
  • 4,445
  • 3
  • 31
  • 44
2

As of 2021, there is a secure user-friendly cross-platform solution for HTTPS remotes. No more typing passwords! No more SSH keys! No more personal access tokens!

Install Git Credential Manager developed by GitHub (downloads). It supports passwordless in-browser OAuth authentication to GitHub, BitBucket, Azure and GitLab. This means you can enable two-factor authentication on GitHub and the other platforms, greatly improving the security of your accounts.

When you push, you are offered a choice of authentication methods:

> git push
Select an authentication method for 'https://github.com/':
  1. Web browser (default)
  2. Device code
  3. Personal access token
option (enter for default): 1
info: please complete authentication in your browser...

On Linux, a tiny bit of setup is required. The following caches credentials in memory for 20 hours, so you have to authenticate at most once per day.

git-credential-manager-core configure
git config --global credential.credentialStore cache
git config --global credential.cacheoptions "--timeout 72000"

Power users familiar with gnome-keyring or KWallet may prefer to change the credential store to libsecret.

Cosmetic configuration (docs):

  1. Prefer choosing authentication method at terminal rather than in GUI (fewer clicks)
  2. Always use browser method rather than be asked every time (even fewer keypresses)
git config --global credential.guiPrompt false
git config --global credential.gitHubAuthModes browser
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
1

Blockquote

if you experience $ git push code issues requesting for username && password follow the procedure below:

Login to your profile.
Go to settings

click developer settings
->Personal access tokens
--> note: access token
--->Select scopes: checkbox on repo
----> Generate token
-----> Make sure to copy and store it safely if you intend to reuse it.

*** use the generated token as password during pushing code for next 30days ***

***HAPPY CODING***
It's VIN
  • 152
  • 7
0

If you are using Git (for example, Git Bash) under Windows (and if you don't want to switch from HTTPS to SSH), you could also use Git Credential Manager for Windows

This application will keep the username and password for you...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
boly38
  • 1,806
  • 24
  • 29
0

As many users has said, you just have to change your Git repository URL from HTTPS to SSH.

If you haven't generated a SSH key in your machine, then your are going to have to do it.

Just as an additional information, after doing this change I still was getting the same error:

Permission Denied.

In my case, the problem was that I was using the Windows Shell to execute the ngh command; since this command should open a prompt to request the SSH phrase and the Windows Shell doesn't open these kinds of prompts, the authentication just failed.

So, I just had to open the Git shell and execute the ngh command there, put the SSH phrase in the prompt every time it asked for it and "voilà"... It just worked fine!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Thisisalexis
  • 104
  • 5
0
 # gen  the pub and priv keys
 # use "strange" naming convention, because those WILL BE more than 10 ...
 ssh-keygen -t rsa -b 4096 -C "me@corp.com" -f ~/.ssh/id_rsa.me@corp.com@`hostname -s`

 # set the git alias ONLY this shell session
 alias git='GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa.me@corp.com.`hostname -s`" git'

 # who did what when and why
 git log --pretty --format='%h %ai %<(15)%ae ::: %s'

 # set the git msg
 export git_msg='issue-123 my important commit msg'

 # add all files ( danger !!! ) and commit them with the msg
 git add --all ; git commit -m "$git_msg" --author "Me <me@corp.com"

 # finally 
 git push
Yordan Georgiev
  • 5,114
  • 1
  • 56
  • 53
-1

Your git and/or SSH version is too old

I was affected by both ssh and git always prompting for passwords on my 2010 Macbook Pro running Snow Leopard.

Originally, I was unable to ssh into my server.

I had enabled HostKeyAlgorithms +ssh-rsa,ssh-dss on my server (/etc/ssh/sshd_config) which I host my git repos and ssh into my accounts. With that change, I was able to use a username and password to connect to git and through ssh.

For reference the latest versions of ssh and git on OS X 10.6.8 are:

git: 1.7.5.4

ssh: OpenSSH_5.2p1

I tried adding a DSA key, but that didn't work. I also looked into updating the RSA1 key, but it looked like my SSH version was simply too old. I looked into Homebrew for snow leopard, but got certificate issues.

My solution is to install MacPorts for Snow Leopard https://github.com/macports/macports-base/releases/download/v2.8.1/MacPorts-2.8.1-10.6-SnowLeopard.pkg

I had the xcode 4.2 update installed which may be hard to find; not sure if that is necessary. Once MacPorts is installed, run

sudo port install git
sudo port install ssh

You may have to enable rsync, but the output will tell you how.

My versions are now 2.41.0 for git and OpenSSH_9.3p1 for ssh. I recreated a 4096 bit rsa key and copied it to my server (and to github) and can now both ssh and git push without any password prompting at all!

Hope this helps out others in my position.

Edit: Gentoo mentioned that since OpenSSH 8.8, support for RSA 1 is disabled (my server being OpenSSH_9.3p1). It can be re-enabled by adding this:

HostkeyAlgorithms +ssh-rsa
PubkeyAcceptedAlgorithms +ssh-rsa

If you are using PuTTY < 0.75, you will be affected by this.

NuclearPeon
  • 5,743
  • 4
  • 44
  • 52
  • Also if you are going to down vote me, at least give a reason as to why. I got the symptoms that the original asker mentioned and my fix enables me to overcome the problem. Just because I answered using my personal server doesn't mean it doesn't work on github either. In fact, I tested it by checking out one of my repos on github (I was previously unable to do so) and I can check out without using a password. So it works. – NuclearPeon Jun 19 '23 at 22:27
-2

Check your git version and update. Then will fixed the issue

$ git update-git-for-windows

Update note : we can use this when your PC git version and server git version mismatched.

Thilina Dharmasena
  • 2,243
  • 2
  • 19
  • 27