Skip to content

Commit 772f8da

Browse files
authored
Merge pull request #16 from uddinmehrab/gitdocs/whatisgitignore
Add doc on .gitignore
2 parents 6dd6188 + 57e5e35 commit 772f8da

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed

docs/git_docs/what_is_gitignore.md

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
---
2+
sidebar_position: 7
3+
---
4+
5+
# .gitignore Best Practices
6+
7+
The `.gitignore` file is a powerful tool in Git for specifying which files or directories should be ignored in version control. By keeping unnecessary or sensitive files out of your Git repository, you ensure a cleaner, more efficient workflow. This guide covers some best practices for creating and managing `.gitignore` files effectively.
8+
9+
## 1. Understand the Purpose of `.gitignore`
10+
11+
The `.gitignore` file helps prevent certain files or directories from being tracked by Git. Typical files to ignore include:
12+
13+
Temporary or Build Files: These include compiled code, temporary data, and build artifacts, such as `.o`, `.class`, `dist/`, or `build/`.
14+
15+
Environment-Specific Files: Files that differ based on environment, such as IDE settings (`*.iml`, `.vscode/`) or operating system artifacts (`.DS_Store` on macOS).
16+
17+
Sensitive Information: Files containing sensitive data, like credentials (`.env`, `config.json`), should be ignored to avoid leaking critical information.
18+
19+
## 2. Use Global `.gitignore` for System-Wide Exclusions
20+
21+
You can define a global `.gitignore` file for files that you want to ignore across all repositories on your machine. This is useful for ignoring OS-generated files like `.DS_Store` or editor-specific files.
22+
23+
To set up a global `.gitignore`, run the following commands:
24+
25+
```bash
26+
git config --global core.excludesfile ~/.gitignore_global
27+
```
28+
29+
Add common system or editor-specific files to `~/.gitignore_global` to prevent them from cluttering your repositories.
30+
31+
## 3. Organize Your `.gitignore` by Category
32+
33+
Organize the contents of your `.gitignore` file for easy readability and maintenance. For example, group entries by their type:
34+
35+
```gitignore
36+
37+
#OS-generated files
38+
39+
.DS_Store
40+
Thumbs.db
41+
42+
#Log files
43+
44+
*.log
45+
46+
#Dependency directories
47+
48+
node_modules/
49+
50+
#Build directories
51+
52+
/build/
53+
/dist/
54+
```
55+
56+
Keeping related items together helps ensure that your `.gitignore` file remains readable, especially in larger projects.
57+
58+
## 4. Leverage `.gitignore` Templates
59+
60+
For many common languages and frameworks, you can find `.gitignore` templates that cover the majority of what needs to be ignored. GitHub provides a useful repository with `.gitignore` templates for different languages:
61+
62+
GitHub .gitignore Templates
63+
64+
Use these templates as a starting point and customize them based on the needs of your specific project.
65+
66+
## 5. Avoid Ignoring Critical Files
67+
68+
Be careful not to ignore files that are essential for running or building the project, such as:
69+
70+
Configuration Files: Certain configuration files (`webpack.config.js`, `package.json`, etc.) may be necessary for the project setup.
71+
72+
Documentation: Files like `README.md` or `LICENSE` should always be tracked.
73+
74+
Before adding something to `.gitignore`, consider whether it might be useful for others working on the project.
75+
76+
## 6. Environment Variables and `.env` Files
77+
78+
Environment variables often contain sensitive information, such as API keys, database credentials, or other private configurations. It's a best practice to store these variables in a `.env` file and include that file in your `.gitignore` to ensure that sensitive information is not committed to your repository.
79+
80+
For example, you can add the following entry to your `.gitignore` file:
81+
82+
```gitignore
83+
84+
#Environment variables
85+
86+
.env
87+
```
88+
89+
To share the structure of the environment variables without exposing sensitive values, create a `.env.example` file that contains placeholder keys. This way, new developers can easily understand what environment variables are required for the project:
90+
91+
```
92+
93+
#Example .env file
94+
95+
API_KEY=your_api_key_here
96+
DB_HOST=localhost
97+
DB_USER=your_db_user
98+
DB_PASS=your_db_password
99+
```
100+
101+
By using `.env` files properly, you help protect sensitive data while providing clear instructions for setting up the development environment.
102+
103+
## 7. Ignore by Patterns
104+
105+
You can use wildcard patterns to efficiently ignore multiple files:
106+
107+
`*.log` will ignore all `.log` files.
108+
109+
`**/temp` will ignore any directory named `temp` regardless of where it appears in the directory tree.
110+
111+
`!important.log` will explicitly track `important.log` even if other `.log` files are ignored.
112+
113+
## 8. Check for Existing `.gitignore` Before Adding Files
114+
115+
When adding new dependencies or files, always check the existing `.gitignore` to avoid unnecessary redundancy or missed files. Adding duplicates can make the file harder to maintain.
116+
117+
## 9. Use `.git/info/exclude` for Local Ignores
118+
119+
If there are files you want to ignore locally (for your copy only), you can use `.git/info/exclude`. This file works similarly to `.gitignore` but doesn’t affect others who clone the repository. It’s perfect for local, temporary settings or configurations.
120+
121+
## 10. Audit Your `.gitignore` Regularly
122+
123+
Over time, projects change, and files that were once useful might become obsolete. Periodically review and clean up the `.gitignore` file to remove entries that are no longer needed. This keeps it relevant and tidy.
124+
125+
## Example `.gitignore` for a Node.js Project
126+
127+
Here’s an example `.gitignore` file for a typical Node.js project:
128+
129+
```gitignore
130+
131+
#Node modules
132+
133+
node_modules/
134+
135+
#Logs
136+
137+
logs
138+
.log
139+
npm-debug.log
140+
141+
#Build artifacts
142+
143+
dist/
144+
145+
#Environment variables
146+
147+
.env
148+
149+
#OS-specific files
150+
151+
.DS_Store
152+
Thumbs.db
153+
```
154+
155+
This example covers commonly ignored files, ensuring that your repository remains clean while maintaining only essential code and configuration.
156+
157+
158+
159+
## Additional Resources
160+
161+
[Git Documentation on `.gitignore`](https://git-scm.com/docs/gitignore)
162+
163+
[GitHub `.gitignore` Templates Repository](https://github.com/github/gitignore)
164+

0 commit comments

Comments
 (0)