# How to stop tracking a file in Git

Kenny DuMez

Graphite software engineer

---

**Note**  
This guide explains this concept in vanilla Git. For Graphite documentation, see our [CLI docs](/content/docs/command-reference?utm_source=guidesCallout/index.html).

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.

### Understanding the basics of tracking and untracking files  
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.

### Using .gitignore to exclude files  
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:

```bash
   vim .gitignore
   ```

4. Add the relative path to the file or pattern you want to ignore, then save and close the editor:

```bash
   path/to/your/file.log
   ```

5. Commit the changes to `.gitignore`:

```bash
   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.

### Removing a tracked file from the repository but keeping it locally  
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:

```bash
   git rm --cached path/to/your/file.log
   ```

2. Commit the change:

```bash
   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.

### Additional considerations  
- **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](/content/guides/how-to-delete-sensitive-data-from-git/index.html).
- **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:

```bash
   git config --global core.excludesfile '~/.gitignore_global'
   ```
- **Check ignored files:** To see what files are currently ignored in your project, you can use:

```bash
   git status --ignored
   ```

For further reading on tracking files in Git, see the [official Git documentation](https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository).
