Gitignore

While working on a Git repository, you will often have files and directories that you do not want to commit, so that they are not available to others using the repository.

In some cases, you might not want to commit some of your files to Git due to security reasons.

For example, if you have a config file that stores all of your database credentials and other sensitive information, you should never add it to Git and push it to GitHub as other people will be able to get hold of that sensitive information.

Another case where you may not want to commit a file or directory, is when those files are automatically generated and do not contain source code, so that you don’t clutter your repository. Also, sometimes it makes sense not to commit certain files that contain environment information, so that other people can use your code with their environment files.

To prevent these types of files from being committed, you can create a gitignore file which includes a list of all of the files and directories that should be excluded from your Git repository. In this chapter, you will learn how to do that!

Ignoring a specific file

Let’s have a look at the following example if you had a PHP project and a file called config.php, which stores your database connection string details like username, password, host, etc.

To exclude that file from your git project, you could create a file called .gitignore inside your project’s directory:

  1. touch .gitignore

Then inside that file, all that you need to add is the name of the file that you want to ignore, so the content of the .gitignore file would look like this:

  1. config.php

That way, the next time you run git add . and then run git commit and git push, the config.php file will be ignored and will not be added nor pushed to your Github repository.

That way, you would keep your database credentials safe!

Ignoring a whole directory

In some cases, you might want to ignore a whole folder. For example, if you have a huge node_modules folder, there is no need to add it and commit it to your Git project, as that directory is generated automatically whenever you run npm install.

The same would go for the vendor folder in Laravel. You should not add the vendor folder to your Git project, as all of the content of that folder is generated automatically whenever you run composer install.

So to ignore the vendors and node_modules folders, you could just add them to your .gitignore file:

  1. # Ignored folders
  2. /vendor/
  3. node_modules/

Ignoring a whole directory except for a specific file

