Sometimes it’s really frustrating to try to remove a file or a folder in a git working repository, but it seems that our configuration doesn't work at all.
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 below in the comments section 💬.
My goal with this blog is mainly to create helpful content for
Java
and Javascript
web devs, if you want to discuss a specific topic related to the programming world, you can reach me on my social platforms. Connect with me here LinkedIn • Twitter • Instagram • GitHub or Medium