1030

I just did a git init on the root of my new project.

Then I created a .gitignore file.

Now, when I type git status, .gitignore file appears in the list of untracked files. Why is that?

reducing activity
  • 1,985
  • 2
  • 36
  • 64
Jacques René Mesrine
  • 46,127
  • 27
  • 66
  • 104
  • 14
    `git add self && git commit -m "-1 for reverting existential depression" && git remote rm HEAD` – Alastair Jan 17 '13 at 12:44
  • 1
    Can I ignore the .git/ folder and put it in ".gitignore"? – Timo Mar 25 '15 at 07:59
  • 2
    You could create a global "gitignore" in your home folder under Linux and store it there: git config --global core.excludesfile ~/.gitignore_global – Timo Mar 25 '15 at 08:01
  • 23
    I came here by searching `how to gitignore .gitinore file` and the question and accepted answer aren't really related to title. Title could be improve. – m.rufca Jun 12 '15 at 18:34
  • @matheusrufca: looks like the answer is that you shouldn't ignore the `.gitignore` file. Why do you want it? – Nick Volynkin Sep 04 '15 at 13:53
  • 7
    There are use cases for ignoring .gitignore. My team's workflow requires me to change a set of files for my local dev environment but they should not be committed. The workflow could be improved with better architecture but it's out of my hands. In the meantime, having git pay attention to those files is a liability. Thus I want to gitignore them but only locally, and I don't want to risk committing my .gitignore as it should not be shared with the team. – Mark Dec 18 '15 at 19:37
  • Add .gitignore in the .gitignore file – cjsimon Jan 02 '18 at 13:13
  • `Add .gitignore in the .gitignore file` @cjsimon - doesn't work. – Drey Mar 23 '19 at 04:21
  • you can use `git rm --cached .gitignore` and untracking **.gitignore** – hassanzadeh.sd Jan 13 '20 at 13:32

19 Answers19

1029

The .gitignore file should be in your repository, so it should indeed be added and committed in, as git status suggests. It has to be a part of the repository tree, so that changes to it can be merged and so on.

So, add it to your repository, it should not be gitignored.

If you really want you can add .gitignore to the .gitignore file if you don't want it to be committed. However, in that case it's probably better to add the ignores to .git/info/exclude, a special checkout-local file that works just like .gitignore but does not show up in "git status" since it's in the .git folder.

See also https://help.github.com/articles/ignoring-files

AdrieanKhisbe
  • 3,899
  • 8
  • 37
  • 45
