How to make Git “forget” a file that was tracked now it is in .gitignore?

· · 3178 views

In my git repository, there is a file that was being tracked by git. However, now the file is on the .gitignore list.

That file keeps appearing in git status after that file is modified. So, How to force git to forget that file and do not track it now?

0
3 Answers

.gitignore will not allow untracked files from being added (without an add -f) to files tracked by git. However, git will continue to track any files that are already being tracked.

To quit tracking a file you have to remove it from the index. This can be accomplished with this command.

git rm --cached <file>

If you need to remove an entire folder, you have to remove all files in it recursively.

git rm -r --cached <folder>

The removal of the file from the head revision will happen on the next commit.

WARNING: While this won't remove the actual file from your local, it will remove the files from other developer's machines on the next git pull.

0

First, remove all of the items from the Git Index (not from the working directory or local repo), and afterward refreshes the Git Index, while respecting git ignores.

git rm -r --cached . 
git add .
git commit -am "Remove ignored files"

Or one-liner:

git rm -r --cached . && git add . && git commit -am "Remove ignored files"
0

For that, you should update the git index, and git update-index does that job.

You should use --skip-worktree which is for modified tracked files that the user doesn't want to commit anymore and keep --assume-unchanged for performance to prevent git to check the status of big tracked files.

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

# or
  
git update-index --assume-unchanged <file>
0

Please login or create new account to participate in this conversation.