Using .gitignore
in R Projects
Learn how to exclude local artifacts like .Rproj.user from version control, keeping your R repositories clean and collaborative.
What is Version Control?
Version control systems like Git track every change to your project files over time. They allow multiple people to collaborate on the same codebase, revert to earlier versions, and maintain a complete history of development.
However, not every file in your project folder belongs in version control. Some files are personal settings, temporary caches, or generated outputs that would clutter the repository and cause conflicts for collaborators.
What is .gitignore?
A .gitignore file is a plain text file placed at the root of your Git repository. It tells Git which files and directories to intentionally ignore: they will not be tracked, staged, or committed.
Why Does This Matter for R?
R projects generate many local artifacts: files that are useful to you on your machine but would cause problems if shared via Git. Without a .gitignore, these files pollute your repository.
Anatomy of a .gitignore File
A .gitignore file uses simple pattern-matching rules, one per line. Each rule specifies a file, directory, or pattern to exclude from tracking.
Pattern Syntax Rules
Tracked vs Ignored vs Untracked
Git classifies every file in your working directory into one of three categories. Understanding these is essential for using .gitignore correctly.
| Tracked | Ignored | Untracked | |
|---|---|---|---|
| In .gitignore? | No | Yes | No |
| Previously committed? | Yes | No | No |
| Shows in git status? | Yes | No | Yes |
| Included in commits? | Yes | No | Only if staged |
| Example | analysis.R | .Rproj.user/ | new_script.R |
Setting Up .gitignore Step by Step
Example 1: Basic R .gitignore
You are starting a new R project for a class assignment. Your project folder contains analysis scripts, a README, and RStudio auto-generated files.
After saving this file and running git status, you will see that .Rproj.user/ and .Rhistory are no longer listed as untracked files. Your commit will be clean.
Example 2: Wildcard Patterns
Your project knits R Markdown files into HTML reports. You want to share the .Rmd source files but not the generated HTML, since collaborators can re-knit them.
Example 3: Data Files & Credentials
Your research project uses a 500 MB dataset and an API key stored in an R script. Neither should go into Git: the data is too large, and the key is sensitive.
How Git Processes .gitignore
The Already Tracked Problem
One of the most confusing aspects of .gitignore: if a file was already committed before you added it to .gitignore, Git will continue tracking it. The ignore rule only applies to new, untracked files.
Global vs Local .gitignore
Git actually supports two levels of ignore rules. Understanding when to use each helps keep your repositories clean without duplicating effort.
| Global .gitignore | Local .gitignore | |
|---|---|---|
| Location | ~/.gitignore_global | Project root |
| Scope | All repos on your machine | This repo only |
| Shared with team? | No | Yes (committed) |
| Best for | .DS_Store, Thumbs.db, *.swp | .Rproj.user, .RData, data/ |
| Setup | git config --global core.excludesfile | Just create the file |
Common Mistakes to Avoid
Always Ignore in R Projects
| Pattern | What It Matches | Why Ignore It |
|---|---|---|
| .Rproj.user | RStudio user settings directory | User-specific, causes merge conflicts |
| .Rhistory | Console command history | Personal, not reproducible |
| .RData | Saved workspace image | Large, may contain sensitive data |
| .Ruserdata | RStudio user data cache | Machine-specific settings |
| .Renviron | Environment variables (API keys) | Security risk if committed |
| *.html / *_files/ | Knitted output | Reproducible from source .Rmd |
Question 1 of 5
What is the primary purpose of a .gitignore file?
Question 2 of 5
You added .Rproj.user/ to your .gitignore, but Git still tracks changes to files inside that directory. What is the most likely cause?
Question 3 of 5
Given the following .gitignore content, which file would still be tracked by Git?
*.csv
!results.csv
.Rproj.user
.RData
Question 4 of 5
Where should you put OS-specific ignore rules like .DS_Store (macOS) or Thumbs.db (Windows)?
Question 5 of 5
Why is it important to commit the .gitignore file itself to the repository?