Skip to content

Update creating-a-gitignore-file.md #95977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,73 +1,73 @@
# .gitignore
## Understanding .gitignore

The .gitignore file is a text file that tells Git which files or folders to ignore in a project.
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.

A local .gitignore file is usually placed in the root directory of a project. You can also create a global .gitignore file and any entries in that file will be ignored in all of your Git repositories.
## Why Use .gitignore?

## Why .gitignore
Now you may wonder why would you want git to ignore certain files and folders. Its because you don't want files like build files, cache files, other local configuration files like node modules, compilation files, temporary files IDE's create, etc to be tracked by git. It's usually used to avoid committing transient files from your working directory that aren't useful to other collaborators.
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/`)

## Getting started
To create a local .gitignore file, create a text file and name it .gitignore (remember to include the . at the beginning). Then edit this file as needed. Each new line should list an additional file or folder that you want Git to ignore.
Ignoring these files keeps the repository clean, reduces conflicts, and prevents security risks.

The entries in this file can also follow a matching pattern.
## Creating a .gitignore File

```
* is used as a wildcard match
/ is used to ignore path names relative to the .gitignore file
# is used to add comments to 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.

This is an example of what the .gitignore file could look like:
### 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
.DS_Store

# Ignore node_modules folder
node_modules
# Ignore dependency folders
node_modules/
venv/

# Ignore all text files
*.txt
# Ignore log and cache files
*.log
.cache/

# Ignore files related to API keys
# Ignore environment files
.env

# Ignore SASS config files
.sass-cache

# Ignore all text files
*.txt
```
To add or change your global .gitignore file, run the following command:

```
## Global .gitignore (For All Projects)
To create a global `.gitignore` file (applies to all repositories):
```sh
git config --global core.excludesfile ~/.gitignore_global

```
This will create the file ~/.gitignore_global. Now you can edit that file the same way as a local .gitignore file. All of your Git repositories will ignore the files and folders listed in the global .gitignore file.

## How to Untrack Files Previously Committed from New Gitignore

To untrack a single file, ie stop tracking the file but not delete it from the system use:

```
git rm --cached filename
```
Then, edit `~/.gitignore_global` as you would a local `.gitignore`.

To untrack every file in .gitignore:
## Removing Files from Git Tracking

First, commit any outstanding code changes, and then run:

```
git rm -r --cached
```
If a file was already committed before adding it to `.gitignore`, you need to remove it from tracking:

This removes any changed files from the index(staging area), then run:
- **Untrack a single file** (but keep it locally):
```sh
git rm --cached filename
```

```
git add .
```
Commit it:
- **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
```
git commit -m ".gitignore is now working"
```

To undo ```git rm --cached filename```, use ```git add filename```