Ignoring the .gitignore file

Ignoring the .gitignore file

Today I have a small disagreement with GIT and gitignore rules. When you add a file to a GIT repository, it is tracked by GIT. Then adding it to .gitignore won’t make GIT forget about that file.

In this post, I will explain how to stop GIT from tracking a file that is already tracked. You can use this cool command for this situation.

git rm --cached the_file_that_should_be_not_tracked.txt

If you modify the file again and run git status, you won’t see the file in the GIT repository. The problem with this command is that this file won’t be a part of the GIT repository anymore. This means it will be deleted on the remote when you push the repository.

Sometimes you want to push a file to the remote and forget it. You don’t need to edit it anymore. If you want GIT to assume the file is unchanged, You can use the command below:

git update-index --assume-unchanged the_no_edit_file.txt

git update-index wants the file names as an argument, so if you need to apply it to a directory, you may use the command below.

cd the_no_edit_directory
git ls-files -z | xargs -0 git update-index --assume-unchanged

That is exactly what we desire. Edits to this file are ignored by GIT, and the file is still on the remote repository.

References

  1. https://git-scm.com/docs/git-rm

  2. https://git-scm.com/docs/git-update-index

Did you find this article valuable?

Support Erdal TAŞKESEN by becoming a sponsor. Any amount is appreciated!