You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: site/posts/understanding-gitignore/index.qmd
+29-31Lines changed: 29 additions & 31 deletions
Original file line number
Diff line number
Diff line change
@@ -2,25 +2,23 @@
2
2
title: "Understanding gitignore: A Simple Guide"
3
3
author: "rajwanur"
4
4
date: "2025-09-04"
5
-
categories: [git, tools, version-control]
5
+
categories: [git, tools, gitignore]
6
6
toc: TRUE
7
7
toc-title: "Table of contents"
8
-
toc-depth: 5
8
+
toc-depth: 3
9
9
---
10
10
11
-
# Understanding gitignore: A Simple Guide
12
-
13
-
If you are working with Git but find yourself dealing with unnecessary files cluttering your repository, **.gitignore** is a tool that can help. Let's explore what it does in plain terms.
11
+
If you are working with Git but find yourself dealing with unnecessary files cluttering your repository, `.gitignore` is a tool that can help. Let's explore what it does in plain terms.
14
12
15
13
## What is gitignore?
16
14
17
-
.gitignore is simply a text file that tells **Git** which files or folders to ignore in your project. It works like an instruction list for version control - when Git sees something mentioned in this file, it pretends that file doesn't exist and will not include it in tracking changes.
15
+
`.gitignore` is simply a text file that tells **Git** which files or folders to ignore in your project. It works like an instruction list for version control - when Git sees something mentioned in this file, it pretends that file doesn't exist and will not include it in tracking changes.
18
16
19
-
Think of it as creating a "do not pack" list before the travel. .gitignore helps programmers avoid committing files they don't want to track. It also helps programmers exclude the files that don't need to be versioned.
17
+
Think of it as creating a "do not pack" list before the travel. `.gitignore` helps programmers avoid committing files they don't want to track. It also helps programmers exclude the files that don't need to be versioned.
20
18
21
19
## The Problem It Solves
22
20
23
-
When working with code - especially languages like SAS, R, or Python - we often generate temporary files:
21
+
When working with code - especially languages like `SAS`, `R`, or `Python` - we often generate temporary files:
24
22
25
23
- Log files showing execution results
26
24
- Temporary output files
@@ -30,11 +28,11 @@ When working with code - especially languages like SAS, R, or Python - we often
30
28
31
29
These files can clutter the repository, making it harder to see actual code changes. They also unnecessarily increase repository size, which can slow down operations.
32
30
33
-
.gitignore solves this by automatically excluding these unwanted files from version control without someone having to manually specify each time.
31
+
`.gitignore` solves this by automatically excluding these unwanted files from version control without someone having to manually specify each time.
34
32
35
33
## How It Works
36
34
37
-
The .gitignore file uses simple patterns to match filenames:
35
+
The `.gitignore` file uses simple patterns to match filenames:
38
36
39
37
-`*.log` - ignores all files ending with .log
40
38
-`temp/` - ignores any folder named temp
@@ -47,16 +45,16 @@ For complete syntax please refer to the [official documentation](https://git-scm
47
45
48
46
These rules are applied whenever Git checks for changes, so you only see relevant modifications.
49
47
50
-
## About Multiple .gitignore Files
48
+
## About Multiple `.gitignore` Files
51
49
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.
50
+
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
51
54
52
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.
Then add common patterns in .gitignore_global file located at ~/ (home directory) that should be ignored across all projects.
152
+
Then add common patterns in `.gitignore_global` file located at `~/` (home directory) that should be ignored across all projects.
155
153
156
-
## Important Limitation
154
+
## Important Note
157
155
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.
156
+
While `.gitignore` is powerful, it has an important feature: 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
157
160
-
If such a file is already included and needs to be excluded, this need to explicitly remove it from tracking using:
161
-
```
158
+
If such a file is already included and needs to be excluded from version control, this need to explicitly remove it from tracking using:
159
+
```bash
162
160
git rm --cached filename
163
161
```
164
162
165
163
This removes the file from version control but leaves it on the local filesystem.
166
164
167
165
## Best Practices
168
166
169
-
1.**Create early**: Add .gitignore at the beginning of a project
170
-
2.**Commit it**: Make sure .gitignore itself is version controlled
167
+
1.**Create early**: Add `.gitignore` at the beginning of a project
168
+
2.**Commit it**: Make sure `.gitignore` itself is version controlled
171
169
3.**Share with team**: Everyone working on a project should use the same rules
172
170
4.**Review occasionally**: As your project evolves, update your ignore patterns
173
171
5.**Exclude all/include some**: To avoid new file types from being tracked, exclude all and include what is expected
.gitignore is a simple but powerful tool that helps maintain clean repositories by excluding unnecessary files. It's not magical - just practical configuration that saves time and reduces clutter in version control systems.
182
+
`.gitignore` is a simple but powerful tool that helps maintain clean repositories by excluding unnecessary files. It's not magical - just practical configuration that saves time and reduces clutter in version control systems.
185
183
186
-
If you haven't used .gitignore before, give it a try on your next project. You'll likely find yourself wondering how you ever worked without it!
184
+
If you haven't used `.gitignore` before, give it a try on your next project. You'll likely find yourself wondering how you ever worked without it!
0 commit comments