Search code examples
gitgitignore

ignored files still appears as changed, when removing from index, they are removed at other machines


So this happens multiple times now, and want to get rid of it. When my framework makes new files on my system, I do a git status, and see then appearing as "modified", but I don't want them to be added / changed to git, I'll have to add them to the gitignore file, right? But when I do that, they still appears in my git status :S So I searched for this problem, and came across:

https://stackoverflow.com/questions/9750606/git-still-shows-files-as-modified-after-adding-to-gitignore#=

So I tried that:

git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

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:   bootstrap/cache/services.php <<< so these 3 are already in gitignore, but still shows up :S
        modified:   composer.lock <<< so these 3 are already in gitignore, but still shows up :S
        modified:   npm-debug.log <<< so these 3 are already in gitignore, but still shows up :S

no changes added to commit (use "git add" and/or "git commit -a")
folder/projectfolder git rm -r --cached  bootstrap/cache/services.php
rm 'bootstrap/cache/services.php'
folder/projectfolder git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    bootstrap/cache/services.php

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:   composer.lock
        modified:   npm-debug.log

folder/projectfolder git rm -r --cached  composer.lock
rm 'composer.lock'
folder/projectfolder git rm -r --cached npm-debug.log
rm 'npm-debug.log'
folder/projectfolder git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    bootstrap/cache/services.php
        deleted:    composer.lock
        deleted:    npm-debug.log

folder/projectfolder git commit -m "clean gitindex"
[master 084a4e1] clean gitindex
 Committer: ********

 3 files changed, 13823 deletions(-)
 delete mode 100644 bootstrap/cache/services.php
 delete mode 100644 composer.lock
 delete mode 100644 npm-debug.log
folder/projectfolder git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

nothing to commit, working directory clean
folder/projectfolder git push
warning: <<<message and login removed here>>>
Counting objects: 9, done.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 582 bytes | 0 bytes/s, done.
Total 6 (delta 4), reused 0 (delta 0)
To https://*****@bitbucket.org/*****/*****.git
   45ty54y4y54y******y4t43at  master -> master

But after doing this, when my colleague pulls my changes from git, the 3 files are actually deleted from his system, not only from the git index. how can I make git just ignoring all my files in gitignore as it should, without touching filesystems on any machine?

Actually I just only need a "Hey git, go fix your shit and please look at the ignore file before you tell me that things are changed" command ;P

Bart


Solution

  • Sorry to say, but fairly sure that this is working as expected.

    When you did git rm --cached you are telling git to remove the files from the git index, and therefore the repository, but to leave your local versions of them (i.e. the files on your local file system) in place. As a result, they will still live on your machine, however, on the other person's machine, they will be removed as part of the git pull/rebase.

    There is still a larger question here on whether the files need to exist in source control, and whether the changes should indeed by ignored. That question can really only be answered by you and your team.

    If a file needs to exist within source control, but any changes that are done to it should be ignored, then one technique I have used in the past is to do a git revert of those files as part of the build process that I am using. For example, within a psake, or gulp, or Cake script, do a git revert as the final step in the process.

    I have used this technique when I needed the AssemblyInfo.cs file to live in source control, however, as part of the build process this file is updated with the current version number. When running the build locally on developer machines, I didn't want those changes to be included within the changeset, so I used an additional step in my build process to git revert the changes to this file.