Lesson 19 of 60 – Git Ignore & .gitignore
32%

Git Ignore & .gitignore

The .gitignore file tells Git which files and folders should be ignored and should not normally be included in commits. It is commonly used for temporary files, generated files, local configuration files, dependencies, and sensitive local settings.

Note: A .gitignore file affects files that Git is not already tracking. If a file has already been committed, adding it to .gitignore does not automatically remove it from Git tracking.

1. What is .gitignore?

.gitignore is a special text file used by Git to specify files and directories that should be ignored.

Example:

.gitignore

Git reads the rules written in this file when determining which untracked files should be considered for staging.

2. Why Use .gitignore?

Projects often contain files that should not be committed to the repository.

  • Temporary files
  • Log files
  • Build output
  • Dependency folders
  • Local configuration files
  • Operating-system files
  • IDE-specific files

3. Creating a .gitignore File

Create a file named:

.gitignore

It is normally placed in the root directory of the Git repository.

my-project/
├── .git/
├── .gitignore
├── index.html
└── style.css

4. Ignoring a Single File

To ignore a specific file, write its name in .gitignore.

config.txt

Git will then ignore an untracked file named config.txt matching that rule.

5. Ignoring Multiple Files

You can add multiple file names on separate lines.

config.txt
notes.txt
debug.log

Each line represents a separate ignore rule.

6. Ignoring a File Extension

An asterisk can be used to match files with a particular extension.

*.log

This rule can ignore files such as:

error.log
debug.log
application.log

7. Ignoring a Folder

To ignore a directory and its contents, add the directory name.

node_modules/

The trailing slash indicates that the pattern represents a directory.

8. Ignoring node_modules

JavaScript projects commonly use the node_modules directory for installed dependencies.

node_modules/

Instead of committing the entire dependency directory, projects usually store dependency information in files such as package.json and package-lock.json.

9. Ignoring Temporary Files

Temporary files can be ignored using patterns.

*.tmp
*.temp

This can prevent temporary files from appearing as untracked files.

10. Ignoring Log Files

Log files are often generated automatically by applications.

*.log

This rule can ignore log files throughout matching directories.

11. Using Comments in .gitignore

Lines beginning with # are comments.

# Node dependencies
node_modules/

# Log files
*.log

Comments help explain why particular files or folders are being ignored.

12. Ignoring a Specific Directory Path

A path can be used when you want a rule to match a particular location.

/config/local.txt

The leading slash makes the pattern relative to the directory containing the .gitignore file.

13. Ignoring All Files in a Folder

A directory pattern can ignore the directory and its contents.

uploads/

Files such as the following can then be ignored when they are untracked:

uploads/photo.jpg
uploads/document.pdf
uploads/file.txt

14. Wildcard *

The * wildcard can match multiple characters.

*.log

This matches file names ending with .log.

debug.log
server.log
error.log

15. Wildcard ?

The ? wildcard can match a single character.

file?.txt

For example, it can match:

file1.txt
file2.txt

16. Ignoring Environment Files

Some applications use environment files for local configuration. A project may choose to ignore them.

.env

For example, an environment file may contain local configuration values that should not be committed to a public repository.

17. Ignoring IDE Files

Development tools may create project-specific files. For example:

.idea/
.vscode/

Whether these directories should be ignored depends on the project and team requirements.

18. Negating an Ignore Rule

An exclamation mark can be used to negate an ignore pattern.

*.log
!important.log

This means matching log files are ignored except for important.log, subject to the normal Git ignore pattern and directory rules.

19. Example .gitignore File

# Dependencies
node_modules/

# Logs
*.log

# Environment file
.env

# Temporary files
*.tmp

# IDE files
.vscode/

This is an example of combining different ignore rules in one file.

20. .gitignore and git status

After creating or changing .gitignore, use:

git status

Ignored files that match the rules normally do not appear as ordinary untracked files.

21. Checking an Ignore Rule

Git provides a command for checking which ignore rule applies to a particular path.

git check-ignore -v filename

The -v option can show the matching ignore rule and its source.

22. .gitignore Does Not Untrack Committed Files

Suppose a file has already been committed:

git add config.txt
git commit -m "Add config file"

Adding config.txt to .gitignore later does not automatically remove it from tracking.

23. Removing a Tracked File from Git

If a tracked file should no longer be tracked, you can remove it from the Git index while keeping the local file:

git rm --cached config.txt

Then add the file to .gitignore and commit the change.

24. Global Gitignore

Git also supports a global ignore file for patterns that you want to ignore across multiple repositories on your computer.

A global ignore file is useful for machine-specific files such as operating-system or editor files.

The global configuration can be set using:

git config --global core.excludesfile ~/.gitignore_global

25. .gitignore Should Usually Be Committed

A project-level .gitignore file is normally committed to the repository so that the entire team can use the same project ignore rules.

git add .gitignore
git commit -m "Add gitignore rules"

26. Common .gitignore Mistakes

  • Using the wrong file name.
  • Accidentally adding spaces or incorrect patterns.
  • Expecting .gitignore to remove already committed files.
  • Ignoring files that the project actually needs.
  • Forgetting to commit the project-level .gitignore file.
  • Using overly broad patterns.
  • Not checking the result with git status.

27. Useful .gitignore Patterns

*.log
*.tmp
*.bak
node_modules/
.env
.vscode/
.idea/
build/
dist/

These are examples only. The correct patterns depend on the technology and requirements of the project.

28. Complete .gitignore Workflow

# Create the file
touch .gitignore

# Add rules
echo "*.log" > .gitignore

# Check repository status
git status

# Add .gitignore
git add .gitignore

# Commit the rules
git commit -m "Add gitignore rules"

The exact command used to create the file can vary depending on your operating system and shell.

29. Practical Example

Suppose a project contains:

my-project/
├── index.html
├── style.css
├── app.log
├── .env
├── node_modules/
└── .gitignore

The .gitignore file could contain:

*.log
.env
node_modules/

Now Git can ignore the matching untracked files and directory.

30. Summary of .gitignore

The .gitignore file helps control which untracked files and directories Git should ignore.

*.log
.env
node_modules/

It helps keep repositories clean by preventing unnecessary files from being staged and committed.

Remember that .gitignore does not automatically stop tracking a file that has already been committed.

📌 Key Points

  • .gitignore contains Git ignore rules.
  • It is commonly placed in the repository root.
  • Files can be ignored by name, extension, pattern, or directory.
  • *.log can match log files.
  • node_modules/ can ignore a dependency directory.
  • # is used for comments.
  • ! can negate an ignore rule.
  • git check-ignore -v can help identify the matching rule.
  • .gitignore does not automatically untrack already committed files.
  • git rm --cached can remove a tracked file from the index while keeping the local file.
  • A project-level .gitignore is normally committed to the repository.
  • Global ignore rules can be configured for machine-specific files.

🧠 Quick Quiz

Question: What is the main purpose of a .gitignore file?