How to Set Username and Password in Git

Fumbani Banda Feb 02, 2024
  1. Set Username and Password in Git (GitHub/GitLab)
  2. Set Username and Password demo
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.

git-step1

We Create a new python file in our repository that prints Hi there.

git-step2

We add the new file to git and commit all the changes with a message Add print.py

git-step3

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.

git-step4

We create another python file that adds two integers and prints the sum.

git-step5

We also add the new python file to git and commit all the changes with a message Add add.py

git-step7

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.

git-step8

Fumbani Banda avatar Fumbani Banda avatar

Fumbani is a tech enthusiast. He enjoys writing on Linux and Python as well as contributing to open-source projects.

LinkedIn GitHub

Related Article - Git Credentials