Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

What is Git-Ignore and How to Use it?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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

.gitignore is an auto-generated file inside the project folder that ignores/prevents files to get committed to the local and remote repositories.

How to use Git-Ignore?

.gitignore can be used in Git with the help of following steps:

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 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


Some common patterns and format for Git-Ignore :

  • 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.


# Compiled class file

# Log file

# Mobile Tools for Java (J2ME)

# Package Files 







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.

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

My Personal Notes arrow_drop_up
Last Updated : 10 Jul, 2020
Like Article
Save Article
Similar Reads