August Lilleaas
  • 54,010
  • 13
  • 102
  • 111
  • 17
    Shouldn't this be part of the repository's metadata rather than a file that is tracked? – endolith Mar 12 '10 at 03:38
  • 13
    The repository metadata is local to the repository. If you add a commit hook to your repo, and someone clone your repo, they won't get the commit hook, for example. – August Lilleaas Mar 13 '10 at 06:51
  • 93
    @wukong, if you're working on a team, shouldn't everyone should be ignoring the same set of files? That's why the .gitignore file gets added to the repository. No one says you have to deploy it as part of your project. – Ryan Lundy Sep 24 '11 at 23:02
  • 14
    @endolith and wukong It doesn't have to be a file in your repo. You can have your ignore settings in many different places. GitHub has a great article on it http://help.github.com/ignore-files/ You can have global ignore settings anywhere, and you can have repo specific settings in the .git metadata for the repo. – Boushley Dec 22 '11 at 02:46
  • 2
    @Boushley Isn't it dangerous practice to do this, if half a team end up ignoring files that another half is actually working on (and the project comes to depend on)? – deed02392 Aug 05 '13 at 14:28
  • 3
    A global ignore file is very nice for files that are local to that particular environment. For example, a .DS_Store file is specific to Finder.app on OS X and does not really belong in the .gitignore file of the project. Same goes with eclipse and intellij files, unless of course there's a rule that everyone working on the project use a particular IDE. – August Lilleaas Aug 05 '13 at 16:52
  • 7
    @deed02392 In using these ignore files you certainly need to use judgement in what you put into them, but they still have some great uses. For example I use Vim and so in my global gitignore I have *.swp files marked as ignored. This way I don't have to add it to each of my projects, and guys who don't ever use vim don't have to worry about it. – Boushley Aug 07 '13 at 03:30
  • 1
    @Kyralessa well I'm using git-svn while the rest of the team is using svn, so its useful for me to actually ignore .gitignore, probably there are other use cases as well – Moataz Elmasry Sep 19 '13 at 12:35
  • 3
    For git-svn, I don't have a gitignore file. I use .git/info/excludes, which is a repository local file that won't show up in "git status", but it works just like gitignore. – August Lilleaas Sep 20 '13 at 11:49
  • 1
    So if you use `.git/info/exclude` instead of `.gitignore`, then others who clone your repo won't ignore those same files? – trusktr May 15 '14 at 04:25
  • Exactly. It's useful for files that you want to have floating about but don't have a better folder for than the git repo :) I some times have an "etc" folder that I ignore in `.git/info/excludes`, where I put various documents, downloads, etc I don't want to check in. For git-svn it also works well, unless you want to check a .gitignore file into an svn repo :) – August Lilleaas May 15 '14 at 12:29
  • @endolith and others, if you want ignored files to apply to only your repo, and not those of your collaborators, use `.git/info/exclude`. – Paul Draper May 25 '14 at 23:03
  • it is worth mentioning that adding '.gitignore' to the .gitignore file does not actually cause it to be ignored, and it still shows up in git status – Eric Fitting Oct 22 '15 at 17:15
  • 2
    I work in a team, and while we do want to ignore the same set of core patterns, I have been asked to use a global .gitignore for e.g. the pattern `*.swp`. This is created by Vim, which only some of my team (including me) use for writing code. – Medlock Perlman Mar 24 '16 at 10:38
  • I find useful to have convention what to ignore, create initial .gitignore file, and than "seal" it by adding .gitignore to itself. That way you are safe from the accidents, but you can change it if you really want, by "unsealing", updating and "sealing" it again. Everybody can manage it locally if needed, but without committing changes. – Saša Aug 30 '16 at 08:56
  • 5
    My team and I have found that it's best to put *project* related things in the `.gitignore` file and *dev/env/machine* related files in the `.git/info/exclude` file. This way we don't bloat up the gitignore list with things that may be relevant to only one person because he/she has chosen a specific dev setup. Concrete example: the `.DS_Store` files are only relevant to me because I'm using macOS – s g Nov 10 '16 at 19:08
  • adding '.gitignore' to the .gitignore file is just the short answer that is needed. – kenchew Jun 20 '20 at 06:50
  • To summarize, `.gitignore` is used to ignore files for all users, while `.git/info/exclude` is used to ignore files only for the current user – SilleBille Apr 14 '23 at 04:55
293

If you want to store the list of ignored files outside of your Git tree, you can use the .git/info/exclude file. It is applied only to your checkout of the repo.

Earl Zedd
  • 1,101
  • 10
  • 11
