Skip to main content

Command Palette

Search for a command to run...

How to use .gitignore file?

Updated
3 min read
How to use .gitignore file?

I would like to share with you some advices about .gitignore file.

1. Place of .gitignore file

When you are using IDEs (Intellij Idea, VS Code or Eclipse) the file .gitignore it's usually created at the stage of creation the project and placed in the right directory.

Please be aware that .gitignore file should be included in the main directory of repository. If you will accidentially put it lower, in subcatalog, then only files from that subdirectory would be ignored by GIT. To sum up, to quickly create the .gitignore file, you can achieve by linux command

touch .gitignore

in the correct directory of course.

2. What files should be ignored by GIT

In principle it should be all files automatically generated by the IDE(or project creation). So for example if your project is the source code which will be compiled afterwards, ignore all compiled results. Consider to ignore also temporary files/directories, log, huge libraries (like node_modules). Please think about ignoring all private files which contains your own values (with environment settings). You can also ignore readme files, instructions etc. it depends on your own needs.

3. How to do it?

Just write the list of filenames and directory names to be ignored into the .gitignore file using patterns from table below. In the pattern examples I have used relative paths. If you want to exclude one file from the pattern you can use exception mode, using "!" before the filename or catalog name.

The order of rows in this file is very important, especially if you want to write an exception. Always write the exception after the rule! For example

#Ignores all *.md files
*.md

#Exception is Readme.md in catalog "important"
!important/Readme.md
PatternIgnoresExample
config.txt config.txt in all catalogsconfig.txt also local/config.txt
build/Each catalog build with all contained files, but without a file called buildbuild/target.bin build/output.exe but NOT output/build
buildEach catalog called build with all contained files, also all files called buildbuild/target.bin and output/build
*.exeall files with .exe extensionstarget.exe output/res.exe
bin/*.exeall files with .exe extensions in catalog /binbin/output.exe
temp*All files with prefixes temp*...Temp temp.bin temp_output.exe
**/configs each catalog called configsconfigs/prod.py local/configs/preprod.py
**/configs/local.py Each file called local.py in each catalog called configsconfigs/local.py and server/configs/local.py but NOT configs/fr/local.py
output/**/result.exeEvery file called result.exe in each subcatalog where parent catalog is called outputoutput/result.exe output/latest/result.exe output/1991/12/16/result.exe

Regards, Wojciech

I heartily recommend book of Mariot Tsitoara Beginning Git and Github

Some of informations has been taken from this book https://www.goodreads.com/book/show/51649022-beginning-git-and-github?from_search=true&from_srp=true&qid=t8xR4TKrMJ&rank=1