How do I ignore a full directory in .gitignore?
Ah, ignoring a directory in .gitignore
is a piece of cake! All you have to do is add the directory’s name followed by a forward slash (/
) in your .gitignore
file. For example, let’s say you have a directory named node_modules
that you want to ignore:
Just open up your .gitignore
file and add the following line:
node_modules/
That trailing slash is important; it tells Git to ignore the whole directory rather than just a file named node_modules
.
Save the file, and you’re good to go! Git will now ignore the entire node_modules
directory, keeping it out of your commits.
Can I ignore “node_modules/” no matter where it shows up in the code base?
Absolutely, you can ignore node_modules/
directories wherever they pop up in your codebase. To do that, you don’t need the trailing slash or any leading slashes. Just write it as:
node_modules
Add this line to your .gitignore
file and it’ll ignore every node_modules
directory, no matter where it’s located in your project. Simple as that!
This way, you don’t have to worry about accidentally committing all those third-party libraries. Keeps your repo nice and clean!
Do we ever use a wild card like *
in .gitignore?
Oh, for sure! Wildcards like *
can be super useful in .gitignore
files. They help you match multiple files or directories with similar names or extensions. Here are some quick examples to give you an idea:
*.log
: This will ignore all files with the.log
extension, no matter where they’re located.*.tmp*
: This will ignore all files that have.tmp
anywhere in their names (likefile.tmp
,file.tmp123
, etc.).debug/
: Ignores all files in thedebug/
directory.debug*
: Ignores all files and folders that start with the worddebug
.
You can get even fancier by combining wildcards:
**/logs
: This will ignore alllogs
directories no matter where they are in your folder hierarchy. The**
means any number of directories.
So, yes, wildcards can be your best friend in .gitignore
when used wisely. They can help you cover a lot of ground with just a few lines.
