John Lehmann
John Lehmann

Reputation: 8225

How to make git ignore certain file conflicts and differences?

We have an automatically generated javascript file which is tracked in our git repository. The problem is that it includes a (useless) timestamp in a comment, which often creates trivial merge conflicts for us. I'd like to avoid having these conflicts.

I thought I had found a solution with .gitattributes and filter/smudge. I set it up in the following way, so that this timestamp line was stripped from the file upon commit or checkout:

.gitattributes:

<the file>.js filter=tsfilter
<the file>.js smudge=tsfilter

.git/config:

[filter "tsfilter"]
      clean = perl -pe \"s/\\/\\/.*<the pattern>.*$//\"
      smudge = perl -pe \"s/\\/\\/.*<the pattern>.*$//\"

While this seems to have eliminated merge conflicts, it introduced another annoyance in that this file now remains in a constant state of modification. That is, 'status' stills shows it as modified since the local (pre-filtered) copy includes timestamp line (but the committed file does not).

Is there a better way to go about this which avoids the merge conflicts but also hides local changes to this part of the file?

Upvotes: 3

Views: 1714

Answers (2)

qqx
qqx

Reputation: 19465

You could modify the program that produces that file to not include the timestamp, or write a wrapper for the program that would remove the timestamp.

Upvotes: 1

cjc343
cjc343

Reputation: 3765

I may be incorrectly interpreting your question, but it sounds like you could make do with either an ours or theirs merge strategy for the file in question. Based on the answer to this question, you could do the following:

.gitattributes:

file.js merge=ours

.git/config:

[merge "ours"]
    name = Keep my file
    driver = true

This will always keep your version of the file.

A theirs merge is only slightly more difficult -- you can't just change the driver to false, you need to define a script to keep their changes. The question linked above covers a theirs strategy in more detail.

Upvotes: 3

Related Questions