How to Save Username and Password in Git

John Wachira Feb 02, 2024
How to Save Username and Password in Git

This article will discuss how you can save your credentials in Git. We will configure Git to recall our credentials so that we can access our remote repository automatically.

Save Username and Password in Git

You may have noted that every time you want to run commands like push and pull using Git GUIs or through HTTP(S), authentication will require you to enter your username and password. Fortunately, you can save your credentials on Git and instantly access your repository, which we will cover shortly.

We will start by setting a username and password while cloning a repository to make things easier.

You will have to set a username and password in the remote repository’s URL in the command line. Check the example below.

$ git clone https://<USERNAME>:<PASSWORD>@github.com/path/to/repo.git

To run the command above, replace <USERNAME> and <PASSWORD> with your credentials.

Git will store your credentials in a .git/config file.

You can update the URL with the command below for those who cloned their repository without configuring credentials.

$ git remote set-url origin https://<USERNAME>:<PASSWORD>@github.com/path/to/repo.git

To save your credentials on Git, run the command below.

$ git config credential.helper store

The command above will save your credentials in your local repository. You can add the --global argument to save them globally.

$ git config --global credential.helper store

Once prompted, run a git pull command and enter your username and password. Git will save your credentials, and you can access your remote repo automatically from this point.

This method has a small catch. Git will save your password as plain text in a .git-credentials file.

This is not safe, especially if your system is not encrypted. To remedy this, you can use the command below.

git config --global credential.helper manager

The Windows Credential Manager will keep your credential in your system’s safe storage.

Run a git pull command and enter your credentials when prompted. Git will not save your password as plain text.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Config