Open In App
Related Articles

What is Git-Ignore and How to Use it?

Like Article
Save Article
Report issue

There are various types of files we might want the git to ignore before committing, for example, the files that are to do with our user settings or any utility setting, private files like passwords and API keys. These files are not of any use to anyone else and we do not want to clutter our git. We can do this with the help of “


.gitignore file is used in a git repository to ignore the files and directories which are unnecessary to project this will be ignored by the git once the changes as been committed to the Remote repository. The type of files which will gets ignored are the mainly temporary files and the files which should not be versioned.

gitignore File (.gitignore)

When the git commits are pushed to to the remote repository to want to ignore some files then we will use .gitignore file is a text file that specifies which files and directories Git should ignore. Which will reduce the size of the repository.

Creating .gitignore file (.gitignore)

Step 1:Open your terminal/cmd and change your directory to the folder where your files are located. You can use ” ls -a” command to view its contents.

cd directory(or)folder
ls -a

Here, the project files are saved inside folder named story which is further inside web development. Here, we want git to ignore the secrets.txt file.


Step 2: Create .gitignore File inside the project folder.

Step 3: Write the name of the files you want to ignore in the .gitignore text file. Each file name should be written in a new line .


Step 4: Initialize git in your terminal. Add these files to your git repository and commit all the changes with an appropriate message.

git init
git add .
git commit -m "your message"

Step 5: Check the status of the git repository. The file(s) added to the .gitignore text file will be ignored by the git every time you make any changes and commit.

git status



# Compiled class file

# Log file

# Mobile Tools for Java (J2ME)

# Package Files







gitignore file Patterns and Format (.gitignore)

  • Blank Line: A blank line doesn’t refer to any file name, so we can use it to separate two file names for the ease of reading .
  • #: A line beginning with the # symbol refers to a comment .However if # is used as a pattern then use backslash (“\”) before the # symbol so that it is not misunderstood as a comment.
  • /: It is used as a directory separator i.e to include directories,  for example webdev/ .
  • *.extension_name: For example *.txt and *.log can be used to match ALL the files that have .txt and .log as their extension respectively.
  • **/any_name: It is used to match any file or directory with the name any_name.
  • any_name/**: It is used to match anything that is inside the directory of the name any_name. for example webdev/** matches all the files inside webdev directory.

gitignore Rules (.gitignore)

  • The file which are going to be mention in the .gitignore file must be in the specific pattern.
  • The .gitignore file will be read by the git from top to bottom approach.
  • .gitignore file will supports negation which uses ! as prefix.
  • *.log know it will ignores all the file with the .log.
  • build/ it will ignores all the build directory.
  • You can comment the lines in the .gitignore file by using the (#).

Local and Personal Git Ignore Rules (.gitignore)

Local Git Ignore Rules

There will one .gitignore file in the local repository in this file and all the files and the directories which should be ignored are mentioned and it will be shared with the others unless you commit and push it to the remote repository. It applicable only to single and particular repository where the file to be ignored.

Personal Git Ignore Rules

You can set up an .gitignore file which can be applied to the all the git repositories. It will not shared with any other collaborators.

Steps To Create a global .gitignore file

Step 1: Create .gitignore file as following.

touch ~/.gitignore_global

Step 2: Add the required rules to the .gitignore file as shown below.

# ~/.gitignore_global

Step 3: Know we need to make this file as an golbal .gitignore file.

git config --global core.excludesfile ~/.gitignore_global

How to undo any commits?

Before using .gitignore to ignore certain files, if you have already committed files you didn’t want to here’s how you can undo it. Use the following command on Git Bash to undo a commit:

git rm --cached -r

Here, “rm” stands for remove while “r” stands for recursive.

Git rm Command

Note: Head over to the GitHub and search for gitignore repositories, you will find a list of gitignore repositories contributed by various people.

FAQs On gitignore

1. gitignore is Not Working

Following are the some of the causes that your gitignore file causes issues.

  • .gitignore file was not tracked by the git. Use (git add .gitignore)
  • The pattern in the .gitignore file was not correct.
  • FIles and directories that which you want to ignore you may be traced already.

2. gitignore Generator

.gitignore generator is an toll which is used to create .gitignore file for the git repositories they are available for the both online and desktop.

3. gitignore file for Nodejs

.gitignore file where we mention the file and directories which you want to ignore following is the sample .gitignore file for the nodejs.



4. gitignore All File in the Folder

If you want ignore all the file in the folder you can use the following command as.If you want to ignore the files in the folder name called GFG_Folder then you can use as follows.


Last Updated : 17 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Similar Reads