1

I am working on PhpStorm. There are some files/folders that I would like git to ignore. These are on the remote repository, too. I just don't want to reflect any more changes about them but they have to stay - not be deleted - from remote repo since I am going to pull from this repo to the production server.

  • I have added them to the .gitignore file which is in the project root.
  • I have added them to PhpStorm Settings > Version Control > Ignored Files section as described here https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000154070-How-to-gitignore-idea-files
  • How to ignore files which are in repository? this question describes my problem exactly, yet I have tried both methods which are described in the accepted answer, here is what happened:
    • git rm -r --cached .idea/ I did this and related files/folders were deleted from my remote repository too, which I don't want because I think it will result in deleting those when I pull on the production server.
    • git update-index --assume-unchanged .idea/ I did this, "Ignoring path .idea/" was written in the terminal, I hoped that it would work but when I wanted to commit, I saw that folder again as tracked.

What am I doing wrong?

Note: .idea folder may not be important for the production server, but there are some important folders needed for the code to run.

Adem Tepe
  • 564
  • 5
  • 10

2 Answers2

3

Don't use --assume-unchanged to ignore tracked files

The purpose of the --assume-unchanged flag is to tell Git to avoid checking the files in specific path for changes, because they aren't supposed to change.

From the documentation:

setting this bit on a path does not mean Git will check the contents of the file to see if it has changed — it makes Git to omit any checking and assume it has not changed.

This feature exists as a performance optimization in case your repo has very large files and/or a slow file system.

As Junio C Hamano points out in a discussion thread about the difference between .gitignore and --assume-unchanged:

Assume-unchanged should not be abused for an ignore mechanism. It is "I know my filesystem operations are slow. I'll promise Git that I won't change these paths by making them with that bit.

He goes on:

Especially, it is not a promise by Git that Git will always consider these paths are unmodified---if Git can determine a path that is marked as assume-unchanged has changed without incurring extra lstat(2) cost, it reserves the right to report that the path has been modified (as a result, git commit -a is free to commit that change).

In other words, if a file in a path marked with --assume-unchanged is modified, Git may decide to ignore the flag if doing so isn't too slow of an operation. This is probably why you're seeing the files in the .idea/ directory as modified.

Use --skip-worktree instead

There's another way you can tell Git to ignore the changes in path, and that's by setting the --skip-worktree flag.

From the documentation:

When reading an entry, if it is marked as skip-worktree, then Git pretends its working directory version is up to date and read the index version instead.

You can substitute the word entry with the word path. The difference between --skip-worktree and --assume-unchanged is in the assumption they make: while --assume-unchanged expects the files not to be modified, --skip-worktree doesn't care about the state of the files in the working directory at all.

The documentation says as much:

The working directory version may be present or absent. If present, its content may match against the index version or not.

Once a path as been marked with --skip-worktree, Git will go out of its way to exclude any local modifications made to the files in the working directory from the history of the repo.

Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
  • Actually, I realized that I was doing something wrong; I will explain it as an answer. Nevertheless your explanation is great, I have read it thorougly and learnt well. – Adem Tepe Aug 19 '19 at 14:24
0

I discovered where I was wrong. For those who may need, I will explain it with two folders I have:

  • config/ and config.php under it
  • pdf/ and pdf.css under it

So:

git update-index --skip-worktree config/

Running this and changing config.php didn't help. On the other hand:

git update-index --skip-worktree pdf/

After running this I got a pdf output from my web application and it created pdf.html also under pdf/ folder. So, this new file was ignored, good.

Shortly, I concluded that I had to point out the exact file if that's what I want to be ignored, like this:

git update-index --skip-worktree config/config.php

It ignores any changes to this file afterwards.

(I tried the same with --assume-unchanged, it worked, yet since I have understood the difference very well from the answer above, I wanted to highlight the better way here)

Adem Tepe
  • 564
  • 5
  • 10