How to Remove Sensitive Data From a Git History (2024)

You’re not alone if you’re concerned about accidentally exposing sensitive files. Nowadays, sensitive data may include encryption keys, deployment config files, SSH keys, API keys, authorization tokens, and connection strings. These secrets are considered compromised if they are pushed to a remote git repository, even a private one.

Note that an accidentally leaked Split Admin API key or SDK key can be instantly revoked in the Split Web Console, in Admin settings under API keys. An Admin can also easily create a replacement key from the same Split Web Console page.

If you have a GitHub repository containing credentials that cannot be revoked, you can contact GitHub Support to permanently remove cached views of your GitHub-hosted repository and references to sensitive data in pull requests on github.com.

Additionally, you can fully purge sensitive files or secrets from your entire Git repository commit history using the following tools:

  • git-filter-repo
  • BFG Repo-Cleaner

To prepare to remove sensitive strings from your Git history using one of these tools, you can first create a text file, that we’ll name replacements.txt. Each line in the file should be a sensitive string to be removed, optionally followed by an arrow (==>) and the replacement text, as shown below.

password001 cigfkkdmgnl6jrfmbkqd0luaho54l9bbs==>process.env.SDK_KEY [email protected]==>[email protected] password002==>[PASSWORD]

You will need to rewrite all the Git commits that contain sensitive data in your repository history and prune empty commits that result. This process of removing your secrets creates new commits with new SHA-1 ids. Two methods that accomplish this are described below.

The git-filter-repo tool can remove sensitive information and large files (blobs) from your entire Git repository history, not just your last commit. It is a very flexible, open source tool hosted on GitHub and the recommended replacement for git-filter-branch.

git-filter-repo is a single file that you can download and place on your $PATH and that requires Python to be installed on your system. There are also package managers, like pip, that can install it.

You can examine the exhaustive user manual page for git-filter-repo, but for our purposes here are the key steps:

  1. Start with a local clone of your Git repository and create a backup, so that you don’t lose your secrets and some settings (remotes) in the .git/config file.
  2. Checkout all your branches.
  3. Remove sensitive data in your entire Git repository history with the following command:
git filter-repo --replace-text replacements.txt --replace-refs delete-no-add

You can also remove a file with sensitive data from your commit history with the command:

git filter-repo --invert-paths --path <path-to-sensitive-file> --replace-refs delete-no-add

The —replace-refs delete-no-add directive tells Git not to create replace-references for the deleted commits.

  1. Force-push to your remote repo using the --all flag to update all your branches, and again using the --tags flag to update all your tagged releases.

These git-filter-repo steps can also be accomplished using BFG Repo-Cleaner.

Method Two: Using the BFG Repo-Cleaner tool

BFG Repo-Cleaner is also an open source tool hosted on GitHub. It is simpler, faster alternative to git filter-repo (or git filter-branch) for removing large files and sensitive data from Git repositories.

The BFG Repo-Cleaner is bundled as a downloadable jar file and uses the Java runtime environment to run on the command line.

The full instructions are on the main documentation page, but quick overview of the usage is below.

  1. Clean up sensitive data from your source code and commit your changes to ensure that your latest commit is clean.
  2. Clone a bare Git repository using the git clone --mirror command and create a backup. The --mirror flag instructs Git to pull all the repository’s references.
  3. Remove sensitive data in your entire Git repository history with the following command:
java -jar bfg.jar --replace-text replacements.txt repo.git

You can also purge sensitive files from your commit history with the command:

java -jar bfg.jar --delete-files <filename> repo.git
  1. Push your changes to your remote repository, using git push, to update your commits and references.

Following this clean-up of the remote Git repository, your teammates can use the git pull command to pull the clean commits to their local repositories and the git pull --tags command to locally update their tagged commits. Instead of merging, your colleagues should git rebase their working branches.

Follow-Up Steps in Git

Git references the deleted commits in the reflog and retains them in the database as dangling commits for a time. These can be removed manually using the following commands:

git reflog expire --expire=now --all git gc --prune=now --aggressive

The first command deletes the reflog of your local repository, the history of your Git repository’s HEAD pointer. The second command cleans up the dangling commits in your Git repository database.

Generally, it is best practice to add sensitive file paths to a .gitignore file at the root of your Git repository. This will prevent the sensitive files from being added to the Git index, the mechanism by which Git tracks file changes. Files matching .gitignore entries will not be staged by the git add command without the --force flag. Additionally, a file can be removed from the Git index using the git rm --cached command, which operates on the current branch. This requires a follow-up git commit, and the index is updated from that commit forward.

Want to Learn More?

For additional education and relevant content, be sure to check out the following articles:

  • 4 Considerations When Integrating Systems Using APIs
  • A Guide to APIs: REST, SOAP, GraphQL, and gRPC
  • Kubernetes Labels: Best Practices

Switch It On With Split

The Split Feature Data Platform™ gives you the confidence to move fast without breaking things. Set up feature flags and safely deploy to production, controlling who sees which features and when. Connect every flag to contextual data, so you can know if your features are making things better or worse and act without hesitation. Effortlessly conduct feature experiments like A/B tests without slowing down. Whether you’re looking to increase your releases, to decrease your MTTR, or to ignite your dev team without burning them out–Split is both a feature management platform and partnership to revolutionize the way the work gets done.Schedule a demoto learn more.

Get Split Certified

Split Arcadeincludes product explainer videos, clickable product tutorials, manipulatable code examples, and interactive challenges.

How to Remove Sensitive Data From a Git History (2024)

FAQs

How to Remove Sensitive Data From a Git History? ›

