Git
How to Set Username and Password in Git?
Git is a powerful version control system, but to use it effectively, you need to configure your username and authentication credentials. Setting up your username ensures that your commits are correctly attributed, while securely managing your password is essential for interacting with remote repositories.
In this blog, we’ll explore how to set up your username and password in Git and manage credentials securely using modern authentication practices.
Why Set a Username and Password in Git?
- Attribution: Git uses your username and email to associate commits with you, ensuring proper attribution in collaboration.
- Authentication: For interacting with remote repositories (e.g., GitHub, GitLab), Git requires authentication to push or pull changes.
Configuring Username and Email
Step 1: Set Global Username and Email
To set your username and email globally (applies to all repositories):
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
Step 2: Verify Your Configuration
To confirm your settings:
git config --global user.name
git config --global user.email
These values will appear in your commit history and help identify you as the author.
Setting Username and Email for a Specific Repository
If you want to use a different username or email for a particular repository:
- Navigate to the repository folder:
cd /path/to/your/repository
- Set the username and email locally:
git config user.name "Your Repository-Specific Name" git config user.email "[email protected]"
- Verify the local configuration:
git config user.name git config user.email
Configuring Authentication Credentials
In the past, Git allowed storing plain-text passwords, but modern Git services like GitHub, GitLab, and Bitbucket use more secure methods, such as personal access tokens or SSH keys.
Method 1: Using HTTPS with Personal Access Tokens
Step 1: Create a Personal Access Token
- Go to your Git hosting provider (e.g., GitHub).
- Generate a new personal access token with the necessary permissions (e.g., repo, write).
- Copy the token (you won’t be able to see it again).
Step 2: Use the Token as a Password
When Git prompts you for a password during a git push
or git pull
, paste the token instead of your password.
Method 2: Using SSH Keys
Step 1: Generate an SSH Key
If you don’t already have an SSH key:
ssh-keygen -t ed25519 -C "[email protected]"
Press Enter to accept the default file location, and set a passphrase if desired.
Step 2: Add the SSH Key to Your Git Hosting Service
- Copy your public key:
cat ~/.ssh/id_ed25519.pub
- Add the key to your Git hosting service (e.g., GitHub: Settings > SSH and GPG keys > New SSH key).
Step 3: Test Your SSH Connection
ssh -T [email protected]
Once set up, you won’t need to enter a password for HTTPS-based operations.
Managing Credentials with Git Credential Manager
To avoid entering your username and password repeatedly, use Git Credential Manager to securely store credentials.
Step 1: Install Git Credential Manager
Most modern Git installations include Git Credential Manager. If not, download it from Git Credential Manager.
Step 2: Enable Credential Manager
git config --global credential.helper manager
Step 3: Use Credential Manager
- The first time you push or pull, Git will prompt you for your username and password (or token).
- Credential Manager will securely store the credentials for future use.
Checking Your Git Configuration
To see all your Git configuration settings:
git config --list
For a specific scope, use:
- Global:
git config --global --list
- Local:
git config --local --list
Updating or Removing Credentials
Update Stored Credentials
To update a stored username or password:
- Remove the existing credentials:
git credential-manager clear
- Enter new credentials during the next push or pull operation.
Remove Global Configuration
To unset the global username and email:
git config --global --unset user.name
git config --global --unset user.email
Best Practices for Managing Git Credentials
- Avoid Storing Plain-Text Passwords: Use personal access tokens or SSH keys instead.
- Use Credential Helpers: Save time and enhance security with tools like Git Credential Manager.
- Keep Your Tokens Secure: Store personal access tokens in a secure location, and revoke them immediately if compromised.
- Use Separate Credentials for Different Accounts: Configure credentials locally for repositories tied to different accounts.
Conclusion
Setting your username and password in Git is a critical step in ensuring proper attribution and secure access to your repositories.
By following the methods and best practices outlined in this guide, you can configure your credentials efficiently and securely, enabling smooth collaboration and version control.