Home > Enterprise >  Ignore content changes to file after pushed to git repository
Ignore content changes to file after pushed to git repository

Time:07-01

I have a file users.csv which is pushed to the repository. This csv file gets updated every time the test suite is run.

  • I don't want the file to be shown as modified in my local machine every time the test suite is run. In that case i assume i can use the command

    git update-index --assume-unchanged [<file> ...] 
    

Ref: Can I 'git commit' a file and ignore its content changes?

  • Now, assume that another developer pulled all the changes that i pushed. When the test suite is run on his machine, the same csv should not show as modified and the file should be still there in his machine.

I assume adding the file in .gitignore will not help since the file is already tracked.

Any idea on how to achieve this?

CodePudding user response:

Ideally, the test suite should:

  • copy users.csv to user.csv.test (with user.csv.test in the .gitignore, so left as a private file)
  • modify user.csv.test

That way, you don't have to manage update-index on each developer repositories.

CodePudding user response:

Git specifically doesn't offer a way to ignore changes to tracked files. This is thoroughly answered in the Git FAQ:

Git doesn’t provide a way to do this. The reason is that if Git needs to overwrite this file, such as during a checkout, it doesn’t know whether the changes to the file are precious and should be kept, or whether they are irrelevant and can safely be destroyed. Therefore, it has to take the safe route and always preserve them.

It’s tempting to try to use certain features of git update-index, namely the assume-unchanged and skip-worktree bits, but these don’t work properly for this purpose and shouldn’t be used this way.

You are correct that .gitignore has no effect on files that are already tracked.

There are a couple ways you can avoid having a problem here. The first is to use a temporary directory (or an ignored directory like test/tmp) for your test suite input and output to avoid this being a problem. This is what Git does and several other projects do as well.

You could also leave the file users.csv ignored and move the real file to a different location, and then copy it into place before use. Then, if the test suite modifies it, it won't be a problem on development systems.

  • Related