You can purge a file from your repository's history using either the git filter-repo tool or the BFG Repo-Cleaner open source tool. If sensitive data is located in a file that's identified as a binary file, you'll need to remove the file from the history, as you can't modify it to remove or replace the data.

How to remove sensitive data from git commit history? ›

Method One: Using the git-filter-repo tool

The git-filter-repo tool can remove sensitive information and large files (blobs) from your entire Git repository history, not just your last commit. It is a very flexible, open source tool hosted on GitHub and the recommended replacement for git-filter-branch.

How do you remove content from git history? ›

The git rm command allows you to remove a file from the working directory and the index. Using git rm --cached --ignore-unmatch <path> deletes the file from the index without removing it from the working directory. This allows you to remove a file from history without deleting it from your local machine.

How do I remove changes from git history? ›

Choose an option and undo your changes:
  1. To overwrite local changes: git checkout -- <file>
  2. To save local changes so you can re-use them later: git stash.
  3. To discard local changes to all files, permanently: git reset --hard.

How to remove sensitive data from GitLab? ›

To sanitize a gitlab. rb file, review and redact secrets manually, or use a command line utility to automate this process, for example our gitlab. rb sanitizer tool. You can also scrub sensitive data using the grep command line utility.

How do I remove sensitive content from GitHub? ›

You can purge a file from your repository's history using either the git filter-repo tool or the BFG Repo-Cleaner open source tool. If sensitive data is located in a file that's identified as a binary file, you'll need to remove the file from the history, as you can't modify it to remove or replace the data.

How to remove commit data in git? ›

To remove all commits from a specific branch:
  1. Checkout the branch: git checkout the-branch.
  2. Delete all commits: git reset --hard $(git commit-tree HEAD^{tree} -m "Initial commit")
  3. Force push if needed: git push origin the-branch --force.

How do you edit history in git? ›

There are many ways to rewrite history with git. Use git commit --amend to change your latest log message. Use git commit --amend to make modifications to the most recent commit. Use git rebase to combine commits and modify history of a branch.

How to remove commit from git history locally? ›

Ensure that you are on the branch from which you want to remove the commits. git reset HEAD~1 will reset the branch to the previous commit. 5. After running the command, Git will move the branch pointer to the specified commit, effectively removing any commits made after that point.

How do I remove GitHub commit history? ›

Once you've identified the commits that you want to delete, you can use the git rebase command to remove them. This will open an interactive rebase window, where you can select the commits that you want to delete. To do this, simply delete the lines that correspond to the commits that you want to remove.

How to clean commit history in git? ›

How to Delete Commit History – A Step-by-Step Guide
  1. Step 1: Check out to a temporary branch. ...
  2. Step 2: Add all files. ...
  3. Step 3: Commit the changes to the commit history. ...
  4. Step 4: Delete the main branch. ...
  5. Step 5: Rename the temporary branch to main. ...
  6. Step 6: Force update to our Git repository.
Mar 9, 2023

How do I remove big files from git history? ›

Using git filter-branch

This is the most commonly used method, and it helps us rewrite the history of committed branches. Here, the rm option removes the file from the tree. Additionally, the -f option prevents the command from failing if the file is absent from other committed directories in our project.

How do I remove all recent changes from git? ›

Discarding All Local Changes

If you want to undo all of your current changes, you can use the git restore command with the "." parameter (instead of specifying a file path): $ git restore . Again: please be careful with these commands! Once you've discarded your local changes, you won't be able to get them back!

How do I Delete sensitive data? ›

When you need to dispose of sensitive digital data and equipment, you can either delete the data from hard drives or computers, for example, and in this way recycle expensive technical equipment, or you can destroy the entire device containing the information.

How do I remove data from git? ›

The git rm command can be used to remove individual files or a collection of files. The primary function of git rm is to remove tracked files from the Git index. Additionally, git rm can be used to remove files from both the staging index and the working directory.

How do I remove blobs from git history? ›

Removing Large Files from Git History with BFG
  1. Step 1: Install the BFG cli tool. ...
  2. Step 2: Clone your repo as a mirror. ...
  3. Step 3: Back up your repo. ...
  4. Step 4: Run BFG to remove large blobs. ...
  5. Option 1: Strip blobs bigger than a specified size. ...
  6. Option 2: Strip the biggest blobs, limited to a specified number.
Mar 14, 2019

How to remove commit information in git? ›

To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits. You can increase the number to remove even more commits.

How do I remove unnecessary files from a git commit? ›

How to Remove a File from a Git Commit: A Step-by-Step Guide
  1. git reset HEAD~1.
  2. git rm --cached <file-name>
  3. git commit -m "Remove <file-name> from repository"
Feb 15, 2023

How to erase GitHub commit history? ›

Once you've identified the commits that you want to delete, you can use the git rebase command to remove them. This will open an interactive rebase window, where you can select the commits that you want to delete. To do this, simply delete the lines that correspond to the commits that you want to remove.

Top Articles
Mutual Fund Portfolio: How to Choose for Long-Term Success, Income Generation & Diversification
Wrapped LUNA Classic (WLUNC) Price Prediction 2024, 2025–2030 | CoinCodex
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Pearson Correlation Coefficient
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Greg Kuvalis

Last Updated:

Views: 5526

Rating: 4.4 / 5 (75 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Greg Kuvalis

Birthday: 1996-12-20

Address: 53157 Trantow Inlet, Townemouth, FL 92564-0267

Phone: +68218650356656

Job: IT Representative

Hobby: Knitting, Amateur radio, Skiing, Running, Mountain biking, Slacklining, Electronics

Introduction: My name is Greg Kuvalis, I am a witty, spotless, beautiful, charming, delightful, thankful, beautiful person who loves writing and wants to share my knowledge and understanding with you.