Sometimes it’s really frustrating to try to remove a file or a folder in a git working repository, but it appears that our configuration doesn't work whatsoever. Even more, it can lead to us pushing a wrong file to code reviewers, and that’s not professional at all.
So, to fix this problem, I decided to write this easy-to-follow, blog post on how you can ignore a file or a folder using the .gitignore
file.
First thing first, you will need to tell Git not to track this file or folder by removing it from the index.
You can add the -f
option to force the git remove command.
$ git rm --cached <your_file>
The git rm
command, along with the --cached
option, deletes the file from the repository but does not delete the actual file. This means the file remains on your local system and in your working directory as an ignored file.
Once this step above has been done, you can add the file to .gitignore
# The file or directory you want to ignore
/path/to/your/file
A git status
will show that the file is no longer in the repository, and entering the ls
command will show that the file exists on your local file system.
That’s it, your file has been ignored.
I hope you enjoyed reading this, and I'm curious to hear if this tutorial helped you. Please let me know your thoughts or opinions below in the comments section.
You can have more of my content by following me on LinkedIn • GitHub or Medium