How to ignore a file on git after created repository ?

Steps:

  • Create a .gitignore file on the root
  • To ignore a directory: name_of_directory/
  • To ignore a file: name_of_file

We don't need to provide the complete path of the file or directory to be ignored, we just have to provide its name.

If you want to ignore all files with same extention, for example: *.pyc
(this will ignore all files with pyc extention)

Also the above tips will only work at the first time when you have not added the files to the git. But if by mistake you added a file to the git and now you want it to be ignored for the rest of the commits, you need to first clear the git cache regarding that file by running this command;

git rm -r --cached <path_of_the_file>