git .ignore not working in a directory

Git

Git Problem Overview


I have the following .gitignore file

# file named .gitignore

system/application/config/database.php
system/application/config/config.php
system/logs
modules/recaptcha/config/*

However when I add any changes in config and recapcha.php, git status warns me as following. When I add any changes to database.php. it does not show it in status

shin@shin-Latitude-E5420:/var/www/myapp$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	modified:   modules/recaptcha/config/recaptcha.php
#	modified:   system/application/config/config.php
#
no changes added to commit (use "git add" and/or "git commit -a")

How can I fix this?

Thanks in advance.

I am using git version 1.7.5.1 on Ubuntu 11.04

Git Solutions


Solution 1 - Git

The files are already stored in your local commit tree. You'll need to first remove them from the repository. This can be done via:

git rm --cached system/application/config/config.php modules/recaptcha/config/recaptcha.php

After that you'll need to make one more commit and you're good to go.

Solution 2 - Git

Do the following to trigger the gitignore

Step 1: Commit all your pending changes in the repo which you want to fix and push that.

Step 2: Now you need to remove everything from the git index in order to refresh your git repository. This is safe. Use this command:

git rm -r --cached .

Step 3: Now you need to add everything back into the repo, which can be done using this command:

git add .

Step 4: Finally you need to commit these changes, using this command:

git commit -m "Gitignore issue fixed"

Solution 3 - Git

If your file is already in git before you add the directory to .gitignore git will keep tracking it. If you don't want it, do a "git rm" to remove it first.

Solution 4 - Git

I'm guessing yout .gitignore is not in the root of your working tree.

If you put a .gitignore in the same directory as system/application/config/config.php you can just use

echo config.php >> system/application/config/.gitignore

The paths are relative to the current directory (or: .gitignore is a per-directory exclude/include file).

See man git-ignore

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionshinView Question on Stackoverflow
Solution 1 - GitDipSwitchView Answer on Stackoverflow
Solution 2 - GitNanda GopalView Answer on Stackoverflow
Solution 3 - GitEric HogueView Answer on Stackoverflow
Solution 4 - GitseheView Answer on Stackoverflow