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

时间:2010-04-20 07:09:43

标签: git

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

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

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

3 个答案:

答案 0 :(得分:11)

告诉git假设文件未更改:

$ git update-index --assume-unchanged FILE [FILE ...]

从手册:

   --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.

How to Ignore Changes in Tracked Files With Git

中找到

答案 1 :(得分:9)

您可以使用smudge/clean processgitattribute filter driver,描述为in ProGit

clean smudge

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

答案 2 :(得分:2)

基于git的方法:使用生产分支并在那里保留特定于您的生产环境的更改。要发布到生产环境,请将主分支合并到生产分支中。

基于部署的方法:使用工具(例如capistrano / cfengine / ...)来自动化部署过程。这有许多优点,其中之一就是能够将配置与代码分开。