>_ IzzyLab

Hands-on task

This task is step 8 of the course “Everyday Git”

Don't commit secrets

medium~7 min

A .env file with passwords was accidentally committed into the /root/project repository. Remove it from version control (keep the file on disk!) and add a .env line to .gitignore so it's never committed again. Make a commit with these changes.

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

Topic breakdown

Git separates two states of a file: on disk (the working tree) and under version control (the index). .gitignore only affects files that are NOT yet tracked, so for an already committed file a single ignore entry isn't enough — git will keep tracking it. First the file is removed from the index while keeping it on disk (for this git rm has a flag that cancels deletion from disk), and only then is it added to .gitignore and the changes committed.

What's checked

  1. .env removed from version control
  2. .env file remains on disk
  3. removal of .env and .gitignore are committed
  4. git ignores .env

What it looks like