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.
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.
Any private data should never accidently be committed or pushed to a git repo, and the best way to do that is by having git ignore them.
Examples of files often added to a .gitignore are:
.env files for project environment variables. These files often include sensitive data like: - API keys, which are private permission keys that allow 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 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.
Irrelevant files like: - .DS_Store (which locally stores mac Finder UI preferences) - .vs/ (Visual Studio cache/options directory)
Common Mistakes / Misconceptions
“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.
Guided Practice
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 an untracked in git. Don’t add or commit anything yet, though.
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!
Independent Practice
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.
Check for Understanding
Form small groups and discuss:
What sorts of files should you add to your .gitignore?
How can add to 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?