Last Updated : 31 May, 2024
Summarize
Comments
Improve
As GitHub has transitioned away from password-based authentication, using tokens for operations like pushing to repositories is now the recommended approach. This method enhances security and aligns with modern best practices. This guide will walk you through the steps to authenticate your Git push commands using a personal access token.
Steps to Authenticate Git Push
Step 1: Generate a Personal Access Token
1. Log in to GitHub:
Go to GitHub and sign in to your account.
2. Access Token Settings:
- Click on your profile picture in the upper-right corner and select Settings.
- In the left sidebar, click Developer settings.
- In the left sidebar again, click Personal Access Tokens.
3. Generate New Token:
- Click on Generate new token.
- Give your token a descriptive name (e.g., “Git push access”).
- Select the scopes or permissions you need. For pushing to repositories, you will need repo (full control of private repositories).
4. Generate and Copy Token:
- Click Generate token.
- Copy the token to your clipboard. Note: You won’t be able to see this token again, so store it securely.
Step 2: Configure Git to Use Your Token
To authenticate Git operations with your token, you need to update the URL of your repository to include the token. This can be done in several ways:
Method 1: Using Git Command Line
1. Navigate to Your Repository:
- Open your terminal or command prompt.
- Change the directory to your local repository: cd path/to/your/repo.
2. Update Remote URL:
Update the remote URL to include your token:
git remote set-url origin https://<TOKEN>@github.com/username/repository.git
Replace <TOKEN> with your actual token, username with your GitHub username, and repository with the name of your repository.
Method 2: Using Git Credential Manager (Recommended)
1. Install Git Credential Manager:
Ensure you have Git Credential Manager installed. It’s bundled with Git for Windows and can be installed separately for other platforms.
2. Configure Credential Manager:
Open your terminal or command prompt and configure Git to use the credential manager:
Configure Credential Manager
3. Push to Repository:
When you perform a git push, Git will prompt you to enter your username and personal access token. Enter your GitHub username and paste the token as the password.
Method 3: Using SSH (Alternative Approach)
For users who prefer not to include their tokens in URLs or handle tokens directly, SSH keys offer a robust alternative.
1. Generate SSH Key:
If you haven’t already, generate an SSH key:
ssh-keygen -t ed25519 -C "[email protected]"
Follow the prompts to save the key and set a passphrase.
Add SSH Key to GitHub:
2. Copy the SSH key to your clipboard:
cat ~/.ssh/id_ed25519.pub
Go to GitHub, navigate to Settings > SSH and GPG keys, and click New SSH key. Paste your key and save.
3. Update Remote URL:
Change the remote URL to use SSH:
git remote set-url origin [email protected]:username/repository.git
Step 3: Test Your Configuration
Push to Repository:
Make a change in your repository, commit it, and try to push:
git add .
git commit -m "Test commit"
git push origin main
If everything is configured correctly, your push should succeed without prompting for credentials.