3

I just inherited an existing codebase that has multiple configuration files in it. These configuration files are all generic, meant to be edited and customized for each machine they are downloaded to. Git is (obviously) telling me that they have been edited and is always showing them under "changes not staged for commit" every time I do a git status. How can I ignore these files under git? Adding them to .gitignore doesn't work because they are already being tracked by git. I don't want to commit anything to the repo, but at the same time I want to tell git not to track these files anymore on my local machine. Is there a way of doing this that I'm not aware of? I know I can git stash them and they won't be shown anymore, but I feel that is not really what it was meant for... Am I wrong?

Not sure if this changes anything, but I am using git-svn on my local machine to interact with the SVN server.

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
jnevelson
  • 2,092
  • 2
  • 21
  • 31

5 Answers5

4

Don't store configuration files on the repository. You can store example versions of those files or 'generic' versions of them in the repository, but do it under another name.

If there are several of them and they live in their own directory, create a separate directory to hold the generic versions in the repository. If there are only one or two of them and they don't have their own directory, use file names. You can add something like -default or -example to the versions stored in the repository.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
4

To ignore files already tracked by git I have a git alias set up (which actually just does what birryree suggests, but more conveniently). Add this to your .gitconfig:

[alias]
  ignore = !git update-index --assume-unchanged
  unignore = !git update-index --no-assume-unchanged
  ignored = !git ls-files -v | grep "^[[:lower:]]"

Then you can do git ignore foo, git unignore foo, or git ignored to list the ignored files.

Abe Voelker
  • 30,124
  • 14
  • 81
  • 98
  • "ignore" may be a confusing verb for these aliases, given that `git update-index --assume-unchanged` is very different from the ignore / exclude mechanism. – Mark Longair Oct 18 '11 at 06:10
  • This is awesome, thanks. However, I like to remove the files too so they don't appear in rgreps. maybe hardignore rm the file and mark it as unchanged. – Drew Jan 11 '12 at 16:55
  • ignore is as clear a verb as it could be, if anything the underlying git api is confusing and misleading. Especially around finding hidden files. – Drew Jan 11 '12 at 17:00
2

I do something like David Schwartz suggests, which is to create something like shell/template configurations like database.yml.template and then copy that file and configure it for my environment as database.yml. Then I can .gitignore those new files.

If you want to do it without modifying how the files exist in repo, then:

git update-index --assume-unchanged [filename]

Will make git ignore changes to that file, but you have to be conscious of doing it.

More information here: http://gitready.com/intermediate/2009/02/18/temporarily-ignoring-files.html

wkl
  • 77,184
  • 16
  • 165
  • 176
  • That's exactly was what I was looking for, thanks. I know storing config files in the repo isn't the best idea, but currently they are being symlinked to other directories. Copying them and editing them probably is a better way, but we were trying to be as portable as possible. We will look into alternatives. Thanks! – jnevelson Oct 17 '11 at 17:45
  • This isn't the correct answer. You want to use the skip-worktree bit, not the assume-unchanged bit, as the skip-worktree bit is designed for precisely this situation. See my answer below for more information. – Tim Bellis Dec 20 '12 at 18:11
2

My suggestion would be:

  1. Rename *.properties to *.properties.example
  2. Add *.properties.example to repository
  3. Delete *.properties from repository
  4. Add *.properties to .gitignore
Heinrich Filter
  • 5,760
  • 1
  • 33
  • 34
  • 1
    +1, although it's worth being aware of the [risk of ignoring a file which was committed in the repostory in earlier versions](http://stackoverflow.com/questions/7482013/using-git-in-a-production-server/7486368#7486368). – Mark Longair Oct 18 '11 at 06:12
0

You want to use the skip-worktree bit, not the assume-unchanged bit.

git update-index --skip-worktree [filename]

See this question for why this is preferable to assume-unchanged, and this webpage for some of the ramifications.

Basically, skip-worktree tries very hard to keep your changes to the file, but assume-unchanged might lose them when you perform some operations.

Community
  • 1
  • 1
Tim Bellis
  • 1,607
  • 3
  • 14
  • 24