How to Set Username and Password in Git
This article demonstrates the steps of setting a GitHub username and password in Linux to avoid authentication for every GitHub action.
Set Username and Password in Git (GitHub/GitLab)
To set the GitHub username and password in Linux, run this git command below.
git config --global credential.helper store
This command keeps the username and password in plain text on your local machine.
When you pull or push to a GitHub repository, you will be asked for a username and password. Once you input the two for the first time, they will be remembered later. If you have Two Factor Authentication for GitHub enabled, use an access token instead of your password.
Set Username and Password demo
This demonstration will use an existing repository. We will set the password and the username for our GitHub account to avoid authentication every time we push to GitHub or pull from GitHub.
We will create a new file and push it to the GitHub repository while setting the username and password first. We will also create another file to be pushed to the GitHub repository without authentication.
Run the following command, git config --global credential.helper store
, as shown below.
We Create a new python file in our repository that prints Hi there
.
We add the new file to git and commit all the changes with a message Add print.py
We push the new changes to the GitHub repository, and we need to authenticate for the first time to save the username and password on our local machine. Use a GitHub access token instead of a password if you have Two Factor Authentication enabled.
We create another python file that adds two integers and prints the sum.
We also add the new python file to git and commit all the changes with a message Add add.py
We push the changes to the remote GitHub repository, and we are not asked to authenticate this time because we have successfully saved the password and username on our local machine.