23

I'm working with a git repository that's storing data for a website. It contains a .htaccess file, with some values that are suitable for the production server. In order for me to work on the site, I have to change some values in the file, but I never want to commit these changes or I will break the server.

Since .gitignore doesn't work for tracked files, I was using "git update-index --assume-unchanged .htaccess" to ignore my changes in the file, however this only works until you switch branches. Once you change back to your original branch, your changes are lost.

Is there some way of telling git to ignore changes in a file and leave it alone when changing branches? (Just as if the file was untracked.)

Malvineous
  • 25,144
  • 16
  • 116
  • 151

3 Answers3

13

Simply tell git to assume the file is unchanged:

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

From the manual:

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

Found in How to Ignore Changes in Tracked Files With Git

François Beausoleil
  • 16,265
  • 11
  • 67
  • 90
11

You could use a smudge/clean process (a gitattribute filter driver, described in ProGit)

clean smudge

Each time you update your working directory, you would have the opportunity to replace the content of your .htaccess with a predefined one, correct for your current environment.
In the clean stage, you would then restore the original content, as if you had never touched that file.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    Perfect! This solved my problem. I added ".htaccess filter=htaccess" into .git/info/attributes, then in .git/config I added: [filter "htaccess"] clean = sed 's|/mypath|/dev' smudge = sed 's|/dev|/mypath' And now any references to /dev/something in the .htaccess file are quietly replaced with /mypath/something, and restored at the end which means I can even edit the file and commit my changes, without getting the /mypath version in the repo! – Malvineous Apr 21 '10 at 01:37
2

git based approach: use a production branch and keep the changes specific to your production environment in there. To release to production, merge your master branch into your production branch.

deployment based approach: use a tool (such as capistrano/cfengine/...) to automate your deployment process. This has many advantages, one of them is the ability to keep configuration separate from your code.

Michael Reinsch
  • 579
  • 4
  • 8
  • I currently use the git-based approach (with a production branch) but each developer has a separate .htaccess file for their development environment. Even with a separate production branch, we still have to manually avoid committing our custom .htaccess files into the master branch. Granted this won't break the server, but it clutters the repository when it happens. – Malvineous Apr 21 '10 at 01:18