Proper git procedure for changed files that should

2020-02-26 12:49发布

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.

标签: git git-svn
5条回答
时光不老,我们不散
2楼-- · 2020-02-26 13:24

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
查看更多
男人必须洒脱
3楼-- · 2020-02-26 13:27

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

查看更多
何必那么认真
4楼-- · 2020-02-26 13:37

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.

查看更多
Bombasti
5楼-- · 2020-02-26 13:38

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.

查看更多
够拽才男人
6楼-- · 2020-02-26 13:43

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.

查看更多
登录 后发表回答