Where to store a Git personal access token (GitHub, GitLab)
Codemia Knowledge Hub ·
Store a Git personal access token (PAT) in a platform credential manager or a dedicated secret store. Do not keep it in your repository, your shell history, or a plaintext config checked into version control. Treat it like a password with an API attached.
Use a credential manager for interactive Git
For everyday git clone, git pull, and git push, the best place is the operating system's credential storage, accessed through a Git credential helper. You get encryption at rest, the token stays out of scripts, and authentication is mostly invisible after the first setup.
Options include:
- Git Credential Manager on Windows, macOS, and Linux
osxkeychainon macOSlibsecreton Linux desktop environments
A typical setup:
Or on macOS:
The next authenticated Git operation asks for your username and token once. The helper then keeps the token in the system keychain instead of a plaintext file.
When environment variables are acceptable
Environment variables suit short-lived automation, local scripts, and CI jobs, but they are a poor long-term store for interactive Git use. Handled carelessly, they leak into process listings, shell history, debug output, or crash dumps.
For example, a script might read a token like this:
That is fine in tightly controlled automation, especially when a CI secret manager injects the value. It is much less attractive on a developer laptop, where shells, logs, and copied commands pile up.
Storage to avoid
The weakest pattern is putting the token in a repository file or a committed script. Even a private repository is the wrong place, because accidental pushes, copied snippets, and archived logs spread secrets further than you expect.
Git's plaintext credential store is also risky:
It works, but it writes credentials to a local file in plaintext. Use it only if you accept that tradeoff and have no better option.
Secret stores for CI and teams
On shared systems, use the platform's secret manager:
- GitHub Actions secrets
- GitLab CI variables
- cloud secret managers such as AWS Secrets Manager or Azure Key Vault
- container orchestrator secrets for runtime injection
The pipeline then injects the token only into the job that needs it:
Keep the token narrowly scoped here too. A token that only needs repository write access should not have admin or package scopes.
Scope, expiry and rotation
Storage is only part of it. Also:
- give the token the smallest possible scope
- set an expiration date when the provider supports it
- rotate it when people leave the team or workflows change
- revoke it immediately if it shows up in logs or commits
A well-scoped token in a proper credential store is far safer than a broad token sitting in an environment file.
Mistakes that leak tokens
- Committing it, even briefly. Once a token is in Git history, removing it from the current branch is not enough.
- Using environment variables for everything. They help in automation but are often less safe than a keychain for interactive use.
- Sharing one powerful long-lived token across machines. That kills auditability and makes rotation painful. Prefer per-user or per-workflow tokens.
Related reading
- Which cryptographic hash function should I choose?
- Which one is better to user between Parse, Firebase and AWS Cognito?
- Whitelist kube-system namespace using NetworkPolicy
- Why are no Amazon S3 authentication handlers ready?
- Which characters are illegal within a branch name?
- Which commit has this blob?
- Why cant i import WithMockUser to my test
- Why do we need private subnet in VPC?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.