GitHub & Projects
How to Keep Secrets and Junk Out of Your GitHub Repository
A leaked API key or a folder of build junk can undo good work. Set up .gitignore early, keep secrets in environment variables, and check every push.
Every student developer eventually pushes something they should not have. Sometimes it is harmless clutter: a build folder, a cache directory, a database file from a local test. Sometimes it is worse: an API key pasted into a config file, a password in a script, a private token committed because it happened to be sitting in the project folder. The uncomfortable part is that Git is designed to remember. Deleting the file in your next commit does not remove it from history, and anyone who cloned the repository in between still has a copy.
This is one of the most preventable problems in student projects. You need three habits set up before your first push: a .gitignore that matches your stack, secrets kept outside the code, and a short check before every push.
Why a clean repository matters beyond security
Security is the headline reason, but it is not the only one. A repository full of junk is harder for everyone to use. Reviewers cannot tell which files matter. Teammates clone hundreds of megabytes they do not need. Automated checks run against generated files instead of source. And when something breaks, nobody is sure whether the problem is in the code or in a stale local file that should never have been committed. A clean repository signals that you understand the difference between source, which belongs in version control, and everything your tools generate, which does not.
There is also a portfolio effect. A tidy root with a clear README and sensible configuration reads as careful work; a root scattered with database files and logs does not. The habits that protect your secrets also make your work look professional.
What should never be committed
A useful rule is to ask one question about every file: would a teammate need this exact file, unchanged, to build and run the project on a fresh machine? If the answer is no, it probably does not belong in the repository. In practice, that means keeping out the following:
- Secrets: API keys, access tokens, passwords, private keys, and connection strings that contain credentials. These belong in environment variables or a secrets manager, never in code or committed config.
- Local environment files: the .env file itself. Commit a .env.example with placeholder names instead, so teammates know what to set without seeing real values.
- Dependencies: folders your package manager installs, such as node_modules. The lockfile records what to install; the folder itself is rebuilt on demand.
- Build output: dist, build, .next, and similar generated folders. They are produced from source and change constantly, so they only create noise and merge conflicts.
- Local data: test databases, uploaded files from your machine, logs, and cache folders. They reflect your machine, not the project.
- Editor and OS files: settings folders specific to your editor, and operating system files such as .DS_Store. They mean nothing to anyone else.
Everything on this list is private, machine-specific, or reproducible from something else. Source code, documentation, tests, and lockfiles fail all three tests — which is why they belong in the repository.
Set up .gitignore before your first commit
Timing is the whole trick. A .gitignore file tells Git which paths to leave untracked, but it only works on files Git is not already tracking. If you commit node_modules first and add it to .gitignore later, Git keeps tracking it until you explicitly untrack it. So create the .gitignore as one of the first files in the project, ideally in the same commit as your initial scaffold.
Start from the standard ignore list for your stack, then add the project-specific lines. For a typical web project, a starting point looks like this:
# Dependencies
node_modules/
# Build output
dist/
build/
.next/
# Environment and secrets
.env
.env.*
!.env.example
*.pem
*.key
# Local data and logs
*.db
*.log
uploads/
# Editor and OS files
.vscode/
.idea/
.DS_StoreTwo lines deserve attention. The .env.* pattern with the !.env.example exception ignores every environment file except the example teammates should see. The *.pem and *.key patterns catch private keys by extension. Adapt the list to your stack, but keep the structure: dependencies, build output, secrets, local data, editor files.
Keep secrets in environment variables, not in code
Ignoring .env only helps if your code actually reads secrets from the environment. The pattern is simple: your code asks the environment for a value by name, and the value itself lives outside the repository. Locally, that means a .env file on your machine that Git ignores. On a hosting platform, it means the platform's own settings screen, where you paste the value once and it never touches your code. In both cases, the repository contains only the name of the variable, which is safe to share.
The .env.example file lists every variable the project needs, with fake placeholder values, so a teammate can copy it and fill in their own. Placeholders should look obviously fake — something like your_api_key_here, never a real key with a few characters changed.
Run a two-minute check before every push
Setup prevents most accidents, but a final check catches the rest. Before pushing, spend two minutes on the following:
- Read the status output: run git status and actually read the list of files about to be committed. Unfamiliar filenames are the warning sign — investigate before you commit, not after.
- Scan the diff: look through the changes for anything that resembles a key, token, or password, especially in config files and scripts. Searching your staged changes for words like key, token, secret, and password takes seconds.
- Check new folders: if a whole new directory appears in the commit, ask whether it is source or generated output. Generated output means your .gitignore has a gap — fix the gap now.
- Confirm the example file: if you added a new environment variable, make sure .env.example names it with a placeholder, and that your real .env is still untracked.
This pairs with How to Review Your Own Pull Request Before Anyone Else Sees It: the diff you read for logic is the same diff you scan for secrets, so both habits cost one pass.
If you already pushed a secret, act in the right order
Suppose, as a hypothetical example, you realize an API key went into a public repository yesterday. The instinct is to delete the file and push again. That is the second step, not the first. The first step is to treat the secret as compromised: revoke or rotate it at the provider, so the exposed value stops working. Only then clean the repository — remove the file, untrack it, add it to .gitignore, and if the secret sits in history, rewrite that history or start from a clean state, knowing that copies may already exist elsewhere. Rotation is what actually protects you; cleanup only limits further exposure.
For ordinary Git mistakes without secrets, the approach in How to Fix Git Mistakes Without Panicking applies. The secret case is where speed beats tidiness: rotate first, tidy second.
Git remembers everything you push. The goal is not to be perfect — it is to make the safe path the default path, so one tired evening cannot undo your work.
Make it a habit, not a cleanup project
Create the .gitignore with the scaffold, read secrets from the environment from day one, keep .env.example honest, and read your diff before you push. Four small habits protect every commit that follows and leave a repository that is smaller, clearer, and safe to share. For tooling that helps you present the repositories you keep clean, see how ZEVQYN works.