How Do I Make Git Forget about a File That Was Tracked, but Is Now in .gitignore?

Better Stack Team
Updated on June 21, 2024

If you have a file that was previously tracked by Git but is now listed in .gitignore, you need to remove it from the Git index to stop tracking it. Here's how you can do it:

Step 1: Remove the File from the Index

You can use the git rm command with the --cached option to remove the file from the Git index without deleting it from your file system:

 
git rm --cached <file>

Replace <file> with the path to the file you want to stop tracking.

Step 2: Commit the Changes

After removing the file from the index, you need to commit the changes to reflect the removal of the file from Git's tracking:

 
git commit -m "Stop tracking <file>"

Step 3: Verify the Changes

You can verify that the file is no longer being tracked by checking the status:

 
git status

The file should no longer appear in the list of tracked files.

Note:

  • After performing these steps, Git will no longer track changes to the specified file, even if it's listed in .gitignore.
  • Make sure you commit the changes to reflect the removal of the file from tracking. If you don't commit the changes, Git will still consider the file as tracked.
  • Be cautious when using git rm --cached, as it only removes the file from the index and does not delete it from your file system.
Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github