October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

How to Keep Secrets Out of Git: Environment Variables, .gitignore, and Secret Managers

Use environment variables or secret stores for real credentials, ignore local secret files before tracking them, and rotate any value that gets committed.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Keep credentials out of tracked files and supply them to your application at runtime through environment variables or a secret store. Add local secret files to .gitignore before Git tracks them, but remember: ignoring a file does not remove a secret already committed. If a credential has been exposed, revoke or rotate it promptly—even if you delete the visible line.

What belongs in Git—and what does not?

Commit application code and configuration that is safe to share. Do not commit live passwords, API tokens, private keys, database connection strings containing credentials, or other values that grant access. Instead, have the application read a named value at runtime:

As an Amazon Associate I earn from qualifying purchases.

const databaseUrl = process.env.DATABASE_URL;
const apiToken = process.env.API_TOKEN;

if (!databaseUrl || !apiToken) {
  throw new Error("Required configuration is missing");
}

This example shows variable names only; set actual values through your local environment, deployment platform, or secret manager. Environment variables are a delivery interface, not a complete security system: restrict who can set or read them, and avoid printing their values in logs or error messages.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For local development, use a shell variable or a framework-supported local file such as .env. Commit a template such as .env.example with variable names and unmistakably fake placeholders, so collaborators know what to configure without receiving working credentials. This template is a practical convention, not a requirement imposed by GitHub or OWASP.

#1 Best Overall
Yubico - Security Key C NFC - Basic Compatibility - Multi-Factor authentication (MFA) Security Key and passkey, Connect via USB-C or NFC, FIDO Certified
  • POWERFUL SECURITY KEY: The Security Key C NFC is the essential physical passkey for protecting your digital life from phishing attacks. It ensures only you can access your accounts.
  • WORKS WITH 1000+ ACCOUNTS: Compatible with Google, Microsoft, and Apple. A single Security Key C NFC secures 100 of your favorite accounts, including email, password managers, and more.
  • FAST & CONVENIENT LOGIN: Plug in your Security Key C NFC via USB-C and tap it, or tap it against your phone (NFC) to authenticate. No batteries, no internet connection, and no extra fees required.
  • TRUSTED PASSKEY TECHNOLOGY: Uses the latest passkey standards (FIDO2/WebAuthn & FIDO U2F) but does not support One-Time Passwords. For complex needs, check out the YubiKey 5 Series.
  • BUILT TO LAST: Made from tough, waterproof, and crush-resistant materials. Manufactured in Sweden and programmed in the USA with the highest security standards.

How should you choose where secrets come from?

Pick a source based on where the application runs, who needs access, and how much centralized control you need. No option removes the need to protect values at runtime or respond quickly to exposure.

Approach Best fit Considerations
Environment variables or an ignored local file Individual development and simple local setups Keep local files untracked; do not share real values through source control. Configure deployment values separately.
CI/CD or repository secret store Automated builds and deployment workflows Limit which workflows and people can access each value, and rotate credentials when needed. GitHub documents its repository “Secrets and variables” store as one example: GitHub’s leaked-secret remediation guidance.
Dedicated secret manager Services or teams that need centralized provisioning, access policy, auditing, and rotation It adds operational setup and does not eliminate the need for access controls, careful runtime handling, or incident response. OWASP describes these as core secret-management functions: OWASP Secrets Management Cheat Sheet.

How do you keep local secret files out of Git?

Git ignore rules keep matching untracked paths out of ordinary add operations. Put the rule in the repository’s .gitignore before adding the file:

Rank #2
Yubico - YubiKey 5C NFC - Multi-Factor authentication (MFA) Security Key and passkey, Connect via USB-C or NFC, FIDO Certified - Protect Your Online Accounts
  • POWERFUL SECURITY KEY: The YubiKey 5C NFC is the most versatile physical passkey, protecting your digital life from phishing attacks. It ensures only you can access your accounts
  • WORKS WITH 1000+ ACCOUNTS: Compatible with popular accounts like Google, Microsoft, and Apple. A single YubiKey 5C NFC secures 100+ of your favorite accounts, including email, password managers, and more
  • FAST & CONVENIENT LOGIN: Plug in your YubiKey 5C NFC via USB and tap it, or tap it against your phone (NFC), to authenticate. No batteries, no internet connection, and no extra fees required
  • MOST SECURE PASSKEY: Supports FIDO2/WebAuthn, FIDO U2F, Yubico OTP, OATH-TOTP/HOTP, Smart card (PIV), and OpenPGP. That means it’s versatile, working almost anywhere you need it
  • PRIMARY & SPARE KEYS: Just like having a spare house key, we recommend buying two YubiKeys - one for daily use and one as a spare. That way you’ll never get locked out of your accounts
# .gitignore
.env
.env.*
!.env.example

These rules ignore .env and other files whose names begin with .env., while allowing the shared .env.example template. Adjust the pattern if your project uses different filenames; ignore rules can be too broad if projects keep other legitimate files with the same naming pattern.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  1. Create the ignore rule first. Add the local secret filename to .gitignore before creating or adding the file.
  2. Check whether Git already tracks it. An ignore rule does not affect a file already in the index. Use git ls-files -- .env to check whether that exact path is tracked.
  3. If tracked, untrack it without deleting your local copy. Run git rm --cached -- .env, then commit the removal and the ignore rule. Substitute the actual path if your secret file has a different name.
  4. Review the staged changes. Run git diff --cached and check for credentials before committing. Also inspect new files and configuration changes rather than assuming every sensitive file matches an ignore pattern.

Removing a file from the index only changes what the next commit tracks; it does not erase earlier commits or make an exposed credential safe.

