91

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?

I am using git version 1.7.5.1 on Ubuntu 11.04

starball
  • 20,030
  • 7
  • 43
  • 238
shin
  • 31,901
  • 69
  • 184
  • 271
  • possible duplicate of [Git: Remove a file from the repository without deleting it from the local filesystem](http://stackoverflow.com/questions/1143796/git-remove-a-file-from-the-repository-without-deleting-it-from-the-local-filesy) – Cascabel Feb 01 '12 at 20:31

4 Answers4

144

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.

Ryan
  • 29
  • 1
  • 4
DipSwitch
  • 5,470
  • 2
  • 20
  • 24
  • 5
    Thanks. git rm removed a file. But adding --cached solved the problem. What do I need to do if I want to add it again? Do I just need to take out from .gitignore file? – shin May 17 '11 at 12:53
123

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"
Nanda Gopal
  • 2,519
  • 1
  • 17
  • 25
12

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.

Eric Hogue
  • 8,800
  • 4
  • 26
  • 21
  • 5
    Won't `git rm` (without `--cached`) also remove the file? Or does it leave it because it is listed in the `.gitignore` file? – DipSwitch May 17 '11 at 12:26
  • You should probably back it up somewhere before. After you rm and commit, copy it back. But I didn't know about the --cached option, I will look at it. – Eric Hogue May 17 '11 at 12:40
3

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

sehe
  • 374,641
  • 47
  • 450
  • 633