foxneSs
foxneSs

Reputation: 2269

How to deal with files that are changed with zero impact in git?

I have a couple of files in my repository that change every other commit but these changes literally don't change anything. Let me explain. For example, I have a file called ethereal with the following contents:

foo
bar
kappa

I make a commit and then the mysterious entity changes the file to this:

foo, bar,
kappa

These files are exactly the same for the project.

The mysterious entity changes them for no reason. But here's a thing: git doesn't know that these files are the same.

That's why almost every commit includes a meaningless change of the file ethereal.

And no, adding it to .gitignore won't help because ethereal has to be in the project repository.

The commit clutter becomes a real problem.
The only solution I found is typing git update-index --assume-unchanged ethereal before every commit.

But I read here that you are not supposed to do that often. Now imagine that there are 20 files like that.

There's gotta be a better way of dealing with this.

Upvotes: 1

Views: 178

Answers (1)

CodeWizard
CodeWizard

Reputation: 142064

The mysterious entity changes them for no reason. But here's a thing: git doesn't know that these files are the same.

Git will see content as changed even if a single byte has been modified.
In your case the files do change in some way, so they are marked as modified.


You can do one of the following:

  1. .gitignore - wont work for you as you described why
  2. --assume-unchanged you already using it.
  3. smudge / clean is here for your rescue

Smudge

Read all about it and to set it up here:
https://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes

It turns out that you can write your own filters for doing substitutions in files on commit/checkout.

These are called clean and smudge filters.

In the .gitattributes file, you can set a filter for particular paths and then set up scripts that will process files just before they’re checked out (“smudge”, see Figure 8-2) and just before they’re staged (“clean”, see Figure 8-3).

These filters can be set to do all sorts of fun things.

enter image description here

Upvotes: 2

Related Questions