>_ IzzyLab

Hands-on task

This task is step 7 of the course “Linux server security”

Remove a secret from version control

medium~7 min

In the repository /root/repo a secrets file secrets.env was committed by mistake. Take it out of version control without deleting the file itself from disk: stop tracking secrets.env (remove it from the index and record that with a commit) and add the line secrets.env to the .gitignore file so it no longer ends up in commits. Result: secrets.env stays on disk, but it's no longer in HEAD, and .gitignore contains secrets.env.

What you'll practise:
  • git rm --cached
  • index
  • .gitignore
  • stop tracking
  • secrets

Topic breakdown

There's a difference between "a file on disk", "a file in the index", and "a file in a commit": taking a secret out of version control means clearing it from the index while leaving it in the working tree. For this, git rm has the --cached flag, which stops tracking without deleting the file; the change is then recorded with a commit. To keep the file from coming back in future commits, its name is added to .gitignore. Remember: this doesn't erase the secret from old history.

What's checked

  1. secrets.env on disk
  2. secrets.env removed from HEAD
  3. .gitignore contains secrets.env

What it looks like