Master Git & GitHub From Scratch

Clear, practical, and structured lessons designed to take you from Git fundamentals to real-world GitHub collaboration.

```cshtml

Ignoring Files with .gitignore

Not every file in your project should be tracked by Git. Files such as build output, temporary files, logs, and local configuration files often do not belong in version control. Git allows you to exclude these files using a .gitignore file.

In this lesson, you will learn how to create a .gitignore file, define ignore patterns, and verify which files Git tracks.

1. What Is .gitignore?

A .gitignore file contains patterns that tell Git which untracked files and directories it should ignore. Git uses these patterns when identifying files to include in version control.

Common examples of files to ignore include:

  • Temporary files and application logs.
  • Build output and generated files.
  • IDE-specific configuration files.
  • Local environment configuration files.
  • Dependency folders that can be restored from package managers.
Important: .gitignore does not automatically stop tracking files that have already been committed. It primarily affects untracked files.

2. Create a .gitignore File

Create a file named .gitignore in the root directory of your Git repository.

For example, your project structure might look like this:

my-project/
├── src/
├── tests/
├── bin/
├── obj/
├── app.log
└── .gitignore

Add patterns for files and folders that should not be tracked.

# Build output
bin/
obj/

# Log files
*.log

# Temporary files
*.tmp

# Environment configuration
.env

Lines beginning with # are comments. Each remaining pattern tells Git which matching untracked files or directories to ignore.

3. Common .gitignore Patterns

Pattern Meaning Example
*.log Matches files ending in .log. app.log
temp/ Ignores a directory named temp. temp/cache.txt
*.tmp Matches files ending in .tmp. debug.tmp
/config.json Matches config.json at the .gitignore file's directory level. config.json
!important.log Negates an earlier matching ignore pattern, subject to parent-directory rules. important.log
.env Ignores matching .env files according to the pattern's scope. .env

4. Verify Ignored Files

After adding patterns to .gitignore, use git status to check which changes Git reports.

$ git status

To identify the exact ignore rule that matches a file, use git check-ignore -v.

$ git check-ignore -v app.log

If the file matches an ignore rule, Git displays the rule's source and the matching path.

5. Stop Tracking an Already Tracked File

If a file was already committed, adding it to .gitignore will not remove it from version control. To stop tracking the file while keeping it on your computer, use git rm --cached.

$ git rm --cached config.json

Then commit the change:

$ git add .gitignore
$ git commit -m "Stop tracking config file"
Security note: If a secret or password has already been committed or pushed, ignoring the file is not enough. Remove the secret from the relevant history when necessary and rotate the exposed credential.

6. Practical Workflow

  1. Create a .gitignore file in your repository.
  2. Add patterns for temporary files, logs, and build output.
  3. Run git status to review the changes.
  4. Use git check-ignore -v filename to inspect matching rules.
  5. Commit the .gitignore file so the team shares the same ignore rules.
Key Takeaway

The .gitignore file helps keep generated, temporary, and local-only files out of your Git repository. Use clear patterns, verify them with git check-ignore, and remember that ignoring a file does not remove it from Git if it is already tracked.

```