Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Git only ask password when push to remote repository

Tags:

git

github

proxy

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?

like image 552
EthanZ Avatar asked Sep 07 '13 11:09

EthanZ


People also ask

How do I avoid the specification of the username and password at every Git push?

You can avoid being prompted for your password by configuring Git to cache your credentials for you. Once you've configured credential caching, Git automatically uses your cached personal access token when you pull or push a repository using HTTPS.

How do I enable Git authentication password?

To connect to a Git repository with authentication over HTTP(S), every time it needs to set a username and password. You can configure Git to remember a username and password by storing them in a remote URL or by using Git credential helper.


1 Answers

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 [email protected]:<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.

like image 74
Michael Aquilina Avatar answered Sep 25 '22 16:09

Michael Aquilina