git save username & password

How to Save Git Username & Password

By default, git asks for username and password every time you push commits to your repository. This can be annoying if you need to frequently enter user credentials for authentication. If you want to avoid being asked for git username & password every time, you need to save it in your local git repository. In this article, we will learn how to save git username & password.


How to Save Git Username & Password

You are asked for username & password only if you use HTTPS version of your git repository’s remote URL. Users get this problem when they use HTTPS versions of URLs for cloning repositories or pushing commits to repositories. So you can easily fix this by using the SSH version of your git repo’s remote URL.

Here is the command to set the remote URL of your repository, using set-url command.

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

The above command will update the remote repository’s URL in your local git system, to use the SSH version of URL, instead of HTTPS one. When you use SSH version of remote URL, the authentication happens via SSH keys that are already present on your system, and so you are not asked for username and password.

Alternatively, you can make git store the username & password values that you enter the first time.

git config --global credential.helper store

In this case, git will ask you for username & password, the first time you clone or push commits to remote URL. At that time, it will also store these values locally on your system. Thereafter, every time you clone repo or push commits from your system, these stored values will be used, and you won’t be asked for git username & password.

Or you can instruct git to cache the username and password so that you are never asked for it again.

git config --global credential.helper cache

This is similar to git storing your username and password locally, except that it is cached and therefore has a timeout value. In other words, the difference between storing and caching username & password is that storing is permanent while caching is temporary.

You can also set a timeout value for your cache, in case you decide to use the above command.

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

In this article, we have learnt how to save git username and password to avoid being asked every time.

Also read:

How to Get Classname of Instance in Python
How to Lock File in Python
How to Use Boolean Variables in Shell
How to Concatenate String Variables in Shell
How to Iterate Over Arguments in Shell Script

Leave a Reply

Your email address will not be published.