The Wayback Machine - https://web.archive.org/web/20240822105416/https://www.geeksforgeeks.org/ignoring-files-and-folders-in-git/
Open In App

Ignoring Files and Folders in Git

Last Updated : 15 Feb, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git relies on the basis of distributed development of software where more than one developer may have access to the source code of a specific application and can modify changes to it that may be seen by other developers. So in this article, we will cover various concepts related to ignoring files and folders in the git. We will start the article by learning how to create the .gitignore file and how it works.

Creating .gitignore and knowing about its functionalities 

Creating .gitignore file

Using git status to show the status of files

Now we can see that there are a lot of files that are in untracked files and some of them are those files that are being staged already but there are no files that are being ignored. Sounds interesting so that means that there are ignored files also in git. So to ignore a file in git we use a .gitignore file inside that file you can mention whether you want a particular file to be ignored or a file ending with a specific extension. So here in this file, we can see that *.txt is mentioned which means that all the .txt files should be ignored in this directory.

Adding which files shouldn’t be added in …gitignore

Now we will use the git status –ignored the command to see all the ignored files present in the directory. 

Using git status –ignored to see the status of ignored files

Exceptions in a.gitignore file

So how do exceptions work in the .gitignore file is we can take the example of our *.txt which we wrote in our .gitignore file which means that all the .txt files should be ignored. Also, we can write !file_name.txt which means that the particular file shouldn’t be ignored. So here we can see when *.txt was mentioned in the .gitignore file all the .txt files were ignored in the directory.

Editing the .gitignore file with !file_name.txt

Adding the !file_name.txt in the .gitignore file after adding this we can see that the file is not in ignored files now and here you can see the file_name is a.txt which previously was coming in the ignored files and now we can see that now the file is in untracked files.

Seeing the changes after adding this !a.txt in the .gitignore file

So if there are some .txt files in some folder then that folder will be ignored and we cannot easily reinclude files from that folder so there is a procedure how to reinclude all those files from that folder because here according to .gitignore all the *.txt files should be ignored except a.txt.

Using git status –ignored

