![]() |
The .gitignore file is a powerful tool in Git that allows you to specify which files and directories should be ignored by Git. This helps prevent unnecessary files, such as build artefacts and temporary files, from being tracked and cluttering your repository. In this article, we will explore how to create and configure a .gitignore file to keep your Git repository clean and organized. Table of Content Why Use a .gitignore File?Using a .gitignore file is crucial for several reasons:
Approach 1: Creating a .gitignore File ManuallyTo create a .gitignore file manually, follow these steps: Step 1: Open your terminal or command prompt: Navigate to the root directory of your Git repository. Step 2: Create the .gitignore file: Use the following command to create the file touch .gitignore
Open the .gitignore file: Use a text editor to open and edit the .gitignore file. For example, with nano: nano .gitignore
Approach 2: Using TemplatesThere are various templates available for different programming languages and frameworks that you can use as a starting point. GitHub provides a collection of .gitignore templates at github.com/github/gitignore. Steps to use a template:
Configuring the .gitignore FileThe .gitignore file uses simple pattern matching to specify which files and directories should be ignored. Here are some basic patterns: Ignoring Specific Files: To ignore specific files, simply list their names: file.txt
Ignoring Specific Directories: To ignore entire directories, append a slash (/) to the directory name: /node_modules/ Ignoring Files by Extension: To ignore all files with a specific extension, use a wildcard (*): *.log Ignoring Nested Files and Directories: To ignore files and directories at any level, prefix the pattern with two asterisks (**): **/temp/ Negating Patterns: To include a file that would otherwise be ignored by a previous pattern, prefix the pattern with an exclamation mark (!): !important.log ExampleHere are some common examples of patterns used in a .gitignore file: # Logs Applying ChangesOnce you have created and configured your .gitignore file, make sure to add and commit it to your repository: git add .gitignore |
Reffered: https://www.geeksforgeeks.org
Git |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |