Why avoid committing secrets to Git, and secure local alternatives?

This tests basic secret hygiene and environment isolation. A strong answer notes Git history is immutable and distributed, so secrets persist in forks forever, and proposes environment variables or gitignored dotenv files.
What's really being asked
This question evaluates whether a candidate understands the immutable and distributed nature of version control, plus the principle of separating configuration from code. At the senior level, interviewers want to hear that you treat secrets as runtime configuration, not static assets, and that you understand incident response for accidental exposure.
The full answer
First, explain that Git is append-only and distributed. Once a secret is committed, it lives in the object database of every clone, fork, and backup. Even if you delete the file, amend the commit, or make the repository private, the secret remains in history and can be extracted by anyone with a copy. Second, describe the secure local alternative: environment variables or a dotenv file that is explicitly excluded by gitignore. The application reads configuration at startup, keeping credentials out of the repository entirely. Third, mention that for CI/CD, platform-native secret stores like GitHub Secrets, GitLab CI variables, or HashiCorp Vault should inject values at runtime. Fourth, note that if a secret is ever committed, rotation is mandatory because you must assume compromise; rewriting history is insufficient because you cannot guarantee all copies are erased.
The mistakes people make
A major red flag is claiming that deleting the file in a subsequent commit or force-pushing a rewritten history fixes the issue. Another is asserting that private repositories are safe enough for secrets, which ignores insider risk, accidental forking, and future visibility changes. Obfuscation patterns like base64 encoding are also wrong because they are trivially reversible and still store the secret in history. Finally, suggesting hardcoded placeholders with manual replacement is a maintenance nightmare and error-prone.
What usually comes next
The interviewer may ask how you would handle an accidentally committed secret. The correct process is immediate revocation and rotation of the credential, followed by history cleanup only to reduce accidental discovery, not as a fix. They might also ask about secret scanning tools like GitHub secret scanning or truffleHog, or how to manage non-production credentials for a team without sharing them in chat.
A concrete example
Imagine a Node.js application using the AWS SDK. Instead of committing an aws-credentials.json file containing IAM keys, you create a gitignored .env file with AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. At runtime, the application uses dotenv to load these variables. In production, the same variables are injected by the orchestrator or CI secret store, so the code never contains credentials and behaves identically across environments.
Interview question
Which approach best keeps database credentials out of Git while supporting local development?
- a.Encode the credentials with base64 and store them in a committed config file
- b.Commit a template file and manually replace values before each deployment
- c.Store credentials in a committed file and delete it in a later commit if exposed
- d.Use a .env file listed in .gitignore and read via environment variablesCorrect
Why? this is the answer
A gitignored .env file keeps secrets out of the repository while the app reads them at runtime via environment variables. Deleting a committed file later is insufficient because Git history is immutable and distributed, so the secret remains in every clone and fork.
Just read this? Test yourself on what you have been reading.
Read the original → docs.github.com
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles