0

I have follwoing dir structure

enter image description here

and here is content of .gitignore

conf.php

The problem is, when I change contents conf.php and try to commit something, GIT detects conf.php as changed file again. In fact it must be ignored.
I have commited it once. I want to ignore it from now. What am I missing?

Update

I tried following:

  1. Added conf.php to gitignore

  2. removed it from cache git rm --cached conf.php.

    But now, when I rescan project, it wants to stage conf.php as removed.

enter image description here

Its not what I want.

I want to keep it on remote repo and ignore future (from now) changes in local repo.

Community
  • 1
  • 1
heron
  • 3,611
  • 25
  • 80
  • 148
  • 1
    What I usually do with config files is track (for example) a `config-sample.php` file. This contains what a config file *should* look like, but I never reference it directly. I'm always referencing `config.php` (which is in `.gitignore`). This way, each dev can have their own config, while having an example of what the file should contain tracked and in the repo. – Ryan Kinal Oct 25 '12 at 15:55
  • there's a similar question, and interesting answer using filter-branch http://stackoverflow.com/a/6535471/1125394 –  Oct 25 '12 at 16:14

3 Answers3

6

Git will not ignore tracked files

Git will not ignore changes to already-committed files.

What Git ignore is for

Git ignore, ignores noise that's in your repo, for example:

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

If the .gitignore file is modified to ignore the tmp directory:

$ echo "tmp" > .gitignore
$ 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)

the tmp dir contents are nolonger listed - but the .gitignore file IS listed (because I just created it and the file itself is not ignored).

Committing that, there are no changes listed:

$ git add .gitignore
$ git commit -v -m "addding git ignore file"
[master (root-commit) d7af571] addding git ignore file
1 file changed, 1 insertion(+)
    create mode 100644 .gitignore
$ git status
# On branch master
    nothing to commit (working directory clean)

Now any changes to the .gitignore file will show up as pending changes, any new files outside the tmp dir will show up as untracked files and any changes inside the tmp dir will be ignored.

Don't commit files that you don't want to track

If you've already added conf.php to your git repo, git is tracking the file. When it changes git will say that it has pending changes.

the solution to such things is to NOT commit files you don't want to track. Instead what you can do though, is to commit a default file, e.g.:

$ mv conf.php conf.php.default
$ # Edit conf.php.default to be in an appropriate state if necessary
$ git commit -v -m "moving conf.php file"
$ cp conf.php.default conf.php

Now any changes you make to conf.php - will not show up as unstaged changes.

Community
  • 1
  • 1
AD7six
  • 63,116
  • 12
  • 91
  • 123
  • 3
    I have commited it once. I want to ignore it from now. Is that possible? – heron Oct 25 '12 at 14:25
  • no. You need to fix that by following the suggestion in the answer or though some process workaround. Why have you committed a file that you want to modify? – AD7six Oct 25 '12 at 14:32
  • I tried following: Added conf.php to gitignore, removed it from cache git rm --cached conf.php. But now, when I rescan project, it wants to stage conf.php as removed. screencast.com/t/3RnpABKdWk .Its not what I want. I want to keep it on remote repo and ignore future (from now) changes in local repo. – heron Oct 25 '12 at 15:02
  • 1
    git does not work like that, that is not how git works, you can't do that, "no". – AD7six Oct 25 '12 at 15:20
  • You can't have tracking differences on the remote vs local repo. You have to remember that the local repo is where things happen. Nothing can happen remotely without pushing a change first – sciritai Oct 25 '12 at 15:57
4

if you want to add a empty config file to your project and then not track any additonal changes then you can do this:

  1. edit conf.php to be in the state you want it to stay
  2. add and commit the config.php

run the following git command:

git update-index --assume-unchanged conf.php

You will no longer see conf.php as having pending changes

To start tracking changes again, run this command:

git update-index --no-assume-unchanged conf.php
NSjonas
  • 10,693
  • 9
  • 66
  • 92
3

Just run git rm --cached conf.php

If you have more commited files to ignore:

git rm -r --cached .

git add .


Then commit and push your changes.

Eugene Naydenov
  • 7,165
  • 2
  • 25
  • 43
  • 1
    I have commited it once. I want to ignore it from now. Is that possible? – heron Oct 25 '12 at 14:28
  • Yes. `.gitignore` is not forcing git to ignore already commited files. It's exactly what you need - add a rule to `.gitignore`, then run `git rm` command as I showed and commit changes. – Eugene Naydenov Oct 25 '12 at 14:50
  • I tried following: Added conf.php to gitignore, removed it from cache `git rm --cached conf.php`. But now, when I rescan project, it wants to stage conf.php as removed. http://screencast.com/t/3RnpABKdWk .Its not what I want. I want to keep it on remote repo and ignore future (from now) changes in local repo. – heron Oct 25 '12 at 15:01
  • Try to give solution for my case – heron Oct 25 '12 at 15:31