Connect with us

Git

How to Set Username and Password in Git?

Spread the love

Git is a powerful version control system that helps developers collaborate on projects. To ensure secure and identifiable contributions, Git uses authentication methods like usernames and passwords or SSH keys.

This blog explains how to set your username and password in Git, including modern practices like using personal access tokens (PATs) instead of plain passwords for security.

Why Set Username and Password in Git?

  • Identify Contributions: Git tracks changes using your username and email, linking commits to your identity.
  • Authenticate Access: Passwords (or access tokens) ensure only authorized users can push changes to repositories.
  • Collaborate Securely: Modern Git hosts like GitHub, GitLab, and Bitbucket require secure authentication methods.

Modern Authentication Practices

Since August 2021, GitHub and other Git services no longer support basic authentication using a plain password. Instead, personal access tokens (PATs) or SSH keys are recommended for security. While this guide references setting “passwords,” note that PATs are the preferred method.


Step 1: Setting Your Username and Email

Git uses your username and email to identify your commits. Setting these correctly ensures your contributions are recognized.

1. Set Username

Run the following command in your terminal:
“`bash
git config –global user.name “Your Name”

Replace `Your Name` with your desired username.  

#### **2. Set Email**  
Run the following command:  

bash
git config –global user.email “[email protected]

Replace `[email protected]` with your email address.  

#### **3. Verify the Configuration**  
To confirm your username and email are set, use:  

bash
git config –global –list

**Output Example**:  

user.name=Your Name
[email protected]

---

## **Step 2: Setting Up Authentication**  

Modern Git hosts require either:  
- **Personal Access Tokens (PATs)** for HTTPS access.  
- **SSH keys** for key-based authentication.  

### **Using Personal Access Tokens (HTTPS)**  

#### **1. Generate a PAT**  
- **GitHub**:  
  - Go to your GitHub account settings > **Developer settings** > **Personal access tokens**.  
  - Click **Generate new token**, select the scopes (permissions), and generate the token.  

#### **2. Use the PAT in Git**  
When pushing or pulling from a remote repository, Git prompts for your username and password. Enter your username and use the PAT instead of your password.  

To cache credentials, use a credential manager (explained below).  

---

### **Using SSH Keys (Recommended)**  

#### **1. Generate an SSH Key**  
Run the following command to create a new SSH key:  

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

#### **2. Add the Key to Your Git Host**  
- Copy the public key:  

bash
cat ~/.ssh/id_rsa.pub

- Paste it into the SSH key section of your Git host (e.g., GitHub > **Settings** > **SSH and GPG keys**).  

#### **3. Test the Connection**  
Run:  

bash
ssh -T [email protected]

---

## **Step 3: Cache Your Credentials**  

Repeatedly entering your username and PAT/SSH key can be cumbersome. Credential caching makes authentication seamless.  

#### **1. Use Git Credential Manager (HTTPS)**  
Git Credential Manager stores your credentials securely:  

bash
git config –global credential.helper cache

To set a timeout (e.g., 1 hour):  

bash
git config –global credential.helper ‘cache –timeout=3600’

#### **2. Store Credentials Permanently**  
For long-term storage:  

bash
git config –global credential.helper store

Git saves your credentials to a plain text file (less secure).  

---

### **Updating Username or Password**  

If you need to update your credentials:  

1. **Change Username**:  

bash
git config –global user.name “New Name”

2. **Update Password or PAT**:  
   For HTTPS, use:  

bash
git credential-manager clear

   Then re-authenticate the next time you push or pull.  

---

### **Best Practices for Git Authentication**  

1. **Use PATs or SSH Keys**: Avoid using plain passwords for enhanced security.  
2. **Enable Two-Factor Authentication (2FA)**: Protect your account with 2FA for an extra layer of security.  
3. **Keep Keys Secure**: Store SSH private keys securely and never share them.  
4. **Use Scoped PATs**: When generating PATs, limit their permissions to minimize risks.  
5. **Regularly Rotate Tokens**: Periodically update your PATs or SSH keys for added security.  

---

### **Troubleshooting**  

#### **1. Authentication Errors**  
- Verify your username and PAT are correct.  
- Check if 2FA is enabled and configured correctly.  

#### **2. Credential Issues**  
- Clear cached credentials:  

bash
git credential-manager clear

- Reconfigure the credential helper.  

#### **3. SSH Connection Issues**  
- Ensure the SSH agent is running:  

bash
eval “$(ssh-agent -s)”
ssh-add ~/.ssh/id_rsa
“`

  • Confirm the correct SSH key is linked to your Git host.

Conclusion

Setting your username and password (or token) in Git ensures secure and identifiable contributions to your repositories. By following this guide and adopting modern authentication practices, you can collaborate effectively while keeping your accounts safe.


Spread the love
Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *