Have you been using GitHub but still wondering what a .gitignore file means?

If yes, this guide will show you what it is, its constituents, what it does, and how to create a Gitignore.

What Is a Gitignore?

GitHub offers various detailed features, ranging from staging files to pushing them to your remote repository. However, if you don't feel like pushing some files to GitHub, you can choose not to as well.

Leaving some files out like this while staging others for a commit is the ultimate goal of the .gitignore file.

In essence, the .gitignore file contains the name of all the files and folders you don't intend to push to your remote repository.

In addition to being helpful while committing files to GitHub, using a .gitignore can also come in handy while deploying to cloud platforms like Heroku.

A .gitignore is like any other file you use while working on your project. However, be careful not to confuse it with a plain text file that uses an appended .txt. So note that .gitignore is the only naming convention for this file.

How to Make a Gitignore File

Window for saving .gitignore file

To make a .gitignore file, go to your project root folder and create a new file. Name it .gitignore.

Alternatively, you can open any code editor of your choice. Then make a new file in your project root directory and name it .gitignore.

Open the .gitignore file and type the names of files and folders you wish to ignore for commit. Save it like you would any other file on your machine. But ensure that you use the appropriate file extension for each file.

To ignore multiple items at a time, once you type the name of a file or folder on a line, hit Enter and write the next one on a new line.

Sublime text with .gitignore file opened

Once you add a file or folder to .gitignore, GitHub will not pick or stage them for the next commit.

Files or folders listed in .gitignore won't be in your remote repository on GitHub. However, this .gitignore file itself gets pushed to your remote repository.

Once you log on to your remote repository, you can then click .gitignore to see a list of folders and files you left behind while staging others for commit.

Having a glance at .gitignore in your remote repository this way is helpful when you need to go back to your local machine to effect changes in those files.

That's pretty handy, as you can quickly grab the names of such files and remove them locally from .gitignore if you need to. Then update them as you like on your local machine.

In addition to that, it lets you avoid tampering with completed files locally but helps you focus on the ones listed in .gitignore.

However, if you ever decide to exempt a file or a directory from .gitignore before updating or completing them locally, remove them by deleting them from .gitignore. But be sure not to delete the file itself.

After all, you can then run git add --all again to stage them for commit to your remote repository.

What Type of Files Should You Put in Gitignore?

Temporarily used files and folders that are irrelevant to your project are examples of files you can place in .gitignore. If you also have uncompleted files like additional JavaScript or modules, they can go into .gitignore. As we stated earlier, once you decide to update and stage such files, you can remove them from .gitignore anytime.

Using a .gitignore every time isn't a prerequisite for a great project. But it simplifies your workflow. If you plan to use cloud hosting, it can make deployment seamless as well.

Now that you know how to exempt some files and folders from staging with .gitignore, you can also look at how you can clean Git and have a neat working tree.