Ignoring server config files in git

佐手、 提交于 2020-01-03 02:49:12

问题


I have a file in my web project that sets up the site db connection. Obviously, this has different settings for the staging and live branches of the site, and I need it to contain my local dev settings on my machine. The file needs to be in the repo, but now that it's there it doesn't need to be tracked.

I've followed the advice in this question (and others): How to ignore files only locally in git? which has stopped git telling me about changes on a given branch, but if I try to change branches, say from my current bug fixing branch into staging, or if I try to merge staging into the current branch, git warns me that the local changes will be overwritten, and I'm locked in the branch.

So far the only 'solution' I've found has been to un-ignore the file, and commit the differences, or modify my local file so it matches the server branch.

What's the best way to get all branches ignoring the config file?


回答1:


Trying to ignore local-only changes to tracked files in git will only result in problems.

Options like --assume-unchanged and --skip-worktree only have influence when doing git status, when git reads change from the filesystem. When you do operations like git checkout, git will not ignore changes to these files, giving you the errors you run into.

In my experience (and others), it's better to not track the actual config files. In stead, it's better to track a template under a different name. This prevents a lot of issues of overwriting the actual config file or having to exclude / ignore files when copying / extracting files.




回答2:


Try instead, as in this answer:

git update-index --skip-worktree -- <local-only_file>

I detail the difference between the two update-index options (--assume-unchanged and --skip-worktree) in "How do you make Git ignore files without using .gitignore?"




回答3:


You can use the assume-unchanged flag
https://git-scm.com/docs/git-update-index

--[no-]assume-unchanged

When this flag is specified, the object names recorded for the paths are not updated.

Instead, this option sets/unsets the "assume unchanged" bit for the paths.

When the "assume unchanged" bit is on, the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index. If you want to change the working tree file, you need to unset the bit to tell Git. This is sometimes helpful when working with a big project on a filesystem that has very slow lstat(2) system call (e.g. cifs).

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.



来源:https://stackoverflow.com/questions/37966898/ignoring-server-config-files-in-git

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!