Skip to content

Commit ef1d1f8

Browse files
committed
Add section on multiple .gitignore files in a project and .gitignore limitation
1 parent 9288a77 commit ef1d1f8

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

site/posts/understanding-gitignore/index.qmd

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,52 @@ For complete syntax please refer to the [official documentation](https://git-scm
4747

4848
These rules are applied whenever Git checks for changes, so you only see relevant modifications.
4949

50+
## About Multiple .gitignore Files
51+
52+
It's worth noting that it is possible to have multiple .gitignore files throughout the project. .gitignore files are not limited to just one in the root directory. When we place a .gitignore file in a subfolder, Git applies those specific ignore patterns only within that folder.
53+
54+
This is particularly useful for tools like R's renv package or Python's virtual environments, which might have their own temporary files and configurations that should be ignored at different levels of project structure.
55+
56+
```{mermaid}
57+
---
58+
config:
59+
layout: fixed
60+
---
61+
flowchart LR
62+
A[🏠 my-r-project/] --> B[📄 .gitignore<br/>ROOT LEVEL]
63+
A --> C[📁 R/]
64+
A --> D[📁 data/]
65+
A --> G[📁 vignettes/]
66+
A --> J[📁 docs/]
67+
A --> N[📄 README.md]
68+
69+
B --> B1[".Rproj.user/<br/>*.Rproj<br/>.Rhistory<br/>.RData<br/>.Ruserdata<br/>docs/<br/>Meta/<br/>doc/<br/>*.tar.gz"]
70+
71+
C --> C1[📄 utils.R]
72+
C --> C2[📄 main_functions.R]
73+
C --> C3[📄 plot_functions.R]
74+
75+
D --> D1[📄 .gitignore<br/>DATA SPECIFIC]
76+
D --> D2[📄 raw_data.csv]
77+
D --> D3[📄 processed_data.rda]
78+
D1 --> D1A["*.csv<br/>*.xlsx<br/>raw_*<br/>temp_*<br/>backup_*"]
79+
80+
G --> G1[📄 introduction.Rmd]
81+
G --> G2[📄 .gitignore<br/>VIGNETTE SPECIFIC]
82+
G2 --> G2A["*.html<br/>*.pdf<br/>*_cache/<br/>*_files/<br/>figure-html/"]
83+
84+
85+
J --> J1[📄 .gitignore<br/>DOCS SPECIFIC]
86+
J --> J2[📁 _site/]
87+
J --> J3[📄 pkgdown.yml]
88+
J1 --> J1A["_site/<br/>*.html<br/>search.json<br/>sitemap.xml"]
89+
90+
style A fill:#e1f5fe
91+
92+
classDef gitignoreFile fill:#ff9800,stroke:#e65100,stroke-width:2px,color:#fff
93+
class B,D1,F3,G2,H2B,H3B,I2,J1,K2 gitignoreFile
94+
```
95+
5096
## Different Ways to Use gitignore
5197

5298
### Basic Setup
@@ -107,6 +153,17 @@ git config --global core.excludesfile ~/.gitignore_global
107153

108154
Then add common patterns in .gitignore_global file located at ~/ (home directory) that should be ignored across all projects.
109155

156+
## Important Limitation
157+
158+
While .gitignore is powerful, it has an important limitation: it won't ignore files that are already included (staged or committed) in Git. If a file is added to the staging area with `git add`, Git will continue tracking it even if it matches patterns in existing .gitignore file.
159+
160+
If such a file is already included and needs to be excluded, this need to explicitly remove it from tracking using:
161+
```
162+
git rm --cached filename
163+
```
164+
165+
This removes the file from version control but leaves it on the local filesystem.
166+
110167
## Best Practices
111168

112169
1. **Create early**: Add .gitignore at the beginning of a project

0 commit comments

Comments
 (0)