Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I use .gitignore?

Tags:

git

Currently, whenever I create a file that I'd like Git to track I simply add it to the index. If I don't add it to the index Git would not "see it". So why should I use a .gitignore file, as opposed to just not adding it to the index?

like image 763
John Sums Avatar asked Dec 02 '12 23:12

John Sums


People also ask

What is purpose of using Gitignore?

The purpose of gitignore files is to ensure that certain files not tracked by Git remain untracked. To stop tracking a file that is currently tracked, use git rm --cached.

Is Gitignore required?

No, it is not mandatory. . gitignore is used to make sure nobody using the repository is committing files that are "forbidden" (i.e. that should never be a part of a commit).

What should you Gitignore?

gitignore should list the names or name-patterns of files that will be found in work-trees when working with your project, but that should not be committed to the project. In other words, it's not OS-specific, it's project-specific.

Should I include Gitignore file?

Normally yes, . gitignore is useful for everyone who wants to work with the repository. On occasion you'll want to ignore more private things (maybe you often create LOG or something. In those cases you probably don't want to force that on anyone else.


2 Answers

You'll generally find that adding each and every file one by one can become tedious when your project becomes bigger.

In Java, you may end up with many .class files that you wouldn't want to add to your repository. Similarly, you may end up with many .o files in C, or .pyc in Python (for example).

Having patterns like *.class, *.o, *.pyc in your .gitignore files allows you to ignore those files once and for all. Subsequent git status runs (or similar if you're using a GUI) will not tell you these are new, untracked files, thereby letting you focus on the files that are really new and noticeably untracked.

This can also be useful if you add an entire directory (e.g. git add myproject): this lets you ignore a category of files altogether.

like image 170
Bruno Avatar answered Sep 20 '22 06:09

Bruno


It hides the file from git status. Especially with a lot of generated files you really don't want them to show up in there all the time. It also prevents you from accidentally adding them e.g. when doing git add somefolder.

The purpose of gitignore files is to ensure that certain files not tracked by git remain untracked.

like image 25
ThiefMaster Avatar answered Sep 19 '22 06:09

ThiefMaster