Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.gitignore does not work - file is still being tracked [duplicate]

Tags:

git

gitignore

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?

like image 457
Gnu Engineer Avatar asked Jun 17 '11 00:06

Gnu Engineer


People also ask

Why is .gitignore not ignoring my files?

gitignore ignores only untracked files. Your files are marked as modified - meaning they were committed in the past, and git now tracks them. To ignore them, you first need to delete them, git rm them, commit and then ignore them.

How do I Gitignore a file that is already tracked?

Simply move the files to a folder outside of git, then do "git add .", "git commit". (This removed the files) then add the gitignore, referencing the files/folders, commit again to add the gitignore file to git, then copy/move back in the folders, and they should be ignored.

Are Gitignore files cloned?

gitignore is created, the file will still be part of the repository and therefore cloned. Even that's an overstatement: an entry in a . gitignore file is only considered when some file is untracked. A committed file, as (because) it is being checked out, automatically becomes tracked.


2 Answers

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

like image 119
freespace Avatar answered Sep 17 '22 22:09

freespace


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 . 
like image 24
ikegami Avatar answered Sep 16 '22 22:09

ikegami