RE: Understanding Git ignore: How and When to use it?
The `.gitignore` file plays a crucial role in Git version control system. This file instructs Git to deliberately ignore certain files (or types of files) in your project directory. ### When to use .gitignore `.gitignore` file is used when there are files or folders in your project that you don't want Git to track or consider. For example, the following cases highlight situations when `.gitignore` is helpful: 1. **Compiled source codes**: Files like .o and .class files, which are re-created whenever you build your app. They derive directly from your code and there's no need to store them in version control. 2. **Packages and libraries**: The folders holding the necessary libraries and packages like node_modules. It's better to ignore them and advise anyone who checks out the repository to download them independently. 3. **Logs and databases**: These are system-generated files such as log, .ldb, .suo, and .sqlite, which are constantly changing and do not contribute to code functionality. 4. **System Files**: These are files created by the operating system or editor, like `.DS_Store` on MacOS and `Thumbs.db` on Windows, `.vscode` or `.idea` files created by Visual Studio Code or Intellij Idea IDEs respectively. 5. **Environment sensitive files**: Files like .env which might contain sensitive data such as API keys, passwords, and other related configs that vary from one environment to another. ### How to use .gitignore You create a `.gitignore` file in the root directory of your repository. The syntax is pretty straightforward. Precede each entry with path from the repository root and use `/` to ignore paths relative to `.gitignore`. Use `#` to add comments. Here’s an example of a typical `.gitignore` file: ``` # Compiled source code *.class *.o # Packages node_modules/ # Logs and databases *.log *.sqlite # System Files .DS_Store Thumbs.db # User-specific files (e.g. IDE/editor) .vscode/ .idea/ # Environment files .env ``` Files already tracked by Git are not affected; `.gitignore` will only ignore untracked files. If a file is already tracked by Git, you must first untrack it by using the command `git rm --cached `. Please note that `.gitignore` is not a security tool and should not be used to protect sensitive data. Sensitive data committed to a Git repository, even in the past, can still be accessed and seen by others. [GitHub provides a collection of useful .gitignore templates](https://github.com/github/gitignore) for many common programming languages and tools which you might find useful as a starting point. By using the `.gitignore` file, you keep your repository clean, avoid tracking unnecessary files, and improve performance.