Rank #3
Yubico - YubiKey 5 NFC - Multi-Factor authentication (MFA) Security Key and passkey, Connect via USB-A or NFC, FIDO Certified - Protect Your Online Accounts
  • POWERFUL SECURITY KEY: The YubiKey 5 NFC is the most versatile physical passkey, protecting your digital life from phishing attacks. It ensures only you can access your accounts
  • WORKS WITH 1000+ ACCOUNTS: Compatible with popular accounts like Google, Microsoft, and Apple. A single YubiKey 5 NFC secures 100+ of your favorite accounts, including email, password managers, and more
  • FAST & CONVENIENT LOGIN: Plug in your YubiKey 5 NFC via USB and tap it, or tap it against your phone (NFC), to authenticate. No batteries, no internet connection, and no extra fees required
  • MOST SECURE PASSKEY: Supports FIDO2/WebAuthn, FIDO U2F, Yubico OTP, OATH-TOTP/HOTP, Smart card (PIV), and OpenPGP. That means it’s versatile, working almost anywhere you need it
  • PRIMARY & SPARE KEYS: Just like having a spare house key, we recommend buying two YubiKeys - one for daily use and one as a spare. That way you’ll never get locked out of your accounts

What prevention and detection should you add?

Use more than one checkpoint: review changes locally, run a secret scanner where practical, and enable hosting-platform protections when your repository and configuration support them.

  • Review staged changes: inspect git diff --cached before committing, especially after adding configuration, logs, or test fixtures.
  • Use secret scanning: GitHub says secret scanning checks repository Git history across branches for hardcoded credentials. A scan can help find supported patterns, but it should not be treated as proof that every kind of credential is absent. See About secret scanning.
  • Enable push protection where available: GitHub says command-line push protection blocks pushes containing supported detected secrets. Eligibility and setup vary by product and configuration, and detection is not universal. See Push protection from the command line.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What should you do if you committed a secret?

Assume the credential is exposed. Deleting the line or making the repository private does not undo the exposure.

Best Value
Yubico - YubiKey 5C - Multi-Factor authentication (MFA) Security Key and passkey, Connect via USB, FIDO Certified - Protect Your Online Accounts (5C)
  • POWERFUL SECURITY KEY: The YubiKey 5 is a versatile physical passkey that protects your digital life from phishing attacks. It ensures only you can access your accounts.
  • WORKS WITH 1000+ ACCOUNTS: Compatible with popular accounts like Google, Microsoft, and Apple. A single YubiKey 5 secures 100+ of your favorite accounts, including email, password managers, and more.
  • FAST & CONVENIENT LOGIN: Plug in your YubiKey 5 via USB and tap it to authenticate. No batteries, no internet connection, and no extra fees required.
  • MOST SECURE PASSKEY: Supports FIDO2/WebAuthn, FIDO U2F, Yubico OTP, OATH-TOTP/HOTP, Smart card (PIV), and OpenPGP. That means it’s versatile, working almost anywhere you need it.
  • BUILT TO LAST: Made from tough, waterproof, and crush-resistant materials. Manufactured in Sweden and programmed in the USA with the highest security standards.
Rank #4
Yubico - Security Key NFC - Basic Compatibility - Multi-Factor Authentication (MFA) Key, Connect via USB-A or NFC, FIDO Certified
  • POWERFUL SECURITY KEY: The Security Key NFC is the essential physical passkey for protecting your digital life from phishing attacks. It ensures only you can access your accounts.
  • WORKS WITH 1000+ ACCOUNTS: Compatible with Google, Microsoft, and Apple. A single Security Key NFC secures 100 of your favorite accounts, including email, password managers, and more.
  • FAST & CONVENIENT LOGIN: Plug in your Security Key NFC via USB-A and tap it, or tap it against your phone (NFC) to authenticate. No batteries, no internet connection, and no extra fees required.
  • TRUSTED PASSKEY TECHNOLOGY: Uses the latest passkey standards (FIDO2/WebAuthn & FIDO U2F) but does not support One-Time Passwords. For complex needs, check out the YubiKey 5 Series.
  • BUILT TO LAST: Made from tough, waterproof, and crush-resistant materials. Manufactured in Sweden and programmed in the USA with the highest security standards.
  1. Revoke or rotate the credential with its issuer. GitHub’s guidance is explicit: “Real secrets that have been exposed must be revoked to avoid unauthorized access.” Do this promptly; removing the text from a file is not a substitute.
  2. Check for possible use. Review relevant account or service activity logs and determine what systems, data, and permissions the credential could access. Follow the issuer’s incident-response guidance if you find suspicious activity.
  3. Remove the secret from the current version. Replace hardcoded values with runtime configuration, and ensure the corrected version is committed. Do not put the replacement credential in Git.
  4. Decide whether history rewriting is warranted. GitHub notes that history removal can be time-intensive and may be unnecessary once a credential is revoked. Consider the sensitivity of the exposed data and the repository’s circumstances; revocation is the urgent step.
  5. If rewriting history, coordinate the cleanup. Rewriting and force-pushing can disrupt collaborators. GitHub warns that copies may remain in clones, forks, cached views, and pull-request references; a force push alone does not erase those copies. Its guide to removing sensitive data from a repository covers the process and coordination involved.

How do you make the workflow repeatable?

  • Keep credential values out of source and committed configuration; use names such as DATABASE_URL in code.
  • Provide local values through an ignored file or shell environment, and share required variable names in a placeholder template.
  • Supply deployment credentials through the platform’s secret store or a dedicated manager, with access limited to those that need it.
  • Review staged changes and use scanning or push protection as additional safeguards, not guarantees.
  • Rotate exposed credentials promptly, then decide whether coordinated history cleanup is also necessary.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.