How to Push to a Private GitHub Repository Using a Fine-Grained Personal Access Token
If you have an existing local Git repository and need to push it to a private GitHub repository, but GitHub is rejecting your credentials, you may need to use a fine-grained personal access token (PAT). This guide will walk you through the process step by step. Step 1: Generate a Fine-Grained Personal Access Token (PAT) GitHub has moved away from password authentication for Git operations, so you must use a personal access token (PAT) instead. 1.1 Navigate to GitHub Settings Go to GitHub Personal Access Tokens. Click on "Generate new token (fine-grained)". 1.2 Configure Token Permissions Repository Access: Select the specific repository you want to push to. Permissions: Under "Repository permissions," set "Read and Write" access for "Contents." Expiration: Choose an expiration date or select "No expiration" (not recommended for security reasons). Click "Generate token" and copy the token immediately (you won’t see it again). Step 2: Set the Remote URL with the PAT Now, update your Git remote URL to authenticate using the token. 2.1 Navigate to Your Local Repository Open a terminal and move to your project directory: cd /path/to/your/local/repository 2.2 Update the Remote URL Run the following command, replacing {your-personal-token}, {github_username}, and {private-repo-name} accordingly: git remote set-url origin https://{your-personal-token}@github.com/{github_username}/{private-repo-name}.git Note: The token will be stored in your Git configuration. For security reasons, it’s best to use a Git credential manager instead of embedding it in the URL. Step 3: Push Your Code to GitHub 3.1 Add and Commit Changes If you haven’t already committed your changes, do so: git add . git commit -m "Initial commit" 3.2 Push to the Private Repository Now, push your code to GitHub: git push -u origin main If everything is set up correctly, your repository should now be updated on GitHub. Step 4: Verify Your Push Go to your GitHub repository and refresh the page. You should see your files successfully pushed. Optional: Use a Credential Manager for Better Security Instead of storing your token in the remote URL, you can configure a credential manager to securely store and automatically use your token when needed. For macOS: git credential-osxkeychain For Windows: git credential-manager-core For Linux: git credential-store Then, configure Git to use the credential manager: git config --global credential.helper store This way, you won’t need to include your token in the remote URL manually. Conclusion By following these steps, you should be able to push to a private GitHub repository using a fine-grained personal access token. This method ensures secure authentication without exposing your GitHub password. Always remember to keep your access tokens safe and use GitHub's credential storage features for added security.

If you have an existing local Git repository and need to push it to a private GitHub repository, but GitHub is rejecting your credentials, you may need to use a fine-grained personal access token (PAT). This guide will walk you through the process step by step.
Step 1: Generate a Fine-Grained Personal Access Token (PAT)
GitHub has moved away from password authentication for Git operations, so you must use a personal access token (PAT) instead.
1.1 Navigate to GitHub Settings
- Go to GitHub Personal Access Tokens.
- Click on "Generate new token (fine-grained)".
1.2 Configure Token Permissions
- Repository Access: Select the specific repository you want to push to.
-
Permissions:
- Under "Repository permissions," set "Read and Write" access for "Contents."
- Expiration: Choose an expiration date or select "No expiration" (not recommended for security reasons).
- Click "Generate token" and copy the token immediately (you won’t see it again).
Step 2: Set the Remote URL with the PAT
Now, update your Git remote URL to authenticate using the token.
2.1 Navigate to Your Local Repository
Open a terminal and move to your project directory:
cd /path/to/your/local/repository
2.2 Update the Remote URL
Run the following command, replacing {your-personal-token}
, {github_username}
, and {private-repo-name}
accordingly:
git remote set-url origin https://{your-personal-token}@github.com/{github_username}/{private-repo-name}.git
Note: The token will be stored in your Git configuration. For security reasons, it’s best to use a Git credential manager instead of embedding it in the URL.
Step 3: Push Your Code to GitHub
3.1 Add and Commit Changes
If you haven’t already committed your changes, do so:
git add .
git commit -m "Initial commit"
3.2 Push to the Private Repository
Now, push your code to GitHub:
git push -u origin main
If everything is set up correctly, your repository should now be updated on GitHub.
Step 4: Verify Your Push
Go to your GitHub repository and refresh the page. You should see your files successfully pushed.
Optional: Use a Credential Manager for Better Security
Instead of storing your token in the remote URL, you can configure a credential manager to securely store and automatically use your token when needed.
For macOS:
git credential-osxkeychain
For Windows:
git credential-manager-core
For Linux:
git credential-store
Then, configure Git to use the credential manager:
git config --global credential.helper store
This way, you won’t need to include your token in the remote URL manually.
Conclusion
By following these steps, you should be able to push to a private GitHub repository using a fine-grained personal access token. This method ensures secure authentication without exposing your GitHub password. Always remember to keep your access tokens safe and use GitHub's credential storage features for added security.