- Why is it important to have a .gitignore file in your project folder?
Here are links to lessons that should be completed before this lesson:
Adding certain files to .gitignore will prevent staging and committing those files in Git, protecting your private info like keys, passwords, and other secrets. It's also useful to exclude very large or locally generated files from being saved unnecessarily.
Participants will be able to:
- Add unwanted files to their .gitignore
- Know what data to protect from public view
- Know which files don't need to be committed
- Creating a .gitignore at your project root
- Choosing which files to ignore
- Ignoring files - GitHub
- Learning how to use gitignore - Medium
- A collection of useful .gitignore templates - Visual Studio template
- A collection of useful .gitignore templates - NodeJS template
Gitignore (Youtube video)
.gitignore (slides)
When you commit your project data to a version control site like GitHub or Bitbucket, unless you are paying for a private account, all of that data is publicly accessible to anyone.
Private data should never accidentally be committed or pushed to a Git repo, and the best way to do that is by having Git ignore them with a .gitignore file.
Examples of files often added to a .gitignore are:
- .env files for a project, also known as environment variables. These files often include sensitive data like:
- API keys: Private permission keys that let you make a limited number of requests for data from sites like AllRecipes or GoogleMaps
- Database URLs: All user authorization IDs and URLs, which you would need to set up OAuth, Okta, Auth0, etc.
- Locally compiled, large folders that can easily be rebuilt such as:
- node_modules (made from package.json when you do
npm install
) - .cache - build (made by webpack)
- If a file appears as a muted color in your IDE file tree, it is probably locally compiled.
- node_modules (made from package.json when you do
- Irrelevant files like:
- .DS_Store (which locally stores Mac Finder UI preferences)
- .vs/ (Visual Studio cache/options directory)
"I will just remember what not to commit."
- Why make more work for yourself? Tell Git to forget about it once; now you and other contributors won't have a problem for the rest of your project, and your Git dialogue will be cleaner.
- Don't leave it to chance! You will be distracted at some point.
-
On your command line, navigate to your project's root folder.
-
Enter:
touch .gitignore
- Next, enter:
touch .my-secret-keys
git status
You should see both files as untracked in Git. Don't add or commit anything yet, though.
- Open .gitignore in your text editor:
code .gitignore
- Add this text to your file:
# Project Secrets
.my-secret-keys
# Project-generated files
package-lock.json
# Locally-generated files
.DS_Store
-
You should see the text of your ignored files darken or lighten in your IDE's tree view.
-
Go back you your command line and type:
git status
You should no longer see .my-secret-keys in your untracked files because Git is ignoring it!
-
Add 2 more files to your project's .gitignore using what you've learned, then type git status in your command line to check that they are no longer tracked.
-
Add, commit, & push your new .gitignore file to your Git repo.
-
Find an example of a wildcard entry in the links under Supplemental Materials.
- What sorts of files should you add to your .gitignore?
- How can you add all files of the same type to .gitignore?
- How can you add a folder?
- If you want to add all files in the folder except one, how could you do that?
- Should you commit your .gitignore file?