basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    .gitignore doesn’t apply to a file already in the repository

    A line in .gitignore hides only new files. A file that’s already committed is still tracked — edits to it show up in git status. Remove it from the index with git rm --cached, and the file stays on disk. But on other copies it gets deleted after a pull.

    What it looks like

    config.env was committed, then added to .gitignore — and git still sees it:

    git status --short
    #  M .gitignore
    #  M config.env

    Stop tracking it without deleting it from disk:

    git rm --cached config.env
    # rm 'config.env'
    git status --short
    #  M .gitignore
    # D  config.env
    git commit -am "config.env out of the repository, kept only locally"

    D in the first column is a deletion in the index. The file is still on disk; ls config.env shows it. For a whole directory: git rm -r --cached node_modules/.

    What happens on other copies

    To git it’s a file deletion. On the server or a second computer, git pull deletes config.env from disk. If it holds working values there, make a copy first, then pull, then put the copy back:

    cp config.env /root/config.env.keep && git pull && mv /root/config.env.keep config.env

    If the file held a password, it stays in history, so change it (A secret in a commit).

    Why a file is hidden when it should be visible

    The opposite problem — a file won’t add, and git add says The following paths are ignored by one of your .gitignore files:

    git check-ignore -v traefik/data/traefik.yml
    # .gitignore:1:traefik/data/*	traefik/data/traefik.yml

    The file, the line and the rule that hid it.