Git

How to Add Comments in .gitignore File in Git?

While working with Git, sometimes the user does not understand the functionality of a complex project. For that purpose, comments need to be added along with the code to make the functionality easier to understand. In order to improve the maintainability and readability of files, comments can also be added to the “.gitignore” files.

This write-up will explain the method for adding the comments in the .gitignore file in Git.

How to Add Comments in .gitignore File in Git?

To add comments in the “.gitignore” file, try out the instructions stated below:

    • Navigate to Git local repository.
    • Create a “.gitignore” file and track it in the staging area.
    • Open the file in the editor to modify and add comments by using the “#” sign.
    • Track changes by running the “git add” command.
    • Generate multiple files with different extensions.
    • Verify the ignored file by viewing the Git status.

Step 1: Redirect Toward the Git Local Directory

Initially, used the “cd” command and move toward your preferred repository by inserting the path:

cd "C:\Users\user\Git\demo1"

 
Step 2: Generate a .gitignore File
Execute the “touch” command to generate a “.gitignore” file:

touch .gitignore

 

Step 3: Track in Staging Area

Track the created “.gitignore” file to the staging area with the help of the “git add” command:

git add .gitignore

 

Step 4: Check the Current Status

Run the “git status” command to check the current working status of the Git repository:

git status

 
The resultant image indicates that the file has been tracked successfully:


Step 5: Open File in Editor

Open the “.gitignore” file with the default editor by executing the “start” command:

start .gitignore

 
Now, add a particular extension that you want to ignore and insert a comment using the “#” sign:


Step 6: Check Status

After that, execute the given command to check the current state:

git status

 
It can be observed that the “.gitignore” file has been modified:


Step 7: Add Changes in Staging Area

Next, use the “git add” command to add the changes in the staging area:

git add .gitignore

 

Step 8: Verify the Tracked File

For verification of added changes in the staging area, check the Git status:

git status

 
The resultant image shows that the file has been successfully added to the staging area:


Step 9: Commit Changes

Now, commit all changes with the help of the “git commit” command:

git commit -m ".gitignore file added"

 
The below-given output shows that all the changes have been committed successfully:


Step 10: View the Git Status

Use the given command to check the current status of the Git directory:

git status

 
It can be noticed that there is nothing to commit and the working area has been cleaned:


Step 11: Generate New Files of Different Extensions

Run the “touch” command to create new files in the Git repository:

touch newfile.txt newfile.html

 
Here, we have created two new files with different extensions:


Step 12: Verify the Ignored File

Utilize the below-stated command to check whether the file of “.html” is added or ignored:

git status

 
The resultant output shows that the file with the “txt” extension has been created and the file with the “.html” has been ignored:


That’s all about adding comments in the “.gitignore” file in Git.

Conclusion

To add comments in the .gitignore file in Git, first, navigate to Git local repository and create a “.gitignore” file and track it in the staging area. Then, open the file in the editor to modify and add comments using the “#” sign. Next, track changes using the “git add” command. After that, generate multiple files with different extensions and verify the ignored file by viewing the Git status. This tutorial stated the method for adding the comments in the “.gitignore” file in Git.

About the author

Hafsa Javed