让git只将一个文件识别为更改的方法(如果有一组以上的特定行发生更改)?

让git只将一个文件识别为更改的方法(如果有一组以上的特定行发生更改)?

问题描述:

在我当前的项目中,我试图使用git来对文本文件进行版本控制,这些文本文件由从中生成代码的软件所利用.这本身不是问题,问题在于每次我生成代码时,它都会使用属性(例如,代码生成的日期以及我的名字)自动更新此文件.

In my current project, I am attempting to use git to version control text files that are utilized by software that generates code from them. This in itself isn't a problem, the problem is that every time I generate code, it automatically updates this file with properties such as the date the code was generated, as well as my name.

您可以想象它看起来像这样:

You can imagine it looking something like this:

SomeHeader{
  -SomeProperty : x
  -NameOfUserThatGenerateCode: myName
  -DateTimeCodeGenerated: 2013-07-23 06:28
  -SomeOtherProperty: y
}

我想要的是一种告诉git如果Name和CodeGeneration时间都更改的情况(也就是:忽略更改)的方法,那就可以了,但是要注意"SomeProperty"是否更改为说"z".

What I want, is a way to tell git to say that "it is okay" if both the Name and CodeGeneration time to change (ie: Ignore that there was a change), but DO care if "SomeProperty" changes to say "z".

在第二种情况下,它将提交整个文件(带有更新的自动生成的文件).

In that second case, it would commit the entire file (with the updated autogenerated files).

反正有这样做吗?我知道git确实会在文件"级别上进行更改,但是我希望可能会有某种我可以绑定到的预处理钩子,只有在git尝试比较文件更改时才能起作用.

Is there anyway to do that? I recognize that git does changes at the "file" level, but I am hoping that there might be some sort of pre-processing hook that I can tie into that would only work when git attempts to compare file changes.

对于那些关心的人,这将使我能够正确地控制狂想曲文件的版本.

For those that care, this will enable me to properly version control rhapsody files.

我建议:

  • keeping a copy of those files (as private files, meaning not versioned): your software would generate code in those copy
  • a clean script, as a content filter driver, declared in a .gitattributes file (detailed in Git Pro book).

想法是要在git add检测该脚本:

The idea is for that script to detect, on git add:

  • "SomeHeader"属性文件的内容
  • 如果该文件的副本已更改,您需要保留(在这种情况下,您将使用该副本的内容覆盖版本控制的文件)
  • 如果该文件的副本没有显着变化,则在这种情况下,您无需修改​​实际的属性文件.
  • the content of a 'SomeHeader' property file
  • if the copy of that file has changed in way you need to keep (in which case you overwrite the versioned file with the content of that copy)
  • if the copy of that file has not changed significantly, in which case, you don't modify the actual property file.

请注意,也可以通过内容过滤器驱动程序自动执行保留副本"部分,并在git checkout上自动激活smudge脚本.

Note that the "keep a copy" part can be automated also by a content filter driver, with a smudge script activated automatically on git checkout.