Here you can see the secrets folder is in the ignored files because according to the .gitignore all the *.txt files should be ignored. So to reinclude all the *.txt files from the secrets folder we have to first reinclude the secrets folder by writing !folder_name in the .gitignore here folder_name is secrets and then we have to ignore all the files present in the secrets folder by writing secrets/* in the .gitignore and then write !secrets/*.txt to reinclude all the .txt files from the secrets folder to remove the folder from and reinclude all the .txt files in the folder from ignored files. 

Editing .gitignore file

Using git status –ignored to see whether the folder is removed from the ignored files or not

Now if our use case is to remove those files whose file_names are beginning with ! in the .gitignore file then add one more ! or add \ in the front of your file_name or you can also remove a folder by this who is having some files which don’t want git to have a track on them. By using these exceptions in the a.gitignore file you will move these files and folders in the ignored files.

Editing .gitignore

Using git status –ignored to see the files are in ignored files or not

A Global .gitignore file

So if our use case is that if we want specific files to be ignored by git in all repositories then we can create a global .gitignore in any of the repositories and make it global using the following command: 

git config --global core.excludesfile path_of_global_gitignore_file

Here you can see the path should be complete that is the name_of_the_drive/folder_name/.gitignore to avoid errors like this warning: unable to access permission denied fatal: cannot use as an exclude file. Now git will use this in addition to each repository’s .gitignore file. So let us see the syntax of the command:

Showing the syntax of the command: git config –global core.excludes file path_of_global_gitignore_file

So now if we edit the global .gitignore file and write *.py then it will ignore all the *.py files present in our repository. Now let’s see how this works:

Editing the global .gitignore file

So now if we use the git status –ignored command we will see all the .py files in ignored files. 

Using git status –ignored

So now if we include all these *.py files in the local .gitignore file of our repository then what will happen because our global .gitignore file ignores all the .py files. Now the question here is who will take the priority so the answer to this is our local .gitignore file takes priority always whenever there is a file that is ignored by the global .gitignore file but is included by the local .gitignore file. So we will write !*.py in our local. .gitignore file will include all the files .py files and will not put them in ignored files.

Editing the local .gitignore file

So, now we can see all the py files are now in untracked files and there are no py files that are in ignored files.

Using git status –ignored command to see whether the py files are removed from the ignored  files or not

Ignore all the files that already have been committed to a Git repository

So now if we want a file that already has been committed to the git repository and now we want that git should stop tracking it for that type of use-case we can remove it using the following command:

git rm --cached file

This will remove the file from the repository and prevent any further changes from being tracked by Git. The –cached option will make sure that the file is not physically deleted. The previously added contents of this file will still be visible but keep in mind that if anyone else pulls from the repository after the file is being removed then their copy will be physically deleted. 

Before using git rm –cached status of the file

After Using git rm –cached file  

You can make git pretend that the working tree version of the file is up to date thus ignoring changes in it with the skip-worktree option using the command.

git update-index --skip-worktree file_name

Here we can see that readme.md is modified

Here we can see that after using the above command git is ignoring any changes being done in the readme.md

Here we can see by using this above command git is not able to track any further changes in the readme.md and ignore any changes being done in the readme.md. Now to have a track of the changes in the readme.md we can use the command git update-index –no-skip-worktree file_name.

Using the command git update-index –no-skip-worktree file_name

Now we can see that on using git status we can see that readme.md is being modified and some changes need to be staged for commit. Now if you want to force git to ignore any changes made in a  file then we can use the command.

git update-index --assume-unchanged file_name 

Using the above command so that git doesn’t ignore the file

Now if we want that git shouldn’t ignore the file and again take care of it. For this, we will use the command 

git update-index --no-assume-unchanged file_name 

Using the above command so that git doesn’t ignore the file

Ignoring files locally without committing ignore rules

If you want to ignore certain files in a repository locally and not make the file part of any repository, then we will edit the .git/info/exclude inside your repository. Here we will ignore a file say rock.py and now we can see that before editing the file .git/info/exclude git is showing us the file in untracked files.

Before editing the file .git/info/exclude

Editing the file .git/info/exclude

Using git status –ignored to see the status of rock.py

Now we can see that rock.py is in ignored files now.
 

Ignoring a file in any directory 

If we want to ignore a file in any directory we have to just write its name in the  .gitignore file whether in your global or local or it can be your .git/info/exclude of the repository according to your use case. If you want that in your particular repo only a particular file in a specific directory should be ignored then you can mention the name of that file in the local.gitignore file and if you want that all the repositories should ignore the file then you can write the name of that file in global .gitignore then the file would be ignored by all the repositories.

Mentioning the name of the file .gitignore

Using git status –ignored

Here you can see the file is now in ignored files and whatever the folders were containing that file is also ignored. Now if you want to ignore a certain file in a specific folder only then we can edit the .gitignore just like this: mainfolder_name/**/file_name. Here ** denotes subdirectories. The meaning of mainfolder_name/**/file_name this line is only the file contained in this folder or its subdirectories will be ignored. But if the file is at some other location of the repository with the same name then that will not be ignored.

Editing the .gitignore

Using git status –ignored

Now we can see that the cars.cpp file created in the main folder is in the untracked files and the file cars.cpp which is present in the testing folder and its subdirectories are ignored because in the .gitignore file it is written that testing/**/cars.cpp which means all the files with cars.cpp name should be ignored in the testing folder and its subdirectories or what we can do is create a local .gitignore file in the folder where want to ignore the file. Now what are we doing is creating a local .gitignore file in the folder where we are going to ignore the file. So what we have done is we have erased this line testing/**/cars.cpp from our local .gitignore file so that we can see the effect of the .gitignore file present in our folder testing which we have newly created.

Editing the local  .gitignore

Using git status –ignored command

Now you can see that on using git status –ignored command we are seeing that cars.cpp from the testing folder and its subdirectories are ignored. 

