- Git Ignore Tracked Files | .gitignore File | Git Rm --cached - Javaexercise

How To Make Git Ignore Tracked Files?

A file can either be tracked, untracked, or ignored. A tracked file is a file that is version controlled and tracked by Git. An untracked file is not version-controlled.

Git does not track the changes of an untracked file. An untracked file was never a part of a commit and was never added to the staging area(or index).

Git pays no attention to ignored files. It behaves as if the files do not exist. To make Git ignore a file, we need to add the file name to the .gitignore file.

However, Git can only ignore untracked files. Sometimes we may forget to add a file name or pattern to the .gitignore file. If Git is already tracking a file, adding the file name to the .gitignore file is not enough.

Let's learn how to make Git ignore a tracked file.

Step 1: Update the .gitignore file

Make sure the .gitignore file is up-to-date. Add all the names and patterns for the files you want Git to ignore. Git cannot ignore a file that is not mentioned in the .gitignore file.

Step 2: Save your Current Work

Next, save all your current work by stashing or committing the changes. Our working tree should be clean to ensure no work is lost. We can either use the Git Stash command to stash away changes. Or we can commit the changes by using the Git Commit command.

git stash push	# stash changes
git commit -m "Commit Message" # or commit changes

Step 3: Removing the File

We will use the Git Rm command with the --cached option to remove the unwanted file from the repository. Pass the file name to this command to remove or delete that fiile. Note that the --cached option will only delete the file from the repository and not from our file system. The Git Rm --cached command will turn the file into an untracked file.

git rm --cached <file-name>

If we want to remove an entire folder, we can use the -r option. This option will recursively remove the files from the folder.

git rm -r --cached  <folder-name>

We can dry run the command if we are unsure of what the Git Rm command will remove. A dry run will output the file names that will be removed. However, it will not remove or delete those files. Use the --dry-run or the -n option to perform a dry run.

git rm --dry-run --cached -r <folder-name>
git rm -n --cached -r <folder-name>

Step 4: Stage and Commit the Changes

Finally, we can stage the removal of the file and commit the changes. Use the Git Add command for staging the changes and the Git Commit command to commit them. Our file is now untracked and also ignored by Git.

git add .
git commit -m "Removed Ignored Files"

Alternative Method

The steps discussed above are long and not very intuitive. However, there is a Git command that can help us ignore tracked files without even using the .gitignore file. The git update-index --assume-unchanged command helps us in doing that. This command will make Git ignore a specific file. It is independent of the .gitignore file and will work even when the file name is not present in .gitignore.

git update-index --assume-unchanged <file-name>

We can undo the effects of the above command and make Git start tracking the changes by using the --no-assume-unchanged option.

git update-index --no-assume-unchanged <file-name>

Another better alternative is to use the --skip-worktree option. This option is designed for modified tracked files that the user doesn't want to commit.

git update-index --skip-worktree <file-name>
git update-index --no-skip-worktree <file-name>	# to start tracking again


We will have several files that we don't want Git to track. These files will be ignored by Git if we add them to the .gitignore file. Sometimes, we may forget to do so, and now, we have a tracked file that we want to ignore.

We can ignore a tracked file by using the Git Rm command with the --cached option. It will remove the file from our repository but the file will continue to exist on the local system. Another alternative is to use the Git update-index command.