views:

19949

answers:

10

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" appears in the list of untracked files. Why is that?

+6  A: 

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
+93  A: 

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.

August Lilleaas
Shouldn't this be part of the repository's metadata rather than a file that is tracked?
endolith
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
+47  A: 

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

phjr
+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
+9  A: 

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

bayer
+12  A: 

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.

1800 INFORMATION
This doesn't seem to work.
ehsanul
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.
sheepsimulator
+1  A: 

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 *~

+8  A: 

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 suite your OS

1) Add to your ~/.gitconfig file

[core]
excludesfile = /home/username/.gitignore

2) Create a ~/.gitignore file with file patterns to be ignored

2) 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

Alec the Geek
+3  A: 

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
+3  A: 

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: http://stackoverflow.com/questions/1158857/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)

Evolve
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
A: 

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.

weffen