Paweł Hajdan
  • 18,074
  • 9
  • 49
  • 65
  • 20
    +1, this is great for ignores that aren't project related, such as emacs *~ backup files, .DS_Store from OS X and so on. – August Lilleaas May 19 '10 at 07:48
  • 40
    @AugustLilleaas I personally prefer to put these types of {editor,platform}-specific files in `~/.gitignore` so they're ignored for any repository I work on. – Michael Mior Nov 17 '11 at 14:32
  • 23
    Once a file is tracked, you can use `git update-index --assume-unchanged ` to stop tracking changes without changing your repo. This is very useful on large shared projects where you need to make local changes, but nobody else wants to see your stuff committed to the repo. See [blog.pagebakers.nl](http://blog.pagebakers.nl/2009/01/29/git-ignoring-changes-in-tracked-files/) – Chris Esplin May 22 '12 at 16:21
  • 6
    @AugustLilleaas: [Per-user gitignore](http://stackoverflow.com/questions/767147/how-do-i-tell-git-to-ignore-gitignore/974188#974188) is better for that use-case. – Mechanical snail Nov 21 '12 at 04:55
  • 3
    thanks for this tip, i'm using git-svn so the other users of the svn repo on the server wouldn't exactly want .gitignore checked in. – enorl76 Mar 13 '13 at 03:20
  • This is particularly helpful when you are using git svn because other contributors on your team may not want to see *anything* related to git in the subversion repository. – jfmercer Mar 31 '15 at 12:26
  • The .git directory is in the base of your repository, not necessarily the working directory. – emery Oct 30 '15 at 08:07
84

You could actually put a line .gitignore into your .gitignore file. This would cause the .gitignore file to be ignored by git. I do not actually think this is a good idea. I think the ignore file should be version controlled and tracked. I'm just putting this out there for completeness.

Nexaspx
  • 371
  • 4
  • 20
1800 INFORMATION
  • 131,367
  • 29
  • 160
  • 239
  • 7
    Worked for me! version 1.5.6.5. I also agree with 1800 INFORMATION that its not a good idea, but I think it could be okay in certain contexts (say you use a git-svn repository, and you don't want git-ish files to go to svn). The excludes file is probably better. – J. Polfer Aug 03 '10 at 22:14
  • 4
    @ehsanul - the file must not be tracked (you should not have added or commited it). You can untrack it. This is probably not a great idea in a git-only environment, but if you happen e.g. to use git as a smart client for a subversion repository (without the rest knowing, ) - such a trick is great. – Tomasz Gandor Oct 21 '14 at 13:16
  • 2
    @IshanSrivastava You probably have the file tracked already. Try running `git rm --cached .gitignore` – Gideon Apr 09 '18 at 11:38
  • This solution works for me as I just need to ignore changes in my repo. The actual commit is inside subfolders (projects). – Incredible May 31 '22 at 14:12
58

You can also have a global user git .gitignore file that will apply automatically to all your repos. This is useful for IDE and editor files (e.g. swp and *~ files for Vim). Change directory locations to suit your OS.

  1. Add to your ~/.gitconfig file:

    [core]
    excludesfile = /home/username/.gitignore
    
  2. Create a ~/.gitignore file with file patterns to be ignored.

  3. Save your dot files in another repo so you have a backup (optional).

Any time you copy, init or clone a repo, your global gitignore file will be used as well.

Pang
  • 9,564
  • 146
  • 81
  • 122
Alec the Geek
  • 1,970
  • 14
  • 14
  • 6
    I believe this is the best solution for situations where your editors leave behind temp files, e.g. .*.swp (VIM) and ._* (TM), since it wouldn't make sense to continuously add these rules to every git repo, and to force other users with different IDE's to check for these files. – Thomas Hunter II May 31 '11 at 14:45
  • 2
    This works brilliantly for ignores that shouldn't be pushed to any branch. Replace 'username' with your actual username of course, and don't add a second [core] section to .gitconfig if you already have one - just place the excludesfile line under your existing [core] section. – emery Dec 03 '15 at 17:58
57

If someone has already added a .gitignore to your repo, but you want to make some changes to it and have those changes ignored do the following:

git update-index --assume-unchanged .gitignore

Source.

Leif Gruenwoldt
  • 13,561
  • 5
  • 60
  • 64
  • 5
    Bad idea, there is a reason `.git/info/excludes` exists. – Arrowmaster Feb 09 '11 at 22:25
  • 8
    I presume there's a reason `--assume-unchanged` exists too. Why is one better than the other? – Leif Gruenwoldt Feb 10 '11 at 18:58
  • 10
    And btw `.git/info/excludes` does not work if the file is already tracked. – Leif Gruenwoldt Feb 10 '11 at 19:00
  • 1
    This really helped me with a .gitignore that was already committed and for which I did not wish to commit changes. I'm running git 1.7.4.1 from Ubuntu 11.04 repos and the help pages add this in update-index. "This option can be also used as a coarse file-level mechanism to ignore uncommitted changes in tracked files (akin to what .gitignore does for untracked files). Git will fail (gracefully) in case it needs to modify this file in the index e.g. when merging in a commit; thus, in case the assumed-untracked file is changed upstream, you will need to handle the situation manually." – YonahW May 10 '11 at 00:51
  • I vote for this answer 'cause generally you'll need this action for your master repo initial state and keep the benefits of git functionality. There is no good reason to ignore this file (period) – Lazaros Kosmidis Jan 05 '16 at 10:33
  • Just for completeness purposes, how can you revert this action? `--no-assume-unchanged` is the right way – Felipe Quirós Jun 04 '19 at 21:47
  • This is helpful if the file is already tracked and exists on remote. From what I have found so far, this is the only command that, when used together with `.git/info/excludes`, ignores any file and at the same time does not require an extra commit. – DemiA Dec 22 '21 at 07:31
37

After you add the .gitignore file and commit it, it will no longer show up in the "untracked files" list.

git add .gitignore
git commit -m "add .gitignore file"
git status
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
20

Just incase someone else has the same pain we had. We wanted to exclude a file that had already been committed.

This post was way more useful: working with .git/info/exclude too late

Specifically what you need to ignore a file is actually use the command git remove See git rm (http://www.kernel.org/pub/software/scm/git/docs/git-rm.html)

you test it by going

git rm --dry-run *.log
(if you say wanted to exclude all the log files)

this will output what would be excluded if you ran it.

then

you run it by going

git rm *.log
(or whatever filename path / expression you want to)

Then add a *.log line to your .gitignore file.

Community
  • 1
  • 1
Evolve
  • 8,939
  • 12
  • 51
  • 63
  • 4
    Of course, you might want to follow this up with adding the relevant patterns (i.e., *.log) to your .gitignore, so they don't clutter your `git status` if they show up in the future. – Patrick O'Leary Feb 04 '10 at 02:59
  • 2
    Although my problem wasn't related to the same as the OP's: Thanks for letting me know that I would need to use RM to "clean" up my repo after I make changes to .gitignore (if files are already comited.) Noob error, I know, but this was the first place that I personally have seen anyone mention that. – Mike Mar 29 '11 at 11:15
  • Thanks for the feedback. Yeah that's why I wrote it, having come here and then going the long way around to work this out also, thought it would be good to write it up. :) – Evolve Apr 01 '11 at 05:35
20

First of all, as many others already said, your .gitignore should be tracked by Git (and should therefore not be ignored). Let me explain why.

(TL;DR: commit the .gitignore file, and use a global .gitignore to ignore files that are created by your IDE or operating system)

Git is, as you probably already know, a distributed version control system. This means that it allows you to switch back and forth between different versions (even if development has diverged into different branches) and it also allows multiple developers to work on the same project.

Although tracking your .gitignore also has benefits when you switch between snapshots, the most important reason for committing it is that you'll want to share the file with other developers who are working on the same project. By committing the file into Git, other contributers will automatically get the .gitignore file when they clone the repository, so they won't have to worry about accidentally committing a file that shouldn't be committed (such as log files, cache directories, database credentials, etc.). And if at some point the project's .gitignore is updated, they can simply pull in those changes instead of having to edit the file manually.

Of course, there will be some files and folders that you'll want to ignore, but that are specific for you, and don't apply to other developers. However, those should not be in the project's .gitignore. There are two other places where you can ignore files and folders:

  • Files and folders that are created by your operating system or IDE should be placed in a global .gitignore. The benefit is that this .gitignore is applied to all repositories on your computer, so you don't have to repeat this for every repository. And it's not shared with other developers, since they might be using a different operating system and/or IDE.
  • Files that don't belong in the project's .gitignore, nor in the global .gitignore, can be ignored using explicit repository excludes in your_project_directory/.git/info/exclude. This file will not be shared with other developers, and is specific for that single repository
cellepo
  • 4,001
  • 2
  • 38
  • 57
Nic Wortel
  • 11,155
  • 6
  • 60
  • 79
  • Nice mentioning for global `.gitignore` – Gruber Apr 29 '16 at 04:04
  • I think that after initial setup the .gitignore file should be ignored, so there are no accidental changes. It doesn't mean it cannot be changed anymore, but it is protected from accidents, in a way. Accidents on that file can introduce confusion or even endanger some work, so "sealing" (ignoring itself) should be considered also. – Saša Aug 30 '16 at 08:47
  • @Sasa `.gitignore` only works for ignoring files that are not being tracked by Git yet. Adding an already tracked file to `.gitignore` will not prevent you from commiting changes to that file. Even if this were possible, how would you commit the changed `.gitignore` when it instructs Git to ignore itself? – Nic Wortel Aug 30 '16 at 09:01
  • @Nic - I have passed the detailed explanation to my point of view in the separate reply @ the bottom of this thread. – Saša Aug 30 '16 at 13:37
18

Of course the .gitignore file is showing up on the status, because it's untracked, and git sees it as a tasty new file to eat!

Since .gitignore is an untracked file however, it is a candidate to be ignored by git when you put it in .gitignore!

So, the answer is simple: just add the line:

.gitignore # Ignore the hand that feeds!

to your .gitignore file!

And, contrary to August's response, I should say that it's not that the .gitignore file should be in your repository. It just happens that it can be, which is often convenient. And it's probably true that this is the reason .gitignore was created as an alternative to .git/info/exclude, which doesn't have the option to be tracked by the repository. At any rate, how you use your .gitignore file is totally up to you.

For reference, check out the gitignore(5) manpage on kernel.org.

chase
  • 370
  • 3
  • 12
15

The idea is to put files that are specific to your project into the .gitignore file and (as already mentioned) add it to the repository. For example .pyc and .o files, logs that the testsuite creates, some fixtures etc.

For files that your own setup creates but which will not necessarily appear for every user (like .swp files if you use vim, hidden ecplise directories and the like), you should use .git/info/exclude (as already mentioned).

Abdullah
  • 2,015
  • 2
  • 20
  • 29
bayer
  • 6,854
  • 24
  • 35
13

Watch out for the following "problem" Sometimes you want to add directories but no files within those directories. The simple solution is to create a .gitignore with the following content:

*

This seams to work fine until you realize that the directory was not added (as expected to your repository. The reason for that is that the .gitignore will also be ignored, and thereby the directory is empty. Thus, you should do something like this:

*
!.gitignore
Maze
  • 726
  • 6
  • 9
9

This seems to only work for your current directory to get Git to ignore all files from the repository.

update this file

.git/info/exclude 

with your wild card or filename

*pyc
*swp
*~
Naskov
  • 4,121
  • 5
  • 38
  • 62
8

If you've already checked in .gitignore and you want to ignore modifications to it, check out this answer:

Try using this command:

git update-index --assume-unchanged FILENAME_TO_IGNORE

To reverse it (if you ever want to commit changes to it), use:

git update-index --no-assume-unchanged

UPDATE:

Here's how to list 'assume unchanged' files under current directory:

git ls-files -v | grep -E "^[a-z]"

As the -v option will use lowercase letters for 'assume unchanged' files.

Community
  • 1
  • 1
Aaron
  • 2,049
  • 4
  • 28
  • 35
5

In my case, I want to exclude an existing file. Only modifying .gitignore not work. I followed these steps:

git rm --cached dirToFile/file.php
vim .gitignore
git commit -a

In this way, I cleaned from cache the file that I wanted to exclude and after I added it to .gitignore.

felipe.zkn
  • 2,012
  • 7
  • 31
  • 63
Andrea Perdicchia
  • 2,786
  • 1
  • 20
  • 19
  • best solution for me. you can also use git add . in the second place , after that git commit -m "fixing .gitignore" – brainray Apr 30 '15 at 10:01
3

Navigate to the base directory of your git repo and execute the following command:

echo '\\.*' >> .gitignore

All dot files will be ignored, including that pesky .DS_Store if you're on a mac.

ertemplin
  • 897
  • 9
  • 24
weffen
  • 31
  • 1
  • 4
    I wouldn't do that. There might be dot files you need. Instead, I would just add .gitignore and .DS_Store literally. – Edward Falk Jun 28 '13 at 16:51
2

It is quite possible that an end user wants to have Git ignore the ".gitignore" file simply because the IDE specific folders created by Eclipse are probably not the same as NetBeans or another IDE. So to keep the source code IDE antagonistic it makes life easy to have a custom git ignore that isn't shared with the entire team as individual developers might be using different IDE's.

John Brown
  • 189
  • 1
  • 7
1

I found that the best place to set up an ignore to the pesky .DS_Store files is in the .git/info/exclude file.

IntelliJ seems to do this automatically when you set up a git repository in it.

Sergey K.
  • 24,894
  • 13
  • 106
  • 174
1

.gitignore is about ignoring other files. git is about files so this is about ignoring files. However as git works off files this file needs to be there as the mechanism to list the other file names.

If it were called .the_list_of_ignored_files it might be a little more obvious.

An analogy is a list of to-do items that you do NOT want to do. Unless you list them somewhere is some sort of 'to-do' list you won't know about them.

Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
1

I think that there are situations where ignoring the .gitignore is very useful. For instance, when you have multiple teams or a large team working on the same codebase. In that case, you need to have certain conventions, one of those convention is regarding what is ignored at the git repo. It is usually about ignoring files and directories created by IDE or OS, some generated logs, etc.

However, there is a force which is tending to introduce non-conventional changes to .gitignore file. The .gitignore file can be further changed by irresponsible person, by mistake, by a tool that is used, or in some other case.

To have a counter force to this, we can do as followed:

  1. The initial .gitignore should reflect convention in team(s),
  2. After it is pushed, the .gitignore should be secured by adding .gitignore entry and push that change again.The .gitignore file is "sealed" in this way.

The "sealed" .gitignore file can be changed, just locally, without propagating that changers to other members of team(s). However, if a change is widely agreed throughout the whole team(s) than it is possible to "unseal" it, change it and than "seal" it again. That can't be done by mistake, only intentionally.

Sadly, you cannot be 100% protected from the stupidity, but this way you have done everything you can to prevent stupid things to happen.

If you have relatively small team with very good professionals, than this wouldn't be important, but even those guys would appreciate to have one thing less to worry about.

Using .git/info/exclude is cool when you cannot do anything about infrastructure settings, just covering your own a** not to make a mistake.

From a standing point of what is right and what is wrong I am voting for having .gitignore entry inside .gitignore file, giving everybody the freedom to do locally whatever they want, but not invading others.

Saša
  • 4,416
  • 1
  • 27
  • 41