Open In App

How To Login Using The Git Terminal?

Last Updated : 28 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Git is a widely used version control system that allows developers to collaborate on code. When working with remote repositories hosted on platforms like GitHub, GitLab, or Bitbucket, it's essential to authenticate yourself to perform actions such as cloning, pulling, or pushing code.

This article will guide you through the different methods to log in via the Git terminal, ensuring secure and seamless interaction with your remote repositories.

Various Approaches to Login using the GIt Terminal

Approach 1: Using HTTPS and Personal Access Tokens (PAT)

Using HTTPS is a simple method, but it requires generating and using Personal Access Tokens (PAT) for secure authentication, as many platforms have deprecated the use of passwords.

Step 1: Clone the Repository

git clone https://github.com/username/repository.git

If the repository is private, Git will prompt for your username and password.

Example: This is my private git repository now i want to clone this repository. When i try to clone then git will ask me to enter username and password.

git clone https://github.com/rohit-choudhary14/WhatsAppClone.git
Screenshot-2024-08-17-003406
How to login using the Git terminal

Step 2: Generate a Personal Access Token (PAT)

  • Log in to your GitHub account.
  • Go to Settings > Developer settings > Personal access tokens.
  • Click on Generate new token.
  • Select the scopes or permissions you need.
  • Copy the generated token.
Screenshot-2024-08-17-003820
How to login using the Git terminal

Step 3: Authenticate Using the PAT

  • When Git prompts for your password, paste your PAT instead.
  • Git will remember the PAT for the duration of your session.
token_output
After token is provided you will logged in and repository will start to clone

Step 4: Cache the Credentials

To avoid re-entering your PAT, you can cache your credentials:

git config --global credential.helper cache

Approach 2: Using SSH Keys

SSH keys are a more secure and convenient way to log in to Git. Once set up, SSH keys eliminate the need to enter a username and password for each operation.

Step 1: Generate SSH Key Pair

ssh-keygen -t rsa -b 4096 -C "[email protected]"

When prompted, press Enter to save the key to the default location, or specify a custom path.

Screenshot-2024-08-17-004900
How to login using the Git terminal

Step 2: Add SSH Key to the SSH-Agent

Start the SSH-Agent in the background:

eval "$(ssh-agent -s)"
Screenshot-2024-08-17-005048
How to login using the Git terminal

Add your SSH private key to the SSH-Agent:

ssh-add ~/.ssh/id_rsa
Screenshot-2024-08-17-005156
How to login using the Git terminal

Step 3: Add SSH Key to Your GitHub Account

Copy the SSH key to your clipboard:

cat ~/.ssh/id_rsa.pub

Navigate to Settings > SSH and GPG keys in your GitHub account.

Click on New SSH key, paste the key, and save it.

Screenshot-2024-08-17-005529
Click on Add SSH key

Step 4: Clone the Repository Using SSH

git clone [email protected]:username/repository.git

Since you've added your SSH key to GitHub, you won't need to enter a username or password.

Approach 3: Using Git Credential Helper

Git Credential Helper allows you to store your credentials securely so that you don't have to enter them each time you interact with a remote repository.

Step 1: Enable the Credential Helper

git config --global credential.helper store

This command will configure Git to store your credentials permanently in a file, or use cache to store them temporarily.

Step 2: Authenticate

  • The first time you perform an operation that requires authentication (e.g., git pull or git push), Git will prompt you for your username and password.
  • After entering your credentials, they will be saved and used automatically for subsequent operations.

Step 3: View or Edit Stored Credentials

  • Your credentials are stored in the ~/.git-credentials file.
  • You can edit this file to remove or update your stored credentials.
Screenshot-2024-08-17-010304
I'm uploading this half screen shot due to security reasons (It shows your login credentials).

Next Article
Article Tags :

Similar Reads