6

The requirement is probably odd. But sometimes I really need this functionality. Ok, here is the thing, I'm always working behind proxies. Either at office I'm using corp proxy or at home I'm using VPN and proxy as well (You know, I'm in China, so I hope you can understand).

Usually I'm using git protocol to connect github remote repository, but it's hard for me to set proxy behind git protocol, so I decided to switch back to http(s) with following command

git remote set-url origin https://github.com/<username>/repo.git

git config http.proxy http://proxy:8080

And it works like a charm. But git asks for username and password whenever I connect to the remote server, for example pull/push.

I only have one github account and I want to save some typing, so the question is how to avoid typing github username everytime I want to push. I could accept to type my password, but I don't want to type username. How to achieve that?

EthanZ
  • 150
  • 1
  • 3
  • 13

3 Answers3

16

Store Password

You could tell git to store your credentials using the following command:

git config --global credential.helper store

Using this method, you only need to enter your username and password once and git will never ask for it again. But please note that your password will be stored in plaintext which is not great in terms of security.

Cache Password

You can also go for caching instead which will store your password after having typed it once in a session for some period of time.

git config --global credential.helper cache

This is more secure as your password won't be stored on disk - just temporarily in memory. You can set the timeout yourself if your not happy with the default:

git config --global credential.helper 'cache --timeout=600'

Once again this is not always ideal.

SSH Agent - The Ideal Solution

What you should really be using is the ssh protocol to push and pull your data. This will allow you to use your private ssh key to authenticate yourself - which will be handled by your operating system's installed key agent. This should work with proxies without any issues so you should definitely give it a go.

You can set it up by setting your remote url as follows:

git remote set-url origin git@github.com:<username>/<project>.git

If you are using another hosting service like bitbucket, just replace "github.com" with your providers domain.

Once you do that, you will need to set up a public and private key pair for communication between github and your computer. There is a very good tutorial on how to set it up here. If you are using Linux or MacOSX you simply need to follow the steps when running the command ssh-keygen.

After that, you can get an SSH agent to store your password for you which is typically more secure. SSH agents usually ask you to input your password just once after turning on your computer - but after that it should do everything automatically for you. Passwords are stored securely this way unlike the first solution which stores passwords in plain text.

The SSH agent you use will depend on your operating system but it shouldn't be hard to set up. With most popular Linux distros you shouldn't need to do anything and I have been informed in a comment below that you can set Windows up as follows:

git config --global credential.helper wincred

I have never used MacOSX so I cannot say for certain whether this is automatically set up like it is in Ubuntu based distributions.

Michael Aquilina
  • 5,352
  • 4
  • 33
  • 38
  • 2
    Nope, those credentials won't be stored in plain text, if you don't want them to be: see [my own answer](http://stackoverflow.com/a/18673020/6309), and the [how-to-do I wrote](http://stackoverflow.com/a/18607931/6309) – VonC Sep 07 '13 at 12:05
  • Why `git config credentials.helper store` doesn't work for me. It prompt for password every time `git remote show origin` – EthanZ Sep 08 '13 at 14:13
  • 1
    Sorry that is my mistake. It should be `git config credential.helper store`. Tell me how it goes. – Michael Aquilina Sep 08 '13 at 14:26
  • Btw, you will need git 1.7.10 or later to use `credential.helper`. The latest git is 1.8.3 though so you should be fine if you recently downloaded it. You can check your git version with `git --version` – Michael Aquilina Sep 08 '13 at 15:22
  • No problem EthanZ glad to be of help. May I suggest you mark my post as the answer for anyone else who has the same problem? – Michael Aquilina Sep 10 '13 at 16:38
  • 1
    If you are using Git for Windows, use `git config --global credential.helper wincred`. – Steve Pitchers Apr 14 '15 at 10:04
  • Using ssh through a given proxy only works if this proxy authorize ssh and not only http. Might sound stupid to say but i tried your solution for one hour before figure out that the proxy that i use is a http only proxy. – Welgriv Feb 21 '20 at 13:39
5

To complete Nevik's comment, you can change the url of an existing repo:

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

That will specify the username the proxy authentication needs, leaving for you to enter the password.

I personally store all my different credentials (GitHub, BitBucket, ...) in an encrypted %HOME%\_netrc.gpg (Windows) or ~/.netrc.gpg (Unix) using the _netrc credential helper (git1.8.3+), rather than the memory cache credential helper described in Michael's answer.

See more at "Configure Git clients, like GitHub for Windows, to not ask for authentication".

The difference is:

  • with the memory cache credential helper, you have to remember your GitHub password
  • with a gpg netrc credential helper, you only have to remember your gpg passphrase, for all your different credentials.

Plus, if you activated GitHub two-factor authentication, you can no longer use your regular GitGub password, but you need a "Personal Access Token", which is a 40-lenght random string. One more reason to store that once and for all in an encrypted file, rather than having to enter it once per session.

What I just described is for https url, which, in my opinion, remains easier to manage than ssh url, with their public/private ssh keys that you need to passphrase protect, and that you need to publish on your GitHub account (one different per workstation, since you shouldn't be reusing ssh keys)

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • This solution does not work since git return a `fatal: unable to access 'https://xxx/': Proxy CONNECT aborted`. Tested with git 2.23.0 on windows – Welgriv Feb 21 '20 at 14:00
  • @Welgriv Do you have a proxy? Also, can you test with 2.25.1, since there was a curl issue before. – VonC Feb 21 '20 at 14:37
4

This is the simple solution for saving some typing you can use the following steps in git bash easily..

(1) create the remote repository

git remote add origin https://{your_username}:{your_password}@github.com/{your_username}/repo.git

Note: If your password contains '@' sign use '%40' instead of that

(2) Then do anything you want with the remote repository

ex:- git push origin master
Robert
  • 5,278
  • 43
  • 65
  • 115
  • i am using a chromebook and working in a cloud9 ide / not sure about all the caching, etc., so found this answer to work perfectly. was able to simply use my 'personal access token' in place of password when setting up the remote url, and it worked great. – dave campbell Apr 15 '16 at 17:09
  • Beware, it is not only for '@' but for lot's of other special characters as well. Now we know that your password have one or more '@' and no other special character so you might think about changing it... – Welgriv Feb 21 '20 at 13:41