Adding a Pattern to your .gitignore file

  • Check if a .gitignore file exists in your repository. If the file doesn’t exist, create one.
  • Open the .gitignore file in a text editor.
  • Specify a pattern in the file that matches the file you want to ignore.
  • For a single file, use the exact filename (e.g., filename).
  • To ignore specific file types, use wildcards (e.g., *.log).
  • For directories, use the directory name followed by a forward slash (e.g., folder/)
  • Save the .gitignore file.

Example:

Imagine you want to ignore a file named “config.txt” and any files ending in “.log”.

  • Open or create the .gitignore file.
  • Add the following lines to the file:
 config.txt    
*.log

Now, Git won’t track any changes made to “config.txt” or future files with the “.log” extension.


How to Ignore Files that have Already been Committed to the Repo?

In Git, keeping your repository clean and organized is crucial. Sometimes, you might have files that were accidentally committed but shouldn’t be tracked by Git.

This article explores effective methods to handle such situations and prevent these files from being tracked in the future.

Table of Content

  • Removing the file from Git’s Tracking (keeping it Locally)
  • Adding a Pattern to your .gitignore file

Similar Reads

Removing the file from Git’s Tracking (keeping it Locally)

This approach removes the file from Git’s staging area (index) while preserving it in your working directory. Execute the following command, replacing with the actual name of the file you want to ignore:...

Adding a Pattern to your .gitignore file

Check if a .gitignore file exists in your repository. If the file doesn’t exist, create one. Open the .gitignore file in a text editor. Specify a pattern in the file that matches the file you want to ignore. For a single file, use the exact filename (e.g., filename). To ignore specific file types, use wildcards (e.g., *.log). For directories, use the directory name followed by a forward slash (e.g., folder/) Save the .gitignore file....