← All posts
Practice

How to create or update a GitHub Personal Access Token for a private repository

This guide covers how to create a new Fine-grained Personal Access Token, configure its permissions correctly, replace an outdated token on a server, and fix common authorization errors.

GitHub Personal Access Token (PAT) is a personal access token that lets applications, servers, and automated deployment systems securely work with GitHub repositories without using your account password.

Tokens are used for automatically updating websites, pushing changes to GitHub, working with the Git API, and connecting third-party tools.

In this guide we'll cover how to create a new Fine-grained Personal Access Token, configure its permissions correctly, replace an outdated token on a server, and fix common authorization errors.

1. Why you need a GitHub Token

Say you have a site example.com whose source code lives in a private repository:

https://github.com/example-user/my-website

The site runs on a Linux server and uses Git to pull updates and push changes.

Running git pull and git push over HTTPS requires authentication. A regular GitHub password no longer works for these operations -- a Personal Access Token takes its place.

A token is especially useful when:

  • the site updates itself automatically from GitHub;

  • file changes are pushed to the repository straight from the server;

  • the CMS has a built-in Git Deploy module;

  • deployment runs through scripts;

  • third-party applications connect to the private repository.

2. Fine-grained vs. Classic Token: what's the difference

GitHub supports two types of personal tokens.

Fine-grained Personal Access Token -- a more flexible option that lets you restrict access to specific repositories and individual permissions.

Personal Access Token (Classic) -- the older format, which uses broader permission groups (scopes).

For most new integrations, a Fine-grained Token is recommended, as long as the tool you're using supports it.

For example, you can let a token work with just one repository instead of granting access to every project on the account.

3. Creating a new Fine-grained Personal Access Token

Go to:

https://github.com/settings/personal-access-tokens

Click Generate new token.

Fill in the settings:

SettingExample
Token namewebsite-deploy
DescriptionServer access to GitHub
Resource ownerexample-user
Expiration90 days
Repository accessOnly select repositories
Selected repositoriesmy-website

It's best to limit the token's lifetime -- say, 30 or 90 days. You can reissue it later if needed.

Setting permissions

Open the Repository permissions section.

Different tasks need different rights.

TaskPermissions
Only pulling updatesContents: Read-only
Git Pull from a private repositoryContents: Read-only
Git Commit & PushContents: Read and write
Modifying a GitHub Actions workflowContents: Read and write + Workflows: Read and write

The Metadata: Read-only permission is usually set automatically.

It's important to grant only the permissions the application actually needs.

Once configured, click Generate token.

GitHub will show the newly created token. Copy it and store it somewhere secure -- the full token value may not be shown again later.

4. Replacing a token on a Linux server

Say the site's source code lives in:

/var/www/example.com/public_html

Connect to the server over SSH.

Move into the project directory:

cd /var/www/example.com/public_html

Check the current remote:

git remote -v

In our example it should be an HTTPS repository address:

https://github.com/example-user/my-website.git

If it's an SSH-style address like git@github.com:..., you should set up an SSH key instead of an HTTPS token.

To verify access manually, run:

git fetch origin main

When Git asks for credentials:

Username for 'https://github.com':

enter:

example-user

In the Password field, paste the Personal Access Token instead of your GitHub password.

If the token has the right permissions, Git will get access to the repository.

Storing the token securely

For Git to work automatically, you can set up a separate credential store.

For example, for the Linux user the site runs as, you can create a file in its home directory.

touch ~/.git-credentials
chmod 600 ~/.git-credentials

To avoid typing the token straight into your shell history, use a hidden prompt:

read -rsp "GitHub token: " GH_TOKEN
echo

After pasting the new token, write it to the file:

printf 'https://example-user:%s@github.com\n' "$GH_TOKEN" > ~/.git-credentials
chmod 600 ~/.git-credentials
unset GH_TOKEN

Point the repository at that file:

git config --local credential.helper \
  "store --file=$HOME/.git-credentials"

Git can now use the stored credentials without an interactive prompt.

Important: the Git Credential Store keeps the token unencrypted. That means the file's permissions need to be tightly restricted, and for production systems a secrets manager or a dedicated credential manager is the preferred approach. The file must never sit inside the site's public directory.

If your CMS has its own secure way of storing GitHub tokens, use that instead of building a duplicate store.

5. Verifying Git Pull and Git Push

After updating the token, check that you can fetch changes:

git fetch origin main

If there are no errors, read access works.

To check whether you can push changes:

git push --dry-run origin main

This command checks the push without actually updating the remote branch, though it doesn't guarantee every server-side check will pass on a real push.

If you have local commits ready to go, push them:

git push origin main

6. Common GitHub Token errors

Error 403: Write access to repository not granted

remote: Write access to repository not granted.
fatal: unable to access ... error: 403

The cause: the token doesn't have write access to the repository, or a different account with insufficient permissions is being used.

The fix:

  1. Open the GitHub token settings.

  2. Check the selected repository.

  3. Set Contents: Read and write.

  4. Save the permissions.

  5. Try the push again.

Error when modifying a GitHub Actions workflow

refusing to allow a Personal Access Token
to create or update workflow
without workflow scope

This error appears when a commit adds or changes files under:

.github/workflows/*.yml

A Fine-grained Token needs the Workflows: Read and write permission; a Classic Token needs the workflow scope, together with the required repository rights.

After changing the permissions, try the push again.

Error: could not read Username

fatal: could not read Username for 'https://github.com'

Git can't obtain credentials.

Check your credential helper settings, the CMS's authentication method, and that the repository address is correct.

After a failed push, the changed files seem to be gone

This is a common situation.

For example, the system ran:

git commit -m "Update website"

But the following git push failed.

In that case, the changes are already saved in the local commit, so git status may not show any changed files.

Check:

git status -sb

If it shows:

## main...origin/main [ahead 1]

that means one local commit still hasn't been pushed to GitHub.

Once authentication is fixed, it's enough to run:

git push origin main

There's no need to create the commit again.

Non-fast-forward error

If the remote repository has new commits that don't exist locally, Git may reject the push.

Don't reach straight for git push --force.

First check the state of your branches and save any unsaved changes. Then reconcile the history in a suitable way.

7. How to tell a repository is in sync

Run:

git status -sb

Example of a fully synced repository:

## main...origin/main

If it shows [ahead 2], the local branch has two commits that haven't been pushed.

If it shows [behind 3], the remote branch has three commits that haven't been pulled locally yet.

For a more detailed check:

git rev-list --left-right --count HEAD...origin/main

A result of 0 0 means the local and remote branches are on the same commit.

8. Security recommendations

Don't publish tokens in source code, GitHub repositories, screenshots, or logs.

For every server or project, it's best to create a separate token with the minimum permissions it actually needs.

If you suspect a token has leaked, revoke it immediately and issue a new one.

It's also worth regularly checking token expiration dates, removing unused keys, and keeping configuration files out of public access.

Conclusion

A Fine-grained Personal Access Token is a convenient way to set up GitHub access for sites, servers, and external applications.

A properly configured token lets you securely pull changes from a private repository, push commits, and automate project deployment.

The key points: keep permissions tight, remember GitHub Actions needs its own separate permission, and keep in mind that Git Commit and Git Push are two different operations.

Official GitHub documentation:

Need help with something similar? Let's talk ↗