How to Ignore Git Folders and Directories .gitignore |Video upload date:  · Duration: PT4M41S  · Language: EN

Quick guide to ignoring folders and directories with .gitignore rules and how to untrack already committed folders for cleaner repositories

Yes you can tell Git to stop paying attention to your messy build folders and temporary trash. It will not hold a grudge but it will keep ignoring those files until you change your mind. This short guide shows how to create a .gitignore file for ignore folders and ignore directories and how to remove already tracked folders from the repository index with a few polite commands.

What a .gitignore actually does

A .gitignore file lists patterns that tell Git to ignore matching files for future commits. If a file is already tracked then adding it to .gitignore will not magically untrack it. You will need to remove the file from the index first and then commit that change.

Common patterns that save your sanity

  • node_modules/ ignore a whole dependency tree
  • build/ or dist/ ignore compiled output
  • *.log ignore log files that always blame you
  • !.gitkeep or !docs/keep.md unignore a needed file inside an ignored folder

Step by step to ignore folders and remove tracked files

Follow these steps to add ignore rules and stop Git from tracking specific directories. It is quick and mildly satisfying.

  1. Create a file named .gitignore at the repository root if you do not already have one
  2. Add directory names with a trailing slash to target folders only for example node_modules/ or build/
  3. Use wildcards to match patterns for example *.log or dist/
  4. Use negation to allow a single file inside an ignored folder for example !docs/keep.md

Remove already tracked folders from the index

If a folder was committed before the .gitignore rule was added you must remove the tracked copy from the index. These commands remove tracking without deleting the files from your working directory.

git rm -r --cached foldername
# or remove multiple at once
git rm -r --cached .

After running the removal add the changes and commit them so the remote repo stops tracking new files that match your ignore rules.

git add .
git commit -m "Update .gitignore and untrack folders"
git push

Quick checks and useful commands

  • See which files are currently ignored with git ls-files -i --exclude-standard
  • Check which rule matches a file with git check-ignore -v path/to/file
  • Confirm status with git status before you push anything dramatic

Global ignores for OS and editor noise

If you are tired of adding the same OS or editor junk to every repo set a global ignore file and forget about the problem forever.

git config --global core.excludesfile ~/.gitignore_global
# then edit ~/.gitignore_global and add patterns like
# Thumbs.db
# .DS_Store
# *.swp

Parting tips for the mildly paranoid

  • Remember that .gitignore only affects untracked files so remove tracked files first if needed
  • Keep .gitignore in the repository root for repo wide rules and use nested .gitignore files for folder specific exceptions
  • Use explicit patterns rather than trying to guess so you do not accidentally ignore important files

There you go. Your repo will now behave better and your commits will stop including temporary garbage. You can get back to whatever heroic bug fixing you were doing before Git became the trash collector.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.