How to stop tracking a file in Git

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


When you're working with Git, you might find yourself needing to stop tracking a file without deleting it from your local directory. This can be necessary for files that contain sensitive data, are too large, or simply don't belong in a repository. Here, we’ll explore how to do this effectively, touching on different methods and considerations.

In Git, "tracking" refers to files that are included in version control. When you stop tracking a file, you essentially tell Git to ignore future changes to that file. However, it’s important to differentiate between stopping the tracking of a file and removing a file from the repository:

  • Stopping tracking: The file remains in your working directory but changes to the file won't be tracked in new commits.
  • Removing from the repository: The file is deleted from the repo but can remain in your local workspace if specified.

The simplest way to stop tracking a new file is to add it to .gitignore. This special file contains patterns that match file names or paths, which Git will then ignore.

  1. Open your terminal.
  2. Navigate to your repository’s root directory.
  3. Open .gitignore in your text editor:
    Terminal
    vim .gitignore
  4. Add the relative path to the file or pattern you want to ignore, then save and close the editor:
    Terminal
    path/to/your/file.log
  5. Commit the changes to .gitignore:
    Terminal
    git add .gitignore
    git commit -m "Update .gitignore to exclude file.log"

Note that .gitignore only prevents untracked files from being added to the repository. If the file is already tracked, additional steps are required.

If a file is already tracked by Git and you want to stop tracking it without deleting it from your local directory, use the git rm --cached command. This removes the file from the index (staging area) while keeping it on your disk.

  1. Run the following command:
    Terminal
    git rm --cached path/to/your/file.log
  2. Commit the change:
    Terminal
    git commit -m "Stop tracking file.log"

This command updates the repository to no longer track the file, but it remains untouched in your working directory.

  • Commit history: The file’s previous history will remain in the repository. If you need to remove all traces of the file from Git history due to sensitive data, see this guide on how to handle sensitive data leaks in Git.
  • Global ignores: For files that should be ignored across all your Git repositories (like system files or IDE configuration), consider using a global .gitignore file:
    Terminal
    git config --global core.excludesfile '~/.gitignore_global'
  • Check ignored files: To see what files are currently ignored in your project, you can use:
    Terminal
    git status --ignored

For further reading on tracking files in Git, see the official Git documentation.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2