Prefilled .gitignore Templates

Create an Empty folder

It is not possible to add and commit an empty folder in the git so to achieve this we have two methods:

Method one: .gitkeep  

So for tracking an empty folder in git we will create an empty folder inside the repository and in that folder create an empty .gitkeep file which will register the folder to git and we will be able to track the empty folder.

Checking the status of empty folder gitisfun without a .gitkeep

So now we can see that the gitisfun folder is not having any .gitkeep file and without any .gitkeep file it is not being tracked by git.  So let’s create a .gitkeep file in an empty folder so that we can track an empty folder in git.

Creating .gitkeep file in the empty folder

Adding the empty folder in the staging area

So now we can see that the empty folder is now being tracked by git. There is one more method by which we can track the empty folder in git.

Method two: dummy.txt

So to track an empty folder in git using dummy.txt, we will create an empty folder in which we will create a dummy.txt file, and in that, you can insert any funny messages you want and now after creating this dummy.txt file in the empty folder we will be able to add the empty folder in the staging area.

Without dummy.txt the empty folder is not being tracked by git

Created dummy.txt in the empty folder and now we can see that the empty folder is now being tracked by git 

Here the name of the empty folder is batman. So these are the two methods by which we can track an empty folder in git. 

Finding files ignored by .gitignore

So now if you want to see all the files ignored by git in your repository or any of the directories present in the repository we can use the following command:

git status --ignored

Using git status –ignored command

So now if you want to list recursively ignored files in directories, we will be using an additional parameter  –untracked-files=all, and the command would be:

git status --ignored --untracked-files=all

Using git status –ignored –untracked-files=all

So now we can see that it is listing all the recursively ignored files in directories also.

Ignoring changes in tracked files

So now if you are thinking of ignoring changes in tracked files then you might think that why not to use .gitignore or .git/info/exclude and just mention the name of the file which we want to ignore but .gitignore and .git/info/exclude only works for ignoring untracked files. So to ignore a tracked file we use the following command git update-index –skip-worktree file_name. So here you can see that the file try.txt which is in tracked files is modified but now after applying the command: git update-index –skip-worktree file_name any changes done in the try.txt is being ignored by the git because of the command we used.

Modifying the file try.txt

Using the command git update-index –skip-worktree file_name

So now we can see that after applying the command git update-index –skip-worktree file_name the tracked file is being ignored and now if you want to revert this we can use the following command: git update-index –no-skip-worktree file_name so this command will again make the file to be tracked by git.

Using the command git update-index –no-skip-worktree file_name

We can also use the command git update-index –assume-unchanged file_name in alternative to git update-index –skip-worktree file_name both works the same and in alternative to the command git update-index –no-skip-worktree we can use git update-index –no-assume-unchanged file_name command.



Similar Reads

