Git

How to .gitignore all Files/Folder in a Folder, but Not the Folder Itself?

Like other tracking tools, Git also allows for securing sensitive information about development projects. For this purpose, developers create the “.gitignore” file in their project. They can ignore a single file as well as multiple files parallelly with the help of this file. This operation secures the sensitive information and does not show the ignored file’s changes in the working tree.

This blog will demonstrate the procedure of ignoring all files/folders in a folder except the folder.

How to “.gitignore” all Files/Folders in a Folder, but Not the Folder Itself?

To ignore all files in a folder except the folder, follow the given-provided steps:

    • Redirect to the required directory.
    • Make a “.gitignore” file.
    • Track and commit newly added changes.
    • Add the specific files’ extensions in the “.gitignore” file.
    • Navigate to the desired folder and modify its file.
    • Verify changes.

Step 1: Redirect to Local Repository

First, choose the desired directory and switch to it using the below-listed command:

$ cd "C:\Git\ReposA"

 
Step 2: View List of Files/Folders in Repository

Then, display the list of all files and folders in the current repository:

$ ls

 
It can be observed that the repository contains one folder and two files:


Step 3: Create “.gitignore” File

Next, make a “.gitignore” file through the “touch” command:

$ touch .gitignore

 

Step 4: Track “.gitignore” File

Add the “.gitignore” file to the Git staging area by utilizing the provided command:

$ git add .gitignore

 

Step 5: Commit “.gitignore” File

Then, type out the following command with the desired message to save changes:

$ git commit -m ".gitignore folder created"

 

Step 6: Open “.gitignore” File

Now, open the “.gitignore” file in the default editor to add the particular files extension which needs to be ignored:

$ start .gitignore

 
After running the above-provided command the specified file will be opened with the default editor. Now, add the extension of desired files. For example, we have added a “.txt” extension along with an asterisk “*” symbol, which is used for all existing files:


Step 7: Move to Desired Folder

Next, navigate to the particular folder in the repository using the “cd” command:

$ cd Sub_folder

 
Step 8: List Folder’s Content

Type out the below-provided command to display all the files in the existing folder:

$ ls

 
It can be seen that the current folder contains multiple files with different extensions:


Step 9: Open Ignored Extension File

Then, execute the “start” command along with the particular file name for modifications. We have opened the “.txt” extension type file:

$ start F1.txt

 

After that, switch back to the working repository by running the following command:

$ cd ..

 
Step 10: Verify Changes

Lastly, ensure whether the file has been ignored or not by checking the Git status:

$ git status

 
According to the below screenshot, the working tree is clean which indicates that the added changes in the particular files in which extensions have been ignored are not shown in the current working repository status:


We have explained the easiest method to ignore all files in a folder but not the folder itself.

Conclusion

To ignore all files in a folder except the folder itself, first, move to the desired local directory. Then, create an ignore file in it. Next, track and commit the newly created ignored file. After that, open it in the default editor, add the particular file extension which needs to be ignored and save it. This blog demonstrated the procedure to ignore all files/folders in a folder except the folder itself.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.