Can git ignore some specific line from git status?

Viewed 577

I need to ignore some lines from some files which contains timestamp when thoses files are generated. Sometimes we generates thoses files but the only change in the file is the timestamp which we don't care and the file should not be targeted to be commited by the git status.

I know I can use git filters like on this topic : How to tell git to ignore individual lines, i.e. gitignore for specific lines of code

But in that case it's deleting lines when I do git add. I want thoses lines to be commited if there is another change in the file than the timestamp (timestamp line will be ignored). But if the change is only the timestamp line I don't want the file to be commited.

How I can do that using git filters ?

Thanks

1 Answers

Use text conversion to exclude noise when looking for differences, then you can add only the files with changes you care about. git add doesn't automate running diffs to detect changes, it's done often and needs to be fast, but scripting it's easy.

Set a custom diff attribute value on files where you're (un)interested in specific differences, configure that diff's text conversion, run your diffs to detect files that have changes you care about, add those.

Say sed 's,^#Timestamp:.*',#Timestamp:<whatever>,' will make all timestamp lines identical, so if you run that on content before checking for differences, timestamp changes won't show up, even though they're there in the files.

Put

*.xyz diff=notimestamp

in an attributes file, for repo-local work like initial testing and such .git/info/attributes is the right spot.

Then configure the notimestamp diff preprocessing in .git/config:

[diff "notimestamp"]
        textconv = "sed 's,^#Timestamp:.*,#Timestamp:<whatever>,'"

and now whenever git runs a full diff on a *.xyz file it won't see timestamp changes. So you can

git diff | sed -n '/^diff/s,.* b/,,p'

to see a list of files that have changes you do care about, and add those (you could for instance replace the diff header with git add rather than nothing and just run the generated commands).

Related