Git

How to Ignore Certain Files in Git?

Git is a famous DevOps project version control program frequently used to maintain and manage the project and its source code. Developers work with different project files in the Git working repositories, such as class files, features, functionalities, and log files. However, sometimes they do not want to commit all files from the staging area and want to ignore some of them.

This write-up will explain the method for ignoring certain files in Git.

How to Ignore Certain Files in Git?

To ignore certain files in Git or to ignore a single file, first, open the Git repository, create a new file “.gitignore” and place the file name in the “.gitignore” file to ignore that file.

Take a look at the provided procedure to ignore certain files.

Step 1: Launch Git Bash Terminal

First, launch the “Git Bash” terminal from the Start menu:

Step 2: Open Git Repository

Next, go to the local Git repository through the “cd” command:

$ cd "C:\Git"

Step 3: Create “.gitignore” File

Create a new hidden file “.gitignore” using the “touch” command:

$ touch .gitignore

Step 4: Start File

Start the file in the default selected editor through the following command:

$ start .gitignore

Replace the file name or file extension with a “*” sign to ignore all files of the specified extension. For instance, we have ignored all log files using “*.log”:

Step 5: Add File to Staging Environment

After that, add the “.gitignore” file to the staging area:

$ git add .

Check out the Git repository state to verify if the file is added to a staging area or not using the “git status” command:

$ git status

Here, you can see the file is successfully added to the staging area:

Step 6: Create New File

Now, create a new log file using the “touch” command:

$ touch 1.log

Step 7: Verify File is Ignore or Not

Check the repository state with the “git status” command to see if the file is being ignored:

$ git status

As you can see, we did not receive any untracked log files, indicating that we successfully ignored the log files:

Step 8: Create New File

Again, create a new log file. For instance, we will create the “error.log” file:

$ touch error.log

Step 9: Start .gitignore File

Open the “.gitignore” file in the default selected editor using the “start” command:

$ start .gitignore

To ignore a single or specific file, replace “*.log” with the file name. For instance, we want to ignore only the “error.log” file rather than all log files:

Check the repository status again to verify if the “error.log” file is ignored or not:

$ git status

Here, you can see we have successfully ignored a single log file rather than all log files:

We have taught you how to ignore certain files in Git.

Conclusion

To ignore certain files, first, open the Git repository. Next, create a new file with the “.gitignore” name. Start the file in the default selected editor using the “start .gitignore” command. Place the file name or file extension you need to ignore into the .gitignore file and save it. This write-up has taught you how to ignore certain files in Git.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.