/github-for-non-tech

How to hide sensitive files from GitHub?

Step-by-step guide to hide sensitive files from GitHub. Learn how to update your .gitignore, remove tracked files, secure secrets, and protect your repository.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to hide sensitive files from GitHub?

 
Step 1: Initialize your local Git repository
 

First, ensure you have Git installed on your machine. Open your terminal or command prompt and navigate to your project directory. If your project isn’t under version control yet, initialize it:


git init

This command creates a hidden .git folder that Git uses to track changes.

 
Step 2: Create or update your .gitignore file
 

A .gitignore file tells Git which files or directories to ignore. In your project root, create (or open) .gitignore:


touch .gitignore

Edit .gitignore and add patterns for files you want to hide. For example, to ignore environment files and logs:


# Environment variables
.env

# Log files
logs/
\*.log

# Build artifacts
dist/
build/

 
Step 3: Verify .gitignore patterns
 

Make sure your patterns match the actual file paths. You can test whether Git will ignore a file using:


git check-ignore -v path/to/your/file

If the file appears in the output, it’s correctly ignored. Otherwise, adjust your .gitignore entries.

 
Step 4: Remove already-tracked sensitive files from Git history
 

If you’ve already committed sensitive files, Git will continue tracking them despite your .gitignore. To untrack them without deleting locally:


git rm --cached path/to/sensitive.file
git commit -m "Remove sensitive file from tracking"

Use wildcards if you need to untrack multiple files:


git rm --cached \*.key
git commit -m "Stop tracking all .key files"

 
Step 5: Rewrite Git history for thorough removal (optional)
 

To scrub sensitive data from previous commits, use the BFG Repo-Cleaner or git filter-branch. Example with BFG:


# Download BFG jar, then:
java -jar bfg.jar --delete-files YOUR_SECRET_FILE
git reflog expire --expire=now --all && git gc --prune=now --aggressive
git push --force

Always backup your repo before rewriting history. Force-push only if you understand implications.

 
Step 6: Commit your changes and push to GitHub
 

After updating .gitignore and removing tracked files, commit and push:


git add .gitignore
git commit -m "Add .gitignore and remove sensitive files from tracking"
git push origin main

Replace main with your branch name if different.

 
Step 7: Use GitHub Secrets for sensitive environment variables
 

Instead of committing .env or API keys, store them securely in GitHub:

  • Go to your repository on GitHub.
  • Click Settings > Secrets and variables > Actions.
  • Click New repository secret, give it a name (e.g., API\_KEY) and paste the value.

In your GitHub Actions workflow, access it using:


env:
  API_KEY: ${{ secrets.API_KEY }}

 
Step 8: Encrypt files with git-crypt (advanced)
 

If you need to keep encrypted files in the repo, git-crypt can transparently encrypt/decrypt based on GPG keys:


# Install git-crypt
brew install git-crypt      # macOS
sudo apt-get install git-crypt  # Ubuntu

# Initialize and grant access
git-crypt init
git-crypt add-gpg-user --trusted USER\_ID

Then add patterns to .gitattributes:


secrets/\*.key filter=git-crypt diff=git-crypt

 
Step 9: Regularly audit your repository
 

  • Run git status to verify no sensitive files are staged.
  • Use tools like truffleHog or git-secrets to scan for passwords or keys:

# Install git-secrets
brew install git-secrets

# Set up in your repo
git secrets --install
git secrets --register-aws
git secrets --scan

 
Step 10: Educate your team and automate checks
 

  • Document your .gitignore conventions in your project README.
  • Add pre-commit hooks (e.g., husky or pre-commit) to prevent committing secrets.
  • Integrate CI checks to fail builds when secrets are detected.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022