.gitignore for Beginners


.gitignore for Beginners Author: Mohammad Amaan Abbasi | Aug. 28, 2018

In this article, I want to discuss about **.gitignore**.

If you use github then you probably have seen .gitignore on many of the projects.

What is the use of .gitignore? What should one put into .gitingore? These are the two questions that many beginners might have.

We're going to explore these two questions and at last we will see a sample .gitignore file that I use for my projects.

Getting started


Sometimes there are going to be files that you don't want to push to the server for everyone to see.

These could be files that have credentials(like database information) or files that are not necessary for your application to run or files that are generated by the OS (for example .DS_store, thumbs.db).

git has a way to ignore files and that is .gitignore.

One should manually create the .gitignore file in the root of the project.

.gitignore contains names, expressions and directory paths of the files that you want to disregard.

That mean you can type in the exact file name or use a expression for similiar files, and you can also .gitignore a directory.

Now, we know what is .gitignore and what it does. The next question comes is, what files should be ignored?

A file should be ignored if any of the following is true:

  • A file that us not required for other users.
  • A file that is not required for your project to run.
  • A file that is generated by a process.

Note: This is a general rule of thumb.

Examples

  • media files (images, videos), you should ignore these kinds of files, as they can take a lot of space which can put it over your storage limit and also the repository expects you to save code in it and not media files.

  • Databases (for example, db.sqlite).

  • Private information like database passwords and API keys .

  • Other files like *.pyc, __pycache__ , .env should be ignored.

I recommend that you check the complete list here .

For my django projects, Most of the time I normally put the following files in .gitignore .

.gitignore example

  *.pyc
  db.sqlite3
 _pycache_
  .DS_Store
  .venv36
  migrations
  media

I hope it helped you, do check out this site for more examples.

If you have any suggestions please put them down in the comments.