What is .gitignore?

GitGithubGitignore

Git Problem Overview


I just created a Github repository and was wondering what the .gitignore file was for. I started by not creating one, but added one due to the fact that most repositories have one. Do I need to have one? Can/do I just ignore it, or does it have a use?

Git Solutions


Solution 1 - Git

.gitignore tells git which files (or patterns) it should ignore. It's usually used to avoid committing transient files from your working directory that aren't useful to other collaborators, such as compilation products, temporary files IDEs create, etc.

You can find the full details here.

Solution 2 - Git

It's a list of files you want git to ignore in your work directory.

Say you're on a Mac and you have .DS_Store files in all your directories. You want git to ignore them, so you add .DS_Store as a line in .gitignore. And so on.

The git docs will tell you all you need to know: http://git-scm.com/docs/gitignore

Solution 3 - Git

When you are doing a commit you do not want accidentally include temporary files or build specific folders. Hence use a .gitignore listing out items you want to ignore from committing.

Also, importantly git status is one of the most frequently used command where you want git status to list out the files that have been modified.

You would want your git status list look clean from unwanted files. For instance, I changed a.cpp, b.cpp, c.cpp, d.cpp & e.cpp I want my git status to list the following:

git status
a.cpp
b.cpp
c.cpp
d.cpp
e.cpp

I dont want git status to list out changed files like this with the intermediary object files & files from the build folder

git status
a.cpp
b.cpp
c.cpp
d.cpp
e.cpp
.DS_Store
/build/program.o
/build/program.cmake

Hence, to get myself free from git status to list out these intermediate temporary files & accidentally committing them into the repo, I should create a .gitignore which everyone does. All I need to do list out the files & folders in the .gitignore that I want to exclude from committing.

Following is my .gitignore to avoid committing unnecessary files

/*.cmake
/*.DS_Store
/.user
/build

Solution 4 - Git

There are files you don't want Git to check in to. Git sees every file in your working copy as one of three things:

  1. tracked - a file which has been previously staged or committed;
  2. untracked - a file which has not been staged or committed; or
  3. ignored - a file which Git has been explicitly told to ignore.

Ignored files are usually built artifacts and machine-generated files that can be derived from your repository source or should otherwise not be committed. Some common examples are:

  • dependency caches, such as the contents of /node_modules or /packages
  • compiled code, such as .o, .pyc, and .class files
  • build output directories, such as /bin, /out, or /target
  • files generated at runtime, such as .log, .lock, or .tmp
  • hidden system files, such as .DS_Store or Thumbs.db
  • personal IDE config files, such as .idea/workspace.xml

Ignored files are tracked in a special file named .gitignore that is checked in at the root of your repository. There is no explicit git ignore command: instead the .gitignore file must be edited and committed by hand when you have new files that you wish to ignore. .gitignore files contain patterns that are matched against file names in your repository to determine whether or not they should be ignored. Here is a sample.gitignore file. For more details look at this link

Solution 5 - Git

Main purpose of .gitignore – so you avoid adding irrelevant files etc. Git

I have a lot of personal notes / scribbles in certain repositories: they are useful to me but not for anyone else. I would not want it uploaded to github because it would simply confuse everyone who read it. The good thing is that I can ask git to "ignore" those files. The only cost to this approach is that I will not be able to recover those notes if my computer crashes etc.

Solution 6 - Git

The .gitignore file is a text file that instructs Git to ignore certain files or folders in a project. A local .gitignore file is normally kept in the project's root directory. You can also create a global .gitignore file, which will be ignored in all of your Git repositories if any entries in it are found. To create a local .gitignore file, create a text file and name it .gitignore (remember to include the . at the beginning). Then edit this file as needed. Each new line should list an additional file or folder that you want Git to ignore.

The entries in this file can also follow a matching pattern.

* is used as a wildcard match
/ is used to ignore pathnames relative to the .gitignore file
# is used to add comments to a .gitignore file

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaposView Question on Stackoverflow
Solution 1 - GitMureinikView Answer on Stackoverflow
Solution 2 - GitAndy LesterView Answer on Stackoverflow
Solution 3 - GitTheWaterProgrammerView Answer on Stackoverflow
Solution 4 - GitimkView Answer on Stackoverflow
Solution 5 - GitBenKoshyView Answer on Stackoverflow
Solution 6 - GitTranquilleView Answer on Stackoverflow