你怎么告诉git永久忽略文件中的变化?

Mal*_*ous 18 git

我正在使用一个存储网站数据的git存储库.它包含一个.htaccess文件,其中包含一些适用于生产服务器的值.为了让我在网站上工作,我必须更改文件中的一些值,但我从不想提交这些更改,否则我将破坏服务器.

由于.gitignore不适用于跟踪文件,我使用"git update-index --assume-unchanged .htaccess"来忽略我在文件中的更改,但是这只能在切换分支之前有效.一旦您更改回原始分支,您的更改就会丢失.

有没有办法告诉git忽略文件中的更改在更改分支时不管它?(就像文件未被跟踪一样.)

Fra*_*eil 11

只需告诉git假设文件未更改:

$ git update-index --assume-unchanged FILE [FILE ...]
Run Code Online (Sandbox Code Playgroud)

从手册:

   --assume-unchanged, --no-assume-unchanged
       When these flags are specified, the object names recorded for the paths are not updated. Instead, these options set and unset the "assume unchanged" bit for the paths. When the "assume unchanged" bit is on, git stops checking the
       working tree files for possible modifications, so you need to manually unset the bit to tell git when you change the working tree file. This is sometimes helpful when working with a big project on a filesystem that has very slow
       lstat(2) system call (e.g. cifs).

       This option can be also used as a coarse file-level mechanism to ignore uncommitted changes in tracked files (akin to what .gitignore does for untracked files). Git will fail (gracefully) in case it needs to modify this file in the
       index e.g. when merging in a commit; thus, in case the assumed-untracked file is changed upstream, you will need to handle the situation manually.
Run Code Online (Sandbox Code Playgroud)

如何使用Git忽略跟踪文件中的更改中找到

  • 仅供参考,问题中提到了这一点,因为它不起作用......从那时起有什么改变使它现在起作用吗? (2认同)

Von*_*onC 9

您可以使用涂抹/清理过程(一个gitattribute过滤器驱动程序,在ProGit中描述)

干净的污迹

每次更新工作目录时,您都有机会.htaccess用预定义的内容替换您的内容,以更正您当前的环境.
在干净的阶段,您将恢复原始内容,就像您从未接触过该文件一样.

  • 完美的!这解决了我的问题。我在 .git/info/attributes 中添加了“.htaccess filter=htaccess”,然后在 .git/config 中添加了: [filter "htaccess"] clean = sed 's|/mypath|/dev' smudge = sed 's| /dev|/mypath' 现在 .htaccess 文件中对 /dev/something 的任何引用都会悄悄地替换为 /mypath/something,并在最后恢复,这意味着我什至可以编辑该文件并提交我的更改,而无需获取存储库中的 /mypath 版本! (2认同)