.gitignore Issue: How to Make Git Forget the Tracked Files

In Git, you can use .gitignore file to prevent newly added files from being tracked in the future. Notice that adding an already-tracked file to .gitignore does not remove it from being tracked! This causes problems if you want to untrack a file that has been tracked previously.

Luckily, Git has a command you can run to fix this problem:

git update-index --skip-worktree <file-name>

Where you can replace <file-name> with the name of the file or directory you want to untrack.

And to undo this action, run:

git update-index --no-skip-worktree <file-name>

I’m sure this quick answer is enough for well-versed Git users. But if you’re not sure what it does, please read along!

This guide teaches you how to make Git ignore previously tracked files. You will see a concrete example that highlights the issue that arises when ignoring a tracked file and how to fix the problem.

Problem with .gitignore.

As a Git user, you know how important it is to not push every project file into the remote repository. For instance, operating system files (such as Thumbs.db or .DS_Store) are OS-specific files that are different for each developer. Furthermore, they don’t affect the project in any way. These types of files should not be tracked to avoid causing excess overhead.

To ignore files from being tracked, you can write a .gitignore file where you can place the names of files, directories, or extensions to be ignored.

However, a .gitignore file only prevents future files from being untracked. It does not remove currently tracked files from being tracked. This might be counterintuitive, as you might believe files that are in .gitignore would automatically be removed from the index.

Example

Let me demonstrate the problem with .gitignore with an example.

I have an exampleProject directory which is under version control. Inside the folder, there are two files, README.md, and test.txt.

Now, let’s create a .gitignore file and add the test.txt file to it and commit the changes.

$ touch .gitignore
$ echo "test.txt" > .gitignore
$ git add .gitignore
$ git commit -m "Untrack test.txt"

With this change, you would suppose that changes to the test.txt file will no longer be tracked, right?

But let’s make a change to the test.txt file.

Now, let’s see what the git status command says:

As you can see, nothing has changed. You still see the changes and you can commit and push them.

$ git add .
$ git commit -m "These changes shouldn't go through"
$ git push

But this command succeeds and the changes are pushed to the remote.

Even though we added the test.txt to the .gitignore file, the changes are still tracked and you can push changes to the file into the remote origin.

This happens because the test.txt file was tracked previously. As you learned, adding a file to the .gitignore file doesn’t remove existing files from being tracked. It only prevents future files from being tracked.

To untrack a previously tracked file that’s now in .gitignore, you need to run the git update-index –skip-worktree <file-name>. In our example, let’s do:

$ git update-index --skip-worktree test.txt

After running this command, let’s make a change to the test.txt file.

Now, let’s run git status.

Great, now the changes to the test.txt file are truly ignored.

Summary

Today you learned how to make Git truly ignore files by forgetting the tracked files now in .gitignore.

To take home, there’s a clear way to accomplish this. First, add a file name to the .gitignore file. Then tell Git to ignore the working tree of that file:

git update-index --skip-worktree <file-name>

This command skips tracking the file (or directory) specified after the –skip-worktree flag.

Thanks for reading. Happy coding!

Scroll to Top