244

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?

0

8 Answers 8

208

.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.

Sign up to request clarification or add additional context in comments.

6 Comments

So if you create a .gitignore in a new repository on github.com, and then do a git init and git add . on your local computer -- not yet connected to .gitignore as it's not yet connected to that repository -- how does this work? Does it take effect when I perform a git commit? Or?
what is _._gitignore used for? is it used only when the gitignore is in a sub Dir.?
What does ignore mean? Who/what/when does the ignore? I know what the word ignore means but I don't find a clear description of what ignore means in the context of Git. Obviously the term is totally understandable by people that understand it but anything written to define something for people that don't already know it needs to explain all the terms that have definitions unique to the context.
@user34660 Ignore means just that - Git will not track changes in the files specified by (the patterns) in the .gitignore file. Even if you change them, git won't show them as modified.
Then what does track mean? You must understand that a beginner might not know if track applies to before/after a commit or before/after a push or to something else. Terms that have significance only in the relevant context need to be defined/explained. I pushed a project to GitHub and got some files that I thought should not go. I am not asking for help with that except it would help me to be certain of what is supposed to happen so I can be sure that what did happen was not supposed to (something is broken) so then I know I need to fix it.
|
48

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: https://git-scm.com/docs/gitignore

2 Comments

Your .DS_Store scenario finally resonated with me as I guess I wasn't cluing on to this concept because I had not yet needed to use it in a project. Thank you. I ended up using this as a starting point as I use all different operating systems noting specifically the # OS generated files # section.
I know I am late to this thread, but thank you @AndyLester! I am a novice when it comes to programming and Github. I find many of the answers written to address this type of question are filled with terms the novice might not understand (i.e., compilation products, temporary files IDEs create....what the h@ll are these things!). However, your Mac description is perfect in its elegant simplicity!
28

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

1 Comment

I believe you can use .gitattributes to ignore files from diff/status.
11

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

Comments

7

Main purpose of .gitignore:

  • I'm telling git: "Git, do not upload any files I list in my .gitignore file:"

Use case

  • e.g. unencrypted private keys- I definitely do not want upload unencrypted keys to Github. This is a good time to add those files to your .gitignore file. This means they will never be "checked in".

  • I have a lot of personal notes / scribbles in certain open source 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 reads it. The good thing is that I can ask git to "ignore" those files.

In my case, since I don't want to pollute the open source repository with my .gitignore files, I add it to a global .gitignore file.

The only cost to this approach is that I will not be able to recover those notes if my computer crashes etc.

Comments

4

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

1 Comment

Hello and Welcome to StackOverflow. Your answers is precise, concise and to the point. Links to the official documentation may improve it even more.
0

The main purpose of a .gitignore file is to keep your Git repository clean and efficient by excluding unnecessary files, such as compiled binaries, build logs, and local configuration files. By excluding these files, you reduce the repository’s storage footprint and help maintain consistency across different environments. Another advantage is that since ignored files are treated by Git as if they would not exist and are not even shown as "untracked" by git status.

.gitignore is an optional text file, typically added to the Git project working directory, that specifies which files Git should ignore. Each line contains a glob pattern that specifies which files Git should ignore.

A caveat: Git applies .gitignore rules only on untracked files, to ignore tracked files you need to first untrack them (see: How do I make Git forget about a file that was tracked, but is now in .gitignore?).

Here’s a collection of useful .gitignore templates: https://github.com/github/gitignore and a comprehensive guide.

Comments

-2

The git ignore file rule allows you to ignore a file you've committed in the past. You use it when you do not want to recommit a file. basically It's a list of files you want git to ignore in your work directory.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.