Ignoring Things
Last updated on 2025-11-11 | Edit this page
Overview
Questions
- What is a
.gitignorefile? - Why should we use it?
- How do we create and manage it?
Objectives
- Explain the purpose of a
.gitignorefile in Git repositories - Create and edit a
.gitignorefile to exclude unnecessary files - Understand common patterns and pitfalls when using
.gitignore
What is .gitignore?
When using Git, you often have files that you don’t want to track. These could be:
- Temporary files created by your text editor or IDE
- Output files from running your code
- Large data files or binary files
- Configuration files specific to your machine
Git provides a file named .gitignore where you can list
patterns that match files or directories Git should ignore.
Why Use .gitignore?
If you don’t ignore unnecessary files:
- Your repository becomes cluttered
- You may accidentally share sensitive or large files
- Your collaborators may run into issues syncing or running code
How to Create and Use .gitignore
You can create the file manually:
And then edit it:
Or use RStudio’s Git pane: It often shows ignored files in grey.
Does It Remove Already-Tracked Files?
No. .gitignore only affects new files.
If you’ve already added a file to Git, .gitignore won’t
stop it from being tracked. To remove a file already tracked:
Then add it to .gitignore.
Challenge: Ignore and Track
Challenge
- Create a file called
secret.txtand add some text to it. - Add it to your
.gitignore. - Try running
git status— is it listed? - Add and commit your
.gitignorefile.
Advanced Tip: Ignore Locally Only
If you want to ignore files only on your machine (not share with others), you can use:
This works like .gitignore, but it’s not
committed to the repository.
- Use
.gitignoreto prevent tracking of unnecessary or sensitive files - Edit
.gitignoreearly in your project to keep your history clean - Use
.git/info/excludefor machine-specific ignores - Files already tracked by Git won’t be ignored retroactively
