Git basics

Stop tracking a file that's now in .gitignore

Adding a file to .gitignore doesn't untrack it if Git already knew it. Remove it from Git but keep it on disk.

Fill these in once
  1. Stop tracking it (the file stays on your disk)
    Terminal
    git rm --cached .env

    For a whole folder, use git rm -r --cached folder-name. If the file held secrets and was already pushed, change those secrets too.

  2. Make sure it's in .gitignore
    Terminal
    echo ".env" >> .gitignore
  3. Commit the change
    Terminal
    git commit -m "Stop tracking .env"

Check it worked

  1. Check Git now ignores it
    Terminal
    git status
  1. Stop tracking it (the file stays on your disk)
    Terminal · Ubuntu / Debian
    git rm --cached .env

    For a whole folder, use git rm -r --cached folder-name. If the file held secrets and was already pushed, change those secrets too.

  2. Make sure it's in .gitignore
    Terminal · Ubuntu / Debian
    echo ".env" >> .gitignore
  3. Commit the change
    Terminal · Ubuntu / Debian
    git commit -m "Stop tracking .env"

Check it worked

  1. Check Git now ignores it
    Terminal · Ubuntu / Debian
    git status
  1. Stop tracking it (the file stays on your disk)
    PowerShell
    git rm --cached .env

    For a whole folder, use git rm -r --cached folder-name. If the file held secrets and was already pushed, change those secrets too.

  2. Make sure it's in .gitignore
    PowerShell
    Add-Content .gitignore ".env"
  3. Commit the change
    PowerShell
    git commit -m "Stop tracking .env"

Check it worked

  1. Check Git now ignores it
    PowerShell
    git status