.gitignore
it controls which files Git should ignore locally in that repository

Use this when files should stay on your machine but should no longer be tracked by Git.

Example case:

  • files or folders matching h-* were accidentally committed and pushed

Core idea

.gitignore only prevents Git from tracking files in the future.

If a file is already tracked, adding it to .gitignore is not enough.
You must also remove it from Git’s index using git rm --cached.

Example pattern

Add this to .gitignore:

**/h-*

This pattern matches names like:

  • h-temp
  • notes/h-draft.md
  • src/cache/h-build

Steps

1. Add the ignore rule

**/h-*

2. Remove already tracked files from Git’s index

This does not delete your local files. It only stops Git from tracking them.

git rm -r --cached -- '**/h-*'

3. Commit and push the change

git commit -m "Stop tracking h-* files"
git push

Why this works

  • .gitignore tells Git what it should ignore going forward
  • git rm --cached removes matching files from the tracked set
  • after commit + push, those files disappear from the latest repository state but remain on your machine

Notes / Gotchas

  • .gitignore only affects untracked files. It does not remove files already committed.
  • This only changes the latest visible state of the repository.
  • The files may still exist in Git history.
  • If those files contain secrets such as API keys or tokens, you must:
    • rewrite Git history
    • rotate the exposed secrets

Check whether Git is still tracking matching files:

git ls-files | rg '(^|/)h-[^/]*$'

Preview ignored files:

git status --ignored