6

I'm trying to commit the right files in git, but having problems configuring my gitignore properly. I followed the instructions here to create the gitignore file (django project):

# File types #
##############
*.pyc
*.swo
*.swp
*.swn

# Directories #
###############
logs/

# Specific files #
##################
projectname/settings.py

# OS generated files #
######################
.DS_Store
ehthumbs.db
Icon
Thumbs.db
*~

The problem is that settings.py is getting included in the commit:

Admin$ git add .
Admin$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   projectname/settings.py

How can I ignore settings in my gitignore?

Kristaps Karlsons
  • 482
  • 1
  • 7
  • 22
Nick B
  • 9,267
  • 17
  • 64
  • 105
  • possible duplicate of [.gitignore file not ignoring](http://stackoverflow.com/questions/1139762/gitignore-file-not-ignoring) – Enrico Oct 29 '13 at 18:37

3 Answers3

17

It looks like you already have settings.py under version control by git. In this case git will continue to track the file - no matter what you write in .gitignore.

You have to explicitly tell git to forget about settings.py:

  1. Add it to .gitignore (As you did)
  2. Remove the file from git without deleting the file: git rm --cached projectname/settings.py
  3. Commit the change: git commit -m "remove settings.py"

Afterwards git will ignore the file. But be aware that versions which are already commited will stay in your repository.

Scolytus
  • 16,338
  • 6
  • 46
  • 69
  • Ok well what should I do to fix it so that the settings isn't included in the future? – Nick B Oct 29 '13 at 18:47
  • 2
    This is probably the correct answer, people often misunderstand this. **If the file is already versioned git will keep tracking it even if there is a match for it on .gitignore; you have to manually remove it, and then, now, git will ignore it.** – talles Oct 29 '13 at 20:20
  • 1
    @talles, there is another way, without removing it ... `git update-index --assume-unchanged path/to/file` – Markku K. Oct 29 '13 at 20:51
  • If you want to remove the file in a commit but not in the current directory: `cp file tmp; git rm file; git commit ...; mv tmp file` is the hard way :-) and the easy way is `git rm --cached file`. – torek Oct 29 '13 at 21:34
0

Try adding / before your directory

# Specific files #
##################
/projectname/settings.py

For any further information,

$ mkdir git_test
$ cd git_test/
~/git_test $ git init
Initialized empty Git repository in /home/linux/git_test/.git/
~/git_test $ touch .gitignore
~/git_test $ vim .gitignore
~/git_test $ cat .gitignore 
/aa/aa.py

~/git_test $ mkdir aa
~/git_test $ touch aa/aa.py
~/git_test $ 
~/git_test $ 
~/git_test $ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   .gitignore
nothing added to commit but untracked files present (use "git add" to track)

If you still facing the same issue:

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

Siva Cn
  • 929
  • 4
  • 10
  • Thanks for the idea, but unfortunately it is still being included in the commit. Any ideas about how to fix this? Thanks! – Nick B Oct 29 '13 at 18:47
  • see this post: http://stackoverflow.com/questions/11508445/how-to-ignore-existing-committed-files-in-git – Siva Cn Oct 29 '13 at 18:50
0

For a file that is already versioned, the GitHub help page suggests using git update-index --assume-unchanged projectname/settings.py, rather than .gitignore.

Markku K.
  • 3,840
  • 19
  • 20