Sometimes, you want to ignore a directory except for one or a couple of other files within that directory. It could be that the directory is required for your application to run but the files created is not supposed to be pushed to the remote repository or maybe you want to have a README.md file inside the directory for some purpose. To achieve this, your .gitignore file should like like this:

  1. data/*
  2. !data/README.md

The first line indicates that you want to ignore the data directory and all files inside it. However, the second line provides the instruction that the README.md is an exception.

Take note that the ordering is important in this case. Otherwise, it will not work.

Getting a gitignore file for Laravel

To get a gitignore file for Laravel, you could get the file from [the official Laravel Github repository] here(https://github.com/laravel/laravel/).

The file would look something like this:

  1. /node_modules
  2. /public/hot
  3. /public/storage
  4. /storage/*.key
  5. /vendor
  6. .env
  7. .env.backup
  8. .phpunit.result.cache
  9. Homestead.json
  10. Homestead.yaml
  11. npm-debug.log
  12. yarn-error.log

It essentially includes all of the files and folders that are not needed to get the application up and running.

Using gitignore.io

As the number of frameworks and application grows day by day, it might be hard to keep your .gitignore files up to date or it could be intimidating if you had to search for the correct .gitignore file for every specific framework that you use.

I recently discovered an open-source project called gitignore.io. It is a site and a CLI tool with a huge list of predefined gitignore files for different frameworks.

All that you need to do is visit the site and search for the specific framework that you are using.

For example, let’s search for a .gitignore file for Node.js:

Nodejs gitignore file

Then just hit the Create button and you would instantly get a well documented .gitignore file for your Node.js project, which will look like this:

  1. # Created by https://www.toptal.com/developers/gitignore/api/node
  2. # Edit at https://www.toptal.com/developers/gitignore?templates=node
  3. ### Node ###
  4. # Logs
  5. logs
  6. *.log
  7. npm-debug.log*
  8. yarn-debug.log*
  9. yarn-error.log*
  10. lerna-debug.log*
  11. # Diagnostic reports (https://nodejs.org/api/report.html)
  12. report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
  13. # Runtime data
  14. pids
  15. *.pid
  16. *.seed
  17. *.pid.lock
  18. # Directory for instrumented libs generated by jscoverage/JSCover
  19. lib-cov
  20. # Coverage directory used by tools like istanbul
  21. coverage
  22. *.lcov
  23. # nyc test coverage
  24. .nyc_output
  25. # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
  26. .grunt
  27. # Bower dependency directory (https://bower.io/)
  28. bower_components
  29. # node-waf configuration
  30. .lock-wscript
  31. # Compiled binary addons (https://nodejs.org/api/addons.html)
  32. build/Release
  33. # Dependency directories
  34. node_modules/
  35. jspm_packages/
  36. # TypeScript v1 declaration files
  37. typings/
  38. # TypeScript cache
  39. *.tsbuildinfo
  40. # Optional npm cache directory
  41. .npm
  42. # Optional eslint cache
  43. .eslintcache
  44. # Microbundle cache
  45. .rpt2_cache/
  46. .rts2_cache_cjs/
  47. .rts2_cache_es/
  48. .rts2_cache_umd/
  49. # Optional REPL history
  50. .node_repl_history
  51. # Output of 'npm pack'
  52. *.tgz
  53. # Yarn Integrity file
  54. .yarn-integrity
  55. # dotenv environment variables file
  56. .env
  57. .env.test
  58. # parcel-bundler cache (https://parceljs.org/)
  59. .cache
  60. # Next.js build output
  61. .next
  62. # Nuxt.js build / generate output
  63. .nuxt
  64. dist
  65. # Gatsby files
  66. .cache/
  67. # Comment in the public line in if your project uses Gatsby and not Next.js
  68. # https://nextjs.org/blog/next-9-1#public-directory-support
  69. # public
  70. # vuepress build output
  71. .vuepress/dist
  72. # Serverless directories
  73. .serverless/
  74. # FuseBox cache
  75. .fusebox/
  76. # DynamoDB Local files
  77. .dynamodb/
  78. # TernJS port file
  79. .tern-port
  80. # Stores VSCode versions used for testing VSCode extensions
  81. .vscode-test
  82. # End of https://www.toptal.com/developers/gitignore/api/node

Using gitignore.io CLI

If you are a fan of the command-line, the gitignore.io project offers a CLI version as well.

To get it installed on Linux, just run the following command:

  1. git config --global alias.ignore \
  2. '!gi() { curl -sL https://www.toptal.com/developers/gitignore/api/$@ ;}; gi'

If you are using a different OS, I would recommend checking out the documentation here on how to get it installed for your specific Shell or OS.

Once you have the gi command installed, you could list all of the available .gitignore files from gitignore.io by running the following command:

  1. gi list

For example, if you quickly needed a .gitignore file for Laravel, you could just run:

  1. gi laravel

And you would get a response back with a well-documented Laravel .gitignore file:

  1. # Created by https://www.toptal.com/developers/gitignore/api/laravel
  2. # Edit at https://www.toptal.com/developers/gitignore?templates=laravel
  3. ### Laravel ###
  4. /vendor/
  5. node_modules/
  6. npm-debug.log
  7. yarn-error.log
  8. # Laravel 4 specific
  9. bootstrap/compiled.php
  10. app/storage/
  11. # Laravel 5 & Lumen specific
  12. public/storage
  13. public/hot
  14. # Laravel 5 & Lumen specific with changed public path
  15. public_html/storage
  16. public_html/hot
  17. storage/*.key
  18. .env
  19. Homestead.yaml
  20. Homestead.json
  21. /.vagrant
  22. .phpunit.result.cache
  23. # Laravel IDE helper
  24. *.meta.*
  25. _ide_*
  26. # End of https://www.toptal.com/developers/gitignore/api/laravel

Conclusion

Having a gitignore file is essential, it is great that you could use a tool like the gitignore.io to generate your gitignore file automatically, depending on your project!

If you like the gitignore.io project, make sure to check out and contribute to the project here.