84

I've a .gitignore file at the root of my repo. The .gitignore file has the following pattern to exclude the compiled Python files and that is the only line in the file.

*.pyc

Now when i do the following at the root of the repo.

git init
git add . 
git status 

It shows that it still tracks the .pyc file and tries to add it as new file. See output below.

System info: Windows 7, cygwin

Note: This issue is CLEARLY not about the ignored file being already tracked. I also tried both DOS- and Unix-style line endings on the .gitignore file.

git status gives:

# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   .gitignore
#   new file:   feedapp/__init__.py
#   new file:   feedapp/appconfig.py
#   new file:   feedapp/appconfig.pyc

How do I troubleshoot this further?

7
  • What is the output from 'git status'? That'll help a lot. Commented Jun 17, 2011 at 0:36
  • was the .gitignore file in place and populated before your git add .? Commented Jun 17, 2011 at 0:46
  • 13
    What do you get from git rm --cached -r . ; git add . ; git status Commented Jun 17, 2011 at 0:58
  • 1
    Do you have any whitespace at the end of the line containing *.pyc in your .gitignore file? I've just done a little test and it seems to be significant. Commented Jun 17, 2011 at 2:07
  • 1
    refer to below post, it works fine to me: stackoverflow.com/questions/1139762/gitignore-file-not-ignoring Commented Sep 4, 2012 at 7:23

5 Answers 5

142

.gitignore only applies to untracked files. If you are tracking a .pyc then .gitignore won't apply. Remove the .pyc with git rm and next time you do a git status it (and any others) won't show up in the list of untracked file and nor will it be automatically added.


Otherwise if you need to ignore a file already under version control, update the index to ignore changes to files already under version control:

git update-index --assume-unchanged <files>

For more information please see git-update-index(1) Manual Page, the related answer to .gitignore file not ignoring and the related answer to question (GIT: Ignoring Version-Controlled Files).

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

7 Comments

These files are not tracked yet....
According to your 'git status' output, they are.
@Gnu Engineer, They are tracked. They are in the index, a staging area created by git add. Whether they were placed there before .gitignore was created or not is unknowable.
ikegami: Problem is resolved. It seems to due to with the format of the line ending. I had created the .gitignore with an editor and later recreated via echo. I did a d2u now and it worked it. Thanks for your help.
Thanks! Did not know of the git update-index - really useful!
|
37

man gitignore:

A gitignore file specifies intentionally **untracked files** that git should ignore. Note that all the gitignore files really concern only files that are not already tracked by git

git rm file will stop tracking them. I can't find a way to remove all ignored files from the repo.


As you point out, the files don't appear to already exist in the repo. In that case, your git's behaviour matches neither the documentation or the behaviour of mine, and I can't help you.

$ mkdir foo
$ cd foo
/home/ikegami/foo
$ mkdir feedapp
$ touch feedapp/__init__.py
$ touch feedapp/appconfig.py
$ touch feedapp/appconfig.pyc
$ echo '*.pyc' > .gitignore
$ git init
Initialized empty Git repository in /home/ikegami/foo/.git/
$ git add .
$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   .gitignore
#       new file:   feedapp/__init__.py
#       new file:   feedapp/appconfig.py
#
$ 

Perhaps you did

git init
git add .
echo '*.pmc' >> .gitignore
git init
git add .

in which case you can fix the problem using

git rm --cached -r .
git add .

4 Comments

My understanding is that the git is not tracking any files in repo yet. I'm trying to do an initial commit ignoring the *.pyc files.
ikegami: I've the same conclusion as yours. All along i've done this several times before. I suspect this has something to do with cygwin but yet this is a very simple pattern that it has to support....
@Gnu Engineer, I use git over cygwin at home. I'll try it there later. For now, I added a possible cause and solution to the bottom of my answer.
be cautioned when using git rm --cached -r . it actually removes everything on current directory
17

I had the exact same problem. I found out that running "someText" > .gitignore made a file with a weird encoding. I changed it to UTF-8, then all worked fine.

1 Comment

Also make sure there is no BOM! (Byte order mark)
6

I have had exactly the same problem--even if I prepare a .gitignore file before the first time I add anything. And I found the problem was in the blank space in my ignore file. DO NOT add any white space in your ignore file and try again. I think everything will just work fine. Good luck

Comments

5

Check the line endings on your .gitignore file. In an OS X repo, mine was using CR line endings. Everything worked again after switching over to LFs.

perl -p -e 's/\r/\n/g'  < .gitignore > .gitignore-new
mv .gitignore-new .gitignore

Console jockeys will likely want to use sed instead.

Background: I had copied my working copy from a Windows repository after a power failure.

1 Comment

Same for me. If you are using vim, do :set ff=unix. For some reason it was set to mac.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.