John Sums
John Sums

Reputation: 370

Why should I use .gitignore?

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?

Upvotes: 23

Views: 21033

Answers (7)

Sasha Pachev
Sasha Pachev

Reputation: 5326

Adding each file is not tedious. For example, if you have a bunch of new .java files you can do:

find . -name \*.java | xargs git add

Keeping all possible exceptions in .gitignore would be tedious when you have a lot of them. So .gitignore makes sense when exceptions (patterns of files that should not be checked in) are few, but there are many patterns of files that get checked and new files keep popping up on a regular basis. But when it is the other way, I think life is better without .gitignore.

Upvotes: 1

Sandip
Sandip

Reputation: 29

Yes, To avoid unwanted files to be committed in Remote Repository of GIT. Simply provide appropriate description in .gitignore file like,

if I want to avoid files with extension of pyc file or .class file then, *.pyc *.class

I'll put these two lines in .gitignore file. in future I dont need to bother for these files which will never committed in GIT repo.

Thanks!

Upvotes: 1

ThiefMaster
ThiefMaster

Reputation: 318468

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.

Upvotes: 10

Mik378
Mik378

Reputation: 22171

The benefit of specifying some files into .gitignore file is that you can commit all your tracked files at once without worrying about ignored files added accidentally . It is by far quicker to commit your files using for instance:

git commit -a

instead of adding one by one...

Upvotes: 0

Bruno
Bruno

Reputation: 122599

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.

Upvotes: 20

Jesse the Game
Jesse the Game

Reputation: 2630

In addition to its convenience for using git commit -a, it is a safety guard against accidentally polluting your repository or exposing sensitive information.

Upvotes: 1

Matt Ball
Matt Ball

Reputation: 359776

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

http://git-scm.com/docs/gitignore

Say you run git add . from the root directory of your repo, and you have nothing ignored. You've just added to the index all of those files you were mentally ignoring before. An added benefit is that, with a version-controlled .gitignore, you can make sure that others don't make a similar mistake.

Upvotes: 5

Related Questions