How to save username and password in Git

In this article I’ll show how you can save your Git username and password and avoid entering your Git username and password for every push.

There are three methods how to save Git credentials: Git SSH authorization, Git credentials caching, and Git credentials saving using credential.helper.

Recommended and Secure Method: Setup Git SSH Keys authentication

Create an ssh Github key. Go to github.com -> Settings -> SSH and GPG keys -> New SSH Key. Now save your private key to your computer.

Then, if the private key is saved as id_rsa in the ~/.ssh/ directory, we add it for authentication as such:

Even More Secure Method: Git Credentials Caching

We can use git-credential-store to cache our username and password for a time period. Simply enter the following in your CLI (terminal or command prompt):

You can also set the timeout period (in seconds) as such:

Less Secure Method: Saving Git Password In A git-credentials File

To save username and password in Git you can use credential.helper.

Run

then

provide a username and password and those details will then be remembered later. The credentials are stored in a file on the disk, with the disk permissions of “just user readable/writable” but still in plaintext.

Attention: This method saves the credentials in plaintext on your PC’s disk. Everyone on your computer can access it, e.g. malicious NPM modules.

If you want to change the password later

Will fail, because the password is incorrect, git then removes the offending user+password from the ~/.git-credentials file, so now re-run

to provide a new password so it works as earlier.

Want me to do this for you? Drop me a line: itgalaxyzzz {at} gmail [dot] com