## Understanding .gitignore The `.gitignore` file is an essential component of Git's workflow. It tells Git which files and folders to ignore, preventing unnecessary or sensitive data from being tracked in your repository. ## Why Use .gitignore? Certain files should not be included in version control because they are either: - Temporary or system-generated (e.g., cache, build files, logs) - Large dependencies that can be reinstalled (e.g., `node_modules`) - Personal or sensitive configuration files (e.g., API keys, environment variables) - IDE or editor-specific files (e.g., `.vscode/`, `.idea/`) Ignoring these files keeps the repository clean, reduces conflicts, and prevents security risks. ## Creating a .gitignore File To create a `.gitignore` file: 1. In your project root directory, create a new text file named `.gitignore`. 2. List the files and folders you want to ignore, one per line. 3. Save the file. ### Basic Syntax for .gitignore - `*` → Wildcard for matching multiple files. - `/` → Specifies path relative to `.gitignore`. - `#` → Adds comments. ### Example .gitignore File: ```sh # Ignore Mac system files .DS_Store # Ignore dependency folders node_modules/ venv/ # Ignore log and cache files *.log .cache/ # Ignore environment files .env # Ignore all text files *.txt ``` ## Global .gitignore (For All Projects) To create a global `.gitignore` file (applies to all repositories): ```sh git config --global core.excludesfile ~/.gitignore_global ``` Then, edit `~/.gitignore_global` as you would a local `.gitignore`. ## Removing Files from Git Tracking If a file was already committed before adding it to `.gitignore`, you need to remove it from tracking: - **Untrack a single file** (but keep it locally): ```sh git rm --cached filename ``` - **Untrack all ignored files**: ```sh git rm -r --cached . git add . git commit -m "Updated .gitignore" ``` To undo `git rm --cached filename`, use: ```sh git add filename ```