.gitignore file is a text file that tells Git which files or directories to ignore in a project.
This can be especially useful in a coding project where you may want to ignore files or directories that are created by an IDE or by build and runtime artifacts.
How to Add a .gitignore File
Navigate to your local git repository. From the terminal, you can use the
cd (change directory) command to get to your repository.
Once in the directory, use a text editor to create and open a new file named
Simply type the files or file extensions you want to exclude from version control.
Each file or directory should be on a new line.
For example, if you wanted to ignore a directory named
node_modules and a file named
.gitignore file would look like this:
Remember to save your changes and close the file when you’re done.
If you want to ignore all files with a specific extension, just add *[your_extension] to .gitignore.
This ignores all .txt and. png files.
Then, remember to commit the file to the repository:
git add .gitignore git commit -m "Added .gitignore"
If you want to commit existing files that have previously been tracked by Git but no longer wish to track those, make sure to read this guide.
How to View a .gitignore File
.gitignore file is just a text file, so you can view it in any text editor. But depending on your OS settings, it might not be visible in your project folder.
For example, to view it in the terminal, navigate to your repository and use the
cd /path/to/your/repository cat .gitignore
What to Include in .gitignore?
GitHub maintains a repository of useful .gitignore templates for different types of projects.
These are your ready-made .gitignore files that can be a great addition to your specific project. Instead of having to come up with what to add to .gitignore, you can rely on these templates.
- Visit the GitHub .gitignore templates repository.
- You will see a list of .gitignore template files for different programming languages, frameworks, and tools. Click on the one that matches your project type.
- You’ll see the contents of the .gitignore file. You can copy these contents and paste them into your project’s
.gitignorefile. Alternatively, you can download the file directly into your repository by clicking on the “Raw” button, right-clicking on the page, and selecting “Save As”.
Remember to adapt these templates as needed, as your project may not need to ignore all of the same files or directories.
Thanks for reading. Happy versioning!