How to write different conditions on the .gitignore file?

.gitignore file.

Whether you are a beginner or an experienced developer and are working with git, it is best to include a gitignore file in your root directory. 

So, what is the .gitignore file?

Git ignores a plain text file. Within this file, you will put different conditions for whether you want to add those files and directories when you add and commit to the git. 

So, what sort of files and directories should you ignore? 

First of all, you need to decide which files and directories to add to your git repository. When you are working on a project, certainly you do not want to add log files, API keys, credentials, IDE generated files like java or python class files, system-generated files like dist.DS_Store on macOS, .md files, other secrets of the project to the remote repository. Therefore .gitignore files come in handy at this time. So, you tell the git that you do not track those files when you add or commit. 

Now, here we will see some of the conditions how to write on the .gitignore file.

If you write anything after the # sign .gitignore file will not count as a condition. You can use # sign for comment inside the .gitignore file.

The most straightforward condition is that if you want any files to ignore, write on the .gitignore file name of the file you want to ignore.

e.g. - myTextFile.txt

If you want to avoid the entire directory, then write the condition.

e.g. - logs/
  node_modules/

The above directories will not be included when you add git add . command. You can add as many as the directory you want. 

You can use wildcard conditions in the .gitignore file. This is how you can write the condition

e.g., *.log (with this, I am saying to git that matches any file ending with the name extension .log )

Sometimes you can use the negation sign in the .gitignore files.

e.g. - !myFile.txt  with this, I am telling the git that does not ignore this file. But here is some tricky part. if you write the condition like

e.g. - *.log/
!log/myFile.txt

the above condition, myFile.txt, will be ignored. As you can see before that condition, I have added *.log/ directory, and inside the log directory, myFile.txt exists, so ultimately, myFile.txt will be ignored. The reason behind that the log directory will be ignored at first.
Below the video link, you can see in detail how to work on the .gitignore file.


                


Follow me on Facebook , Twitter &amp Buy me a coffee.

Mohammed J Alom

Hi, Thank you so much for visiting my blog programming with mohammed. First, a little bit of myself, I graduated from MTU Munster Technological University with BSc (Hons) in Software Development. I am currently pursuing MSc in Artificial Intelligence. I am very interested in programming and love to learn new technologies and help and teach others.

1 Comments

Post a Comment
Previous Post Next Post