All How-Tos
Beginner

How to Stop Tracking a File and Keep It Locally in Git

Learn how to stop tracking a file while keeping your local copy.

Stop Tracking a File

Adding a tracked file to .gitignore does not stop Git from tracking it. You also have to remove its entry from the Index.

The following example stops tracking config.local in the repository root. Start with no unrelated changes staged and run the commands from that root.

  • Add an Ignore Rule: Add this line to the root .gitignore file so that future git add commands leave your local copy alone.
/config.local
  • Remove the File from the Index: The --cached option keeps the working file on disk and stages its removal from the next commit.
git rm --cached -- config.local
  • Review the Change: Stage the ignore rule and check that the diff contains only the intended changes.
git add -- .gitignore
git diff --cached
  • Commit the Removal: Your local config.local remains on disk and is now ignored.
git commit -m "Stop tracking local configuration"

Stop Tracking a Directory

For a directory, add a matching ignore rule (for example, /local-config/) and use -r to remove its tracked contents from the Index. Then stage .gitignore and commit as above.

git rm -r --cached -- local-config

Considerations

  • Omitting --cached also removes the working file from disk.
  • If git rm --cached refuses because the staged version differs from both HEAD and your working file, preserve any staged-only changes before unstaging the file and retrying.
  • The commit removes the file from the shared project. When collaborators pull it, Git can remove their previously tracked copy; they should save any configuration they need beforehand.
  • For local configuration, consider committing a template such as config.example that new collaborators can copy to their ignored config.local.
  • Older commits still contain the file.
  • If you only want to undo git add before committing, see Unstage Files.