Step-by-Step Guide to Clone and Write repos with GitHub Personal Access Tokens

GitHub’s fine-grained personal access tokens allow precise control over repository access. Here’s a step-by-step guide to generate and manage a token for a specific repository:


1. Access Developer Settings

  1. Log in to your GitHub account.
  2. Click on your profile picture at the top right corner.
  3. Select “Settings.”
  4. From the left sidebar, scroll down and click on “Developer settings.”

2. Create a Fine-Grained Personal Access Token

  1. In the left sidebar, select “Fine-grained personal access tokens.”
  2. Click on “Generate new token.”

3. Configure Token with Write Permissions

  • Token name: Assign a descriptive name, e.g., “Read/Write Access for [Repository Name].”
  • Expiration: Set an expiration date for security purposes.
  • Resource owner: Ensure it’s your personal account or the organization that owns the repository.
  • Repository access: Choose “Only select repositories” and select your desired repository.
  • Repository permissions:
    • Contents: Set to “Read and write” for editing capabilities.
  • Review settings and click “Generate token.”

4. Save Token

Copy the token immediately after generation—it will only be visible once. Store it securely.


Push Changes Using Token

1. Use HTTPS with Token in URL

Use a URL formatted like this to clone the repository:

git clone https://YOUR_USERNAME:YOUR_TOKEN@github.com/YourUserName/YourRepo.git

If you’ve already cloned the repository, update the remote URL:

git remote set-url origin https://YOUR_USERNAME:YOUR_TOKEN@github.com/YourUserName/YourRepo.git

2. Commit and Push Changes

  1. Stage changes: git add .
  2. Commit changes: git commit -m "Your commit message"
  3. Push changes: git push origin main

Important Security Considerations

  • Never commit or share your token publicly.
  • Use tokens with the minimum necessary permissions.
  • Regularly review and revoke unused tokens.
  • For additional security, consider using SSH keys.

Error Handling

If write permissions weren’t initially granted, you may encounter:

remote: Write access to repository not granted.
fatal: unable to access 'https://github.com/...': The requested URL returned error: 403

To resolve:

  1. Either create a new token with read/write permissions, or
  2. Edit an existing token’s permissions by following these steps:
    • Navigate to “Developer settings.”
    • Select “Fine-grained personal access tokens.”
    • Locate and edit the token.
    • Update permissions (e.g., change from “Read-only” to “Read and write”).
    • Save changes.

Conclusion

Fine-grained tokens provide enhanced control for managing repository access. Remember to prioritize security and minimize permissions to reduce risks.?


Discover more from Science Comics

Subscribe to get the latest posts sent to your email.

Leave a Reply

error: Content is protected !!