Make Selenium Easy

Git Tutorial 25 – Important Pattern Formats For the .gitignore file

Introduction

As a part of GIT Tutorials End To End, we will learn about the important pattern formats for the .gitignore file in this post.

Prerequisite posts

I will expect that you are aware of the basic concepts and commands of GIT. But if you are a beginner in GIT then I strongly recommend you to refer GIT Basic Commands and Concepts section on my blog first.

If you are now aware of the .gitignore file or how to create it then you must go through this post – Ignore Files Using the .gitignore

Did you know that I have started a YouTube channel as well and I need your support to make it successful. Please do watch content then comment, like, share, and obviously subscribe.

First of all, we can have multiple .gitignore files in different directories of the same project. Every pattern specified in the .gitignore file will be relative to the location of .gitignore file.

Let’s create a local git repository with .gitignore to practice –

To ignore a specific file

To ignore a specific file you just need to mention the file name in the .gitignore file. I created a file named fileA.txt and the git status command output shows fileA.txt as an untracked file.

Let’s add fileA.txt in the .gitignore file. You can see that fileA.txt is now an ignored file i.e. git will not show it as an untracked file. In fact, git will not bother at all about the file fileA.txt.

Here is an important point. All files with the name fileA.txt will be ignored in the directory. I will create a subdirectory and then create a new file fileA.txt. In this case, both the fileA.txt will be ignored as well. Please note that you need to pass the file name with the extension otherwise git will look for files and directories with the given name.

If you want to ignore a specific file present in a directory then you need to pass the file name with a relative path. To ignore fileA.txt present in the root directory, pass it in the .gitignore file as “/fileA.txt“. To ignore fileA.txt present in the myDir, pass it in the .gitignore file as “myDir/fileA.txt” or “/myDir/fileA.txt“.

Please note that names are case insensitive i.e. FileA.txt and filea.txt are the same for the .gitignore file.

Using Wildcard characters

If you want to ignore files with a specific extension then you can use an asterisk i.e. “*.html“. It will ignore all files ending with .html without bothering about their names.

If you have a requirement to ignore all but with an exception then you can use negating pattern using an exclamation mark i.e. “!abc.html”.

With both patterns in the .gitignore file, all .html files will be ignored but abc.html will not be ignored.

Please note that an asterisk can be used if you want to ignore a file with a specific name ignoring its extensions. A pattern “dummy.*” will ignore all files whose name is “dummy” ignoring their extensions.

If you want to ignore files whose name contains or starts or ends with a specific word then also you can use an asterisk.

demo*” -> It will ignore all files and directories whose name starts with “demo”.

*demo” -> It will ignore all files and directories whose name ends with “demo”.

*demo*” -> It will ignore all files and directories whose name contains “demo”.

The wildcard character asterisk represents more than one character. To represent only one character we can use the question mark. A pattern as “Dummy?.html” will ignore Dummyy.html but not Dummyyy.html.

To ignore directory

We can also add patterns to ignore directories in the .gitignore. If you want to ignore all files inside a directory including subdirectories then just use “<foldername>/” i.e. “testResults/“. All contents of folder testResults will be ignored by git. Please note here that appending slash with directory name represents that git needs to ignore directory with the given name, not files. If we use a pattern without a slash then git will ignore files and directories with the given name.

There are many other patterns as well which you can refer to in the official documentation here. ABove examples will help you to understand the basics and most used patterns.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe to my YouTube channel.
#ThanksForReading
#HappyLearning

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

Git Tutorial 25 – Important Pattern Formats For the .gitignore file

by Amod Mahajan time to read: 3 min
0
%d bloggers like this: