2010-04-20 165 views
15

我正在使用存儲網站數據的git存儲庫。它包含一個.htaccess文件,其中一些值適用於生產服務器。爲了讓我在網站上工作,我必須更改文件中的某些值,但我從不想提交這些更改,否則我會中斷服務器。如何讓git永久忽略文件中的更改?

由於.gitignore不適用於跟蹤文件,因此我使用「git update-index --assume-unchanged .htaccess」來忽略文件中的更改,但只有在切換分支時纔有效。一旦你改回原來的分支,你的改變就會丟失。

是否有某種方式告訴git忽略文件中的更改在更改分支時保持不變? (就像如果文件未被跟蹤。)

回答

9

你可以使用一個smudge/clean process(一gitattribute filter driver,描述in ProGit

clean smudge

每次更新你的工作目錄,你將有機會取代您的.htaccess的內容與預定義的內容一致,適用於您當前的環境。
在清理階段,您將恢復原始內容,就好像您從未觸摸過該文件一樣。

+1

完美的存在!這解決了我的問題。我在.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版本! – Malvineous 2010-04-21 01:37:46

2

基於git的方法:使用生產分支並保留特定於您的生產環境的更改。要釋放生產,請將主分支合併到生產分支中。

基於部署的方法:使用工具(如capistrano/cfengine/...)自動執行部署過程。這具有許多優點,其中之一是可以將配置與代碼分開。

+0

我目前使用基於git的方法(使用生產分支),但每個開發人員爲其開發環境都有單獨的.htaccess文件。即使使用單獨的生產分支,我們仍然必須手動避免將我們的自定義.htaccess文件提交到主分支。當然,這不會破壞服務器,但當它發生時它會使存儲庫變得混亂。 – Malvineous 2010-04-21 01:18:19

7

只需告訴混帳假設該文件是不變的:

$ 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

+0

僅供參考在這個問題中提到了這個問題,因爲沒有工作......自那時以來有所改變,現在就開始工作了? – Malvineous 2013-01-17 03:52:44

+0

@Malvineous,我誤解了這個問題的部分,但顯然,它現在的作品:https://gist.github.com/4564516 – 2013-01-18 13:23:59

+0

對於任何人想知道,再次跟蹤文件使用'--no-assume-unchanged' 。 – SUPERCILEX 2017-05-13 18:51:58