DEV Community

Cover image for all you need to know about .gitignore
Kumar Kalyan
Kumar Kalyan

Posted on

all you need to know about .gitignore

Hi everyone I am back with a new a new article of this series. Today I going to explain about .gitignore file and why to use , when to use are how to use.

why & when to use .gitignore?

In my previous article I have explained that git is a tracking software which tracks every single change on a particular repository, but in some cases we don't want git to track any changes to some file or folders in a git repository, and for this we use a .gitignore file. Now let's understand this with an example, suppose you are building an application and in the same git repository you have created a Todo file where you written some tasks and you don't git to tack anything related to this file, or you don't want to commit this file to your codebase hosted at any of the popular providers like GitHub, GitLab, or Bitbucket, so in this case you can use a .gitignore file and add the file path to it , and then git will totally ignore this file or any changes made to it.

how to create and use a .gitignore file

After you are initialized a git repository , simply open your code editor and create a new file and name it to .gitignore or if you are using git bash , navigate to your repository folder and then type touch .gitignore and this will also create a .gitignore file to your repository.

how to add a particular file or folder to `.gitignore

using git bash

navigate to your repository in gitbash

git rm --cached <file>...

(This command will only work after you have added all files )
ignore demo
see now we don't have the todo file in the commit area
For adding a folder for ignoring
see ab flolder is removed from the staging area

You can also generate .gitignore templates form
.gitignore repo

visit ([]
to generate a predefined template for your project

using editor

use # for comments
*.extntion to ignore all the files with this extension like .txt will ignore all the .txt files

use / for directories like in this case if I add ab/to .gitignore this will ignore everything in ab directory

suppose you have ignored all th files with a particluar extension but now you want to commit a file with that extention in that case you can use ! to neglect ignoring that file like !hello.txt this will neglect ignoring the hello.txt file


Now we are done with all you need to know about .gitignore. Feel free to comment out if you want to ask me something, and make sure to hit a ❤️ if you love this article. Stay tuned for next one, Happy Coding :)


Top comments (0)