How to Ignore Folders and Directories in Git with .gitignore?
Git is a powerful and widely-used version control system that allows developers to track changes in their codebase, collaborate seamlessly, and maintain a robust history of their projects. It was created by Linus Torvalds in 2005, Git has become widely accepted for version control in the software development industry. In this article, we will learn
4 min read
Difference Between Git remote prune, Git prune and Git fetch --prune
Git is a version control system that helps developers manage and track changes in their codebase. Among its many commands, git remote prune, git prune, and git fetch --prune are essential for maintaining a clean and organized repository. This article will explain the differences between these commands, their syntax, uses, and provide examples to il
4 min read
How to Push Folders From Local PC to GitHub using Git Commands?
Git offers a powerful command-line interface for managing code repositories, understanding how to push folders from your local PC to GitHub repositories using Git commands can enhance your workflow and streamline the process of sharing code with collaborators or deploying projects. In this article, we'll see the step-by-step process of pushing fold
2 min read
Git LFS: Managing Large Files in Git Repositories
Git, undoubtedly one of the most popular version control systems, is widely used for managing source code in software development projects. However, one of its limitations is its handling of large files. Traditional Git repositories struggle to efficiently manage large files, leading to bloated repositories and slow performance. This is where Git L
4 min read
Difference Between Git Fetch and Git Pull
Understanding the difference between git fetch and git pull is important for effective version control in Git. These commands are important for managing your repository and collaborating with team members. In this article, Let us look at Git Fetch and Git Pull separately with the help of an example. What is Git Fetch?The Git Fetch command is used t
4 min read
Difference Between Git Push Origin and Git Push Origin Master
Understanding the difference between git push origin and git push origin master is important for efficient version control in Git. These commands are used to upload changes from your local repository to a remote repository, but they function differently. This article will explain these differences, how each command works, and when to use them. What
3 min read
Difference between “git add -A” and “git add”
When working with Git, the git add command is used to move changes from your working directory to the staging area. However, there are different options and flags that can be used with git add, such as -A, which might lead to some confusion. In this article, we'll explore the differences between git add -A and git add. 1. Git add- CommandThe git ad
2 min read
Git - Difference Between Git Revert, Checkout and Reset
Git offers a range of commands to manage and manipulate your codebase. Among these commands, git revert, git checkout, and git reset are frequently used for different purposes. Understanding the differences between these commands is important for effective version control. In this article, we'll explore the functionalities and use cases of git reve
6 min read
Difference Between "git commit" and "git push"?
Git commit and git push are two essential commands you'll use a lot when working with Git. even their frequent use together, they have different functions. In order to help you understand when and how to use these two commands effectively in your version control workflow, this article will break down their differences. What is git commit?git commit
2 min read
Git - git-show Command Line Utility
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git relies on the basis of distributed development of software where more than one developer may have access to the source code of a specific application and can modify changes to it that may be
3 min read
Git Subtree vs. Git Submodule
Git Subtree and Git Submodule are both mechanisms in Git that allow you to incorporate external repositories into your own repository. They provide a way to manage dependencies and include code from other projects while keeping your repository organized and maintainable. Git Subtree: Git Subtree allows you to insert a separate repository as a subdi
4 min read
How to Set Up Git Using Git Config?
Git is a powerful version control system that helps developers manage and track changes in their code. Setting up Git correctly is important for maintaining an organized and efficient workflow. In this article, we'll walk you through the process of setting up Git using the git config command. PrerequisitesBefore we dive into the setup process, make
3 min read
How To Exit Git Log or Git Diff?
When working with Git, the git log and git diff commands are important for viewing commit history and changes between commits, respectively. However, if you're new to Git, you might find yourself stuck in these views, unsure how to exit. This article will guide you through the steps to exit git log or git diff, ensuring a smooth experience while na
2 min read
Git - git prune
Git is a powerful version control system that helps developers manage their code efficiently. However, as your project evolves, you may accumulate unnecessary data that clutters your repository. This is where git prune comes in view. What is git prune?git prune is a Git command that removes objects that are no longer referenced by any branch or tag
5 min read
How to Fix Git Error "Unable to create '/path/my_project/.git/index.lock'"?
The Git error "Unable to create '/path/my_project/.git/index.lock'" typically occurs when Git is unable to create or write to the index.lock file. This file is used to prevent simultaneous processes from modifying the repository, which could lead to corruption. Table of Content Remove the Lock File ManuallyUse Git Command to Remove the Lock FileRes
2 min read
Optimizing Git for Large Binary Files
Version Control Systems are a category of software tools that help in recording changes made to files by keeping track of modifications done in the code. Table of Content What is large binary files?The Challenge of Large Binary Files in GitWhy do we need to optimize binary files in Git?Strategy for optimizing Git for Large Binary Files:Approach 1:
11 min read
How to Add All Files in Git ?
Adding all files in Git is a common task when you want to stage all changes for committing. Adding all files in Git involves staging all modifications, additions, and deletions in your working directory for the next commit. This process ensures that all changes are included in the commit history. In this article, we will see the detailed guide for
3 min read
How to list All Files in a Commit in Git?
Working with Git, it's often essential to inspect the contents of your commits. Whether you need to review changes, debug issues, or understand the history of your project, knowing how to list all files in a commit is a fundamental skill. In this article, we’ll walk you through the steps to list all files in a commit in Git, providing you with the
3 min read
How to Show Files Changed Between Two Revisions in Git?
To show files changed between two revisions in Git, you can use the git diff command with specific commit references. Here's a step-by-step guide: Table of Content Using Git DiffUsing Git LogUsing Git DifftoolUsing Git ShowIdentify the RevisionsRun the Diff CommandView the DifferencesOptional: View Unified DiffReview and Analyze ChangesUsing Git Di
3 min read
How to Stash a Specific File or Multiple Files in Git?
Git stash command is a powerful tool for developers, allowing them to temporarily save changes that are not yet ready to be committed. This is particularly useful when you need to switch branches or pull updates from a remote repository without committing to unfinished work. While `git stash` typically saves all changes in the working directory, yo
4 min read
How to Compare Files From Two Different Branches in Git?
To compare files from two different branches in Git, you can use the git diff command. This command allows you to view the differences between two branches or commits. Below are the steps to compare files from two different branches: Table of Content Using Git DiffUsing Git DifftoolCheckout the BranchesRun the Diff CommandView the DifferencesOption
3 min read
How to Delete Untracked Files in Git?
Git is a powerful version control system widely used in software development for tracking changes in source code. While working with Git, you might find yourself in a situation where you have untracked files—files that are not yet committed to the repository. Over time, these untracked files can clutter your working directory. In this article, we'l
3 min read
How to Remove .DS_Store Files From Git Repositories?
If you're a macOS user, you've probably encountered .DS_Store files. The macOS Finder creates these files to store custom attributes of a folder, such as the position of icons and the view settings. While they are harmless in themselves, they can clutter your Git repository and cause unnecessary conflicts when collaborating with others, especially
3 min read
How to Remove Deleted Files from a Git Repo?
Managing files in a Git repository often involves adding, modifying, and deleting files. However, sometimes files are deleted from the disk but remain tracked in the repository. To keep your repository clean and in sync with your working directory, you need to remove these files from the repository as well. This article will guide you through the p
3 min read
Remove Large Files from Commit History in Git
Sometimes, you may accidentally commit a large file to your Git repository, making it unwieldy and slow. Large files can significantly increase the size of your repository, making operations like cloning, fetching, and pulling slower and more cumbersome. This can be particularly problematic in team environments where multiple developers are constan
2 min read
How to Remove Files From Git Staging Area?
The Git staging area (also known as the index) is an intermediate space where Git stores changes that are about to be committed. When you add a file to the staging area using git add, it is prepared for the next commit. However, there are situations where you might need to remove a file from the staging area before committing it. You added the wron
2 min read
How to Remove Local Untracked Files From Current Git Working Tree?
When working on a Git repository, it’s common to create temporary files or directories that you no longer need. These untracked files can clutter your working tree and make it harder to focus on the changes that matter. Fortunately, Git provides simple commands to clean up these untracked files. This article will guide you through the process of re
4 min read
How to Remove Added Files in Git?
In version control, Git is a powerful tool that helps developers manage and track changes to their code. One common operation in Git is merging branches. However, there are times when you might need to revert a merge commit, either because it introduced issues or was done by mistake. This article will guide you through the process of reverting the
3 min read
Git - Move Files
When managing a project using Git, you may find the need to reorganize your directory structure or rename files. Git provides powerful tools to handle these tasks seamlessly while keeping track of the changes. This article will guide you through the process of moving files within a Git repository, covering the necessary commands and best practices.
3 min read
Difference Between Git and GitHub
Git: Git is a distributed version control system for tracking changes in source code during software development. It is designed for coordinating work among programmers, but it can be used to track changes in any set of files. Its goals include speed, data integrity, and support for distributed, non-linear workflows. GitHub: GitHub is a web-based G
2 min read
Article Tags :
three90RightbarBannerImg