Using GitHub Private Repositories
This guide will walk you through the process of using private GitHub repositories as templates with gitit.
Prerequisites
Before you begin, you'll need:
- A GitHub account with access to private repositories
- A GitHub Personal Access Token (PAT)
- gitit installed on your system
Creating a GitHub Personal Access Token
To access private repositories, you'll need a Personal Access Token:
- Log in to your GitHub account
- Click on your profile picture in the top-right corner
- Select Settings from the dropdown menu
- Scroll down to the bottom of the left sidebar and click on Developer settings
- Click on Personal access tokens → Tokens (classic)
- Click Generate new token → Generate new token (classic)
- Give your token a descriptive name (e.g., "gitit Access")
- Select the following scopes:
- repo (Full control of private repositories)
- read:packages (Optional - if you need to access template packages)
- Click Generate token
- Important: Copy your token immediately. You won't be able to see it again!
Cloning a Private Repository
Once you have your personal access token, you can use it to clone private repositories:
gitit github:username/private-repo my-project --auth "ghp_your_token_here"
Replace:
username
with the GitHub username or organization nameprivate-repo
with your private repository nameghp_your_token_here
with the personal access token you created
Using Environment Variables
For security and convenience, you can set your token as an environment variable:
export GITIT_AUTH="ghp_your_token_here"
gitit github:username/private-repo my-project
Add this to your .bashrc
, .zshrc
, or equivalent shell configuration file for persistent access.
GitHub API Headers
When you provide an authentication token, gitit adds the following headers to GitHub API requests:
Authorization: Bearer your-token
Accept: application/vnd.github+json
X-GitHub-Api-Version: 2022-11-28
Troubleshooting
"Not found" or "404" errors
If you get "Not found" or "404" errors when trying to clone a private repository, check:
- Repository name is correct: Double-check the repository name and username/organization
- Token has proper permissions: Ensure you've given it the "repo" scope
- Access to the repository: Confirm that your GitHub account has access to the repository
"Rate limit exceeded" errors
GitHub API has rate limits. With authentication, these limits are higher but still exist:
Use token authentication: Authenticated requests have higher rate limits
Check your rate limit status: You can check your current rate limit status with:
bashcurl -H "Authorization: Bearer your-token" https://api.github.com/rate_limit
Advanced Usage
GitHub Enterprise
If you're using GitHub Enterprise, you can set a custom API URL:
export GITIT_GITHUB_URL="https://github.your-company.com/api/v3"
gitit github:username/private-repo my-project --auth "your-token"
Specifying Branches
To clone a specific branch from a private GitHub repository:
gitit github:username/private-repo#develop my-project --auth "your-token"
Cloning Subdirectories
To clone only a specific subdirectory from a private repository:
gitit github:username/private-repo/path/to/directory my-project --auth "your-token"
Combined Branch and Subdirectory
You can specify both a branch and a subdirectory:
gitit github:username/private-repo/path/to/directory#develop my-project --auth "your-token"
Configuration File
You can store your GitHub authentication in your gitit.config.ts
file:
// gitit.config.ts
export default {
auth: process.env.GITIT_AUTH || 'your-token',
// other options...
}
Just be careful not to commit this file to version control if it contains your actual token.
CI/CD Integration
For CI/CD pipelines, you can use the built-in GitHub token:
# GitHub Actions example
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Clone GitHub template
run: gitit github:username/private-repo my-project
env:
GITIT_AUTH: ${{ secrets.GITHUB_TOKEN }}
Best Practices
- Use fine-grained tokens: GitHub now offers fine-grained tokens with more precise permissions
- Set token expiration: Always set an expiration date for your tokens
- Use environment variables: Avoid putting your token directly in commands
- Use specific versions: Reference specific tags or commits for consistency
- Regularly audit tokens: Periodically review and revoke unused tokens
- Use separate tokens: Create different tokens for different purposes
GitHub CLI Integration
If you're using GitHub CLI (gh
), you can create a token and use it directly:
# Create a token with GitHub CLI
gh auth token
# Use the token with gitit
gitit github:username/private-repo my-project --auth "$(gh auth token)"
This approach avoids storing the token in your environment or files.