DevOps | Scripts | Automation

git

What is a .gitignore file?

You need to know some git basics to read further this article. The “.GITIGNORE” (Git Ignore) file in the repository is used to exclude files from being committed and checked into the Git repository.

Sometimes when you write codes that use multiple files, extensions, and directories. Some of them are for your local testing purpose and some files you don’t want to share with users when they use your repo like your secret files.

In that case, instead of skipping commit for individual files every time, you just need to specify your .gitignore file once in your project and every time, you push files to the repo these files/folders will be ignored.

You can create a .gitignore file generally under the root of your project folder when you clone the repository.

Creating a .gitIgnore file is super easy. There are multiple methods to create a .gitIgnore file. Below I haven’t covered all methods but some of them I find easy to create files.

If you are creating a new project/repository on a GitHub then there is an option to create a .gitignore file initially.

You just need to select the programming template and the file will be created automatically. If you want to append or overwrite a file later after your project/repo is created, you can follow the below methods.

If you haven’t created the .gitignore file before, open your cloned repo and under the root folder of your project, create a file with the name .gitignore. This will be the empty file and you need to put all the files and folders you want to skip from being pushed to repo when you commit.

This is not the preferred way because you have to mention each file and folder for that specific programming language that you want to ignore.

Here is the documentation for the .gitignore file format to ignore files.

https://git-scm.com/docs/gitignore

You can generate a .gitnignore file online by just specifying the name of the language. Multiple websites can do this job but I prefer this website.

https://gitignore.io/

For Example, I want to generate a .gitIgnore file for Terraform then type and select Terraform in the search box and click on Create

It will generate the below file.

.gitignore file sample.

Copy the content and add it to your repo’s .gitignore file.

Visual Studio Code is a popular editor that most programmers use and it supports a .gitignore extension. Once you have installed the extension, you can use the command palette (Ctrl + Shift + P) to add a .gitignore file.

You can search and select the language that you want to generate the .gitignore file.

If the file already exists then VS code will give you the option to Append or Overwrite the .gitignore file.
If your project requires multiple scripting languages then you can append all the file extensions and folders added to the file considered to be ignored.

Loading