Git uses authentication to securely connect with remote repositories on platforms like GitHub, GitLab, and Bitbucket for collaborative development.
- Verifies user identity before repository access.
- Required for clone, pull, and push operations.
- Ensures secure collaboration on shared codebases
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/repositoryIf 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
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.

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.

Step 4: Cache the Credentials
To avoid re-entering your PAT, you can cache your credentials:
git config --global credential.helper cacheApproach 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 "your_email@example.com"When prompted, press Enter to save the key to the default location, or specify a custom path.

Step 2: Add SSH Key to the SSH-Agent
Start the SSH-Agent in the background:
eval "$(ssh-agent -s)"
Add your SSH private key to the SSH-Agent:
ssh-add ~/.ssh/id_rsa
Step 3: Add SSH Key to Your GitHub Account
Copy the SSH key to your clipboard:
cat ~/.ssh/id_rsa.pubNavigate to Settings > SSH and GPG keys in your GitHub account.
Click on New SSH key, paste the key, and save it.

Step 4: Clone the Repository Using SSH
git clone git@github.com:username/repository.gitSince 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 storeThis 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.
