Course contentsShow
← Git & GitHub
Lesson 18 of 962. Core Git MechanicsFree lesson

Ignoring Unwanted Files and Directories

You will configure a gitignore file to prevent temporary or sensitive system files from being tracked or staged in your repository.

In this lesson, you will learn how to create a .gitignore file to prevent Git from tracking files that don’t belong in your version history.

Why We Need to Ignore Files

As you develop projects, your operating system and code editors often create "junk" files. For example, macOS creates .DS_Store files to manage folder views, and editors like VS Code create a .vscode folder to store your personal workspace settings. You don't want these files cluttering your repository or being shared with collaborators, as they aren't part of your actual source code.

Creating the .gitignore File

To tell Git to ignore these files, you create a plain text file named exactly .gitignore in the root of your project directory. Inside this file, you list the names or patterns of the files and folders you want Git to ignore.

For example, if you want to ignore all .DS_Store files and the .vscode folder, your .gitignore file would look like this:

# Ignore system files
.DS_Store

# Ignore editor settings
.vscode/

Once this file exists and is committed to your repository, Git will ignore any files matching these patterns. Even if you run git add ., Git will simply "look past" these files as if they don't exist.

Important Considerations

  • Case Sensitivity: Git is case-sensitive, so ensure your patterns match the file names exactly.
  • Existing Files: If you have already staged or committed a file before adding it to your .gitignore, Git will continue to track it. You must first remove the file from the index (using the skills you learned in lesson 16) before the ignore rule will take effect.
  • Project Specifics: A .gitignore file should only contain files that are specific to your local environment. Avoid adding files that other team members might need to run the project.

By maintaining a clean .gitignore file, you ensure that your repository remains a "source of truth" containing only the files that matter to your project’s history.

Key Takeaway: Use a .gitignore file to explicitly instruct Git to skip over system artifacts and temporary configuration files, keeping your project history clean and professional.