4

The question may seem absurd but I have a doubt. I'm not a git expert but I've use it every day for 5 years via the command-line so I'm pretty used to it.

Here is the scenario I can't solve:

  • A valuable colleague of mine started a WordPress project. He commited every single file in the repo... bad idea.
  • I come on the project now, clone the project (so my index is super clean). I start by adding few entries in the .gitignore file and commit it. In these rules, i have added "wp-config.php", the regular WP config file, which will change on each instance (local/preprod/prod). Easy.
  • But then I change this specific file, with my local config:

    me@myproject$ vi wp-config.php 
    me@myproject$ git st
    On branch develop
    Your branch is up-to-date with 'origin/develop'.
    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:   wp-config.php
    
    no changes added to commit (use "git add" and/or "git commit -a")
    

Why is he doing that? Why isn't he ignoring my files? After few reading, I saw that some fellows does a file deletion, like :

git rm -r --cached wp-config.php

Sure we can do that but then we have to commit the file deletion, cause the index is spotting the file as deleted (not physically ofc). Is it the standard way to ignore an existing file in a git repo?

Thanks for your helps.

Here is a copy of my .gitignore file:

# Ignore all logs files.
*.log

# Ignore the instance-based configuration.
wp-config.php

# Ignore cache, backups and uploaded files.
wp-content/advanced-cache.php
wp-content/backup-db/
wp-content/backups/
wp-content/blogs.dir/
wp-content/cache/
wp-content/upgrade/
wp-content/uploads/
wp-content/wp-cache-config.php
wp-content/plugins/hello.php

# Ignore some site-based root files.
/.htaccess
/sitemap.xml
/sitemap.xml.gz

# Ignore editor files.
.idea

# Ignore OS generated files.
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
Andrew Brēza
  • 7,705
  • 3
  • 34
  • 40
AntoineV
  • 61
  • 2
  • See this answer https://stackoverflow.com/a/20241145/6330106. `git update-index --skip-worktree ` has a similar effect. `--no-skip-wortree` or `--no-assume-unchanged` cancels the effect when you'd like to track the changes. – ElpieKay Nov 17 '17 at 09:36
  • 5
    Possible duplicate of [How to make Git "forget" about a file that was tracked but is now in .gitignore?](https://stackoverflow.com/questions/1274057/how-to-make-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitignore) – phd Nov 17 '17 at 09:41

1 Answers1

0

You need to git rm --cached path/to/file and commit. If you have a lot of files, you can run the following from the repository root to remove the ignored files from the index: git rm --cached $(git ls-files -i --exclude-from=.gitignore)

The second command retrieves the ignored files as processed by .gitignore and inserts those paths into the git rm --cached command. Note if you have nested .gitignores in child directories, you point to that .gitignore file which will granularly clean up those individual directories.

The reason his files aren't deleted inherently is because .gitignore prevents automatically adding files matching patterns in the .gitignore file. It will not inherently remove files which are already tracked in the index.

codewario
  • 19,553
  • 20
  • 90
  • 159
  • Do note that if there *are* any cases of ignored files you want to keep in the working tree, this will remove those files. You can individually execute `git reset ` before you commit on each file to keep those exceptions tracked. – codewario Nov 17 '17 at 16:10