Git

How to Save Username and Password in Git

Git is a freely available decentralized version control system used by multiple people around the globe. However, sometimes it becomes time-consuming and difficult to prove your identity whenever you want to interact with a Git remote repository for performing different operations. To make these operations more feasible, permanently save these credentials and connect the Git local repository with the remote repository.

This study will briefly discuss the procedure of saving the username and password in Git.

How to Save Username and Password in Git?

As a Git user, you always have to provide your credentials for connecting a remote repository with Git’s local repository. To avoid this hassle, Git enables you to store a username and password locally and globally to be accessible to all current project users.

For the corresponding purpose, follow the below steps.

Step 1: Open GitHub Account

Firstly, open a browser, go to your “GitHub” repository where you want to save your username and password, and copy the repository “URL”. For instance, we will copy the link of our “test” remote repository from the address bar:


Step 2: Launch Git Bash

Search for the “Git Bash” with the help of the “Startup” menu and launch it:


Step 3: Clone Repository

Now, we will clone the selected remote Git repository and set a username and password to make things easier. To do so, we will execute the “git clone” command with copied remote repository URL as follows:

$ git clone https://Linuxhint:12345@github.com/itslinuxhint/test

 
Here, we have specified “Linuxhint” as our username and “12345” as the password:


The below output indicates that the “test” remote repository is successfully cloned:


Step 4: Save Credentials

Next, run the following “git config” command to save the specified Git credentials in the “.git/config” file:

$ git config credential.helper store

 
Above command will store our provided credentials in our local repository:


Next, we will add the “–global” option with the “git config” command to save the credential globally:

$ git config --global credential.helper store

 

Step 5: Pull Request

Enter your username and password, which you have recently stored in the “~/.git-credential” file as plain text, in the case when you pull or push from the remote repository for the first time:

$ git pull

 
The output of the above-given command displayed “Already up to date” message because we have already saved the username and password in Git after cloning the repository:


If you have cloned the repository without setting the credentials, follow the below section to save your username and password.

How to Save Password and Username for Already Cloned repository?

If you have cloned a Git remote repository without configuring a username and password, Git bash enables you to update the “URL” and specify credentials with the help of the following command:

$ git remote set-url origin https://LinuxWorld:09876@github.com/itslinuxhint/test

 
Here, we have specified “LinuxWorld” as our username, “09876” as its password, and specified the link of the cloned repository after the “@” sign.

After executing the above-given command, perform the same steps for saving the credentials we already provided in the first section.

Conclusion

To save username and password in Git, open your “GitHub” remote repository and copy its “URL”. Then, launch “Git Bash”, paste the “URL” with the “$ git clone” command, specify the credential and execute it. Lastly, run the “$ git config –global credential.helper store” command to save the credential in the “.git/config” file. This study demonstrated the procedure for saving a username and password in Git.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.