3

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

Community
  • 1
  • 1
Bart Huisman
  • 125
  • 1
  • 1
  • 9

1 Answers1

2

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.

Gary Ewan Park
  • 17,610
  • 5
  • 42
  • 60
  • 1
    thanks. is there an other way how I can tell git to "refresh" its "status file" (maybe its not called index?) and look at the ignore file? i just want git to "not do anything with the ignored files, just as the filename implicates, just ignore it, don't tell me it's changed, even when it wasn't in the ignore file before, now it is, so just please ignore it for me and don't tell me its changed when i do a git status..." – Bart Huisman Aug 24 '16 at 08:54
  • 1
    Once a file has been added to the git index, any modifcation to the file will appear in `git status` even though it appears in the .gitignore file. The only way to prevent this would be to remove the file from the index, as you did above and then add it to .gitignore. However, at that point, the file will NOT be in your repository. There is no in between here. The file either exists in the repo and you are tracking changes, or you never add it, and ignore it. – Gary Ewan Park Aug 24 '16 at 08:57
  • In situations where I have HAD to add a file, but I never want to commit any changes to it, I have add a `git revert` as part of the build script I have been running, so that any changes to that file are always undone. – Gary Ewan Park Aug 24 '16 at 08:59
  • "once a file has been added to the git index"... is the only way to not get it added, to add it to gitignore before its added to the filesystem? – Bart Huisman Aug 24 '16 at 09:17
  • what do you mean by your last reply? – Bart Huisman Aug 24 '16 at 09:17
  • Yes, to the best of my knowledge, that is the only way to have it not be added to the index. – Gary Ewan Park Aug 24 '16 at 09:18
  • Assuming we are talking about some code in this repository, you are likely going to have some sort of compilation step, and it is this compilation step that will ultimately modify the files that you are trying to ignore, right? In these situations (as a Windows guy) I would use something like psake or cake to script out the build process. As a final step in this build process I would use a `git revert` command to undo the changes to the file. That way, they would never appear in the output of `git status`. Hope that makes sense. – Gary Ewan Park Aug 24 '16 at 09:21
  • 2
    thanks, we're using php laravel here with composer, gulp and npm. i'll search for way's to add it there then. thanks – Bart Huisman Aug 24 '16 at 09:25
  • You should definitely be able to revert the changes within your gulp script, if that is the route you choose to go down. There is still a larger question about why those files are being modified and whether they are actually valid changes. The only time I have had to do this is when I need to update the version number of the application, which was part of my build task. I didn't want the changes to be included in source control, but I did need the file that contained the version number to be in source control. Have you answered that question within your team? – Gary Ewan Park Aug 24 '16 at 09:28
  • It's mostly in this early developing process that we are adding more and more "basic stuff" that's not in the gitignore yet. When we're done making the base, I guess all new files and folders will be made in existing ignored folders. For example the log file mentioned in the topic start, it was made because I failed in making the npm installation. Instead of deleting it, and the possibility that it maybe comes back in the future, I'd like to add it to the gitingore so there's no chance for it to pop up in my status later. But thats not possible because I havn't add it allready to the gitignore – Bart Huisman Aug 24 '16 at 12:11
  • "But thats not possible because I havn't add it allready to the gitignore" so, to be crystal clear, if you go ahead and remove/delete the file (using the git rm) command, add this file to the gitignore file, then it won't appear again in your `git status` output. Also, you might want to take a look here: https://github.com/github/gitignore this gives a set of standard gitignore files that you can use on a per language basis, which covers the majority of files that you would want to ignore in most projects. – Gary Ewan Park Aug 24 '16 at 12:17
  • 2
    ah, keep it simple stupid... yes, so after that steps, you can run whatever script made that file, again, and it wouldn't be in the status... – Bart Huisman Aug 24 '16 at 12:24
  • or move the file / folder temporarely out of your project folder, add it to gitignore, and add it again.... i'll try this one the next time the problem pops up... – Bart Huisman Aug 24 '16 at 12:26
  • Moving the file isn't enough. You would still need to commit the removal to the repository. Either by committing the deletion of the file, or by using `git rm` After that, you would be able to move it back in again, after you add it to the gitignore. – Gary Ewan Park Aug 24 '16 at 12:31
  • 2
    i thought that, when you don't commit the adding of the file, the removal would be seen as "nothing happend here". in case of changing an existing file, yes, your comment is true, but at this moment most of these "problems" i experience, are adding of new files i didn't know would be added, and then i'm too late with adding it to the gitignore, then my trick will work i guess ;) – Bart Huisman Aug 24 '16 at 12:37
  • Gotcha, yes, in which case that would make sense. I would definitely recommend taking a look at the project I linked to above. It covers all the normal files that you would want to ignore, and takes away a lot of the guess work. I normally always use the latest gitignore file for Visual Studio from there whenever I start a new project. – Gary Ewan Park Aug 24 '16 at 12:39