Operations 11 min read

Master .gitignore: How to Exclude Files and Folders in Git

This guide explains what a .gitignore file is, how Git tracks files, how to create and populate .gitignore with common patterns, and the steps to stop tracking files that were already committed, providing practical commands and examples for effective version control.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master .gitignore: How to Exclude Files and Folders in Git

What is a .gitignore file?

A .gitignore file is a plain‑text file placed in a Git repository that lists file and directory patterns Git should ignore, preventing them from being tracked or committed.

File statuses in a Git repository

tracked

– files that Git knows about (added with git add and committed). untracked – new files in the working directory that have not been added. ignored – files explicitly excluded via .gitignore; Git does not consider them at all.

Creating a .gitignore file

Typically the file is created in the repository root, but it can exist in any folder. On macOS or Linux you can create it from the terminal: touch .gitignore Hidden files (those beginning with a dot) are not shown by ls unless you use the -a flag:

ls -a

Typical entries to ignore

Operating‑system generated files (e.g., .DS_Store on macOS).

Editor and IDE configuration files.

Compiled artifacts such as .o files.

Dependency directories like node_modules.

Sensitive files containing credentials, e.g., .env.

Runtime logs, e.g., .log files.

Pattern syntax for ignoring files and folders

Specify the path relative to the repository root to ignore a single file: /text.txt Ignore a file in a subdirectory: /test/text.txt Ignore all files with a given name anywhere: text.txt Ignore an entire directory (trailing slash required): test/ Use a wildcard to ignore files by prefix or extension:

img*
*.md

Negate a pattern with an exclamation mark to keep a specific file while ignoring the rest:

# ignore all .md files
*.md
# but keep README.md
!README.md

Note: Negation does not work for files inside a directory that is already ignored.

Removing files that were already committed

Git can only ignore files that are not yet tracked. To stop tracking a file that has already been committed, follow these steps:

# add the file to .gitignore
 echo ".env" >> .gitignore
# remove it from the index without deleting the local copy
 git rm --cached .env
# stage the updated .gitignore
 git add .gitignore
# commit the change
 git commit -m "update ignored files"

Using git status will show the file as no longer part of the repository, while it remains on the local filesystem.

Best practices and conclusion

Create a comprehensive .gitignore at the start of a project to list all files and patterns you never want to commit. Remember that Git only ignores untracked files; to retroactively ignore a tracked file you must remove it from the index as shown above. With these techniques you can keep your repository clean and avoid accidentally publishing sensitive or unnecessary files.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DevOpsGitVersion Controlgit commandsgitignoreignore files
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.