Reputation: 1920
I want a specific config file in my project to be have different configs in the local environment than to the prod environment and I don't want to change or stash/pop the file every time I push something to the main repository
I found out that using :
git update-index --skip-worktree <file>
lets me do exactly that.
I have one question though , In a scenario where someone else changes the config file and pushes the changes to the main repositary and the next time I pull the changes would my local config file be updated?
Upvotes: 0
Views: 807
Reputation: 38106
If the config file is useless to do version control (since you need to config differently in local) you'd better ignore the file in .gitignore
:
touch .gitignore
echo <file> >> .gitignore
git rm <file> --cached
git add .
git commit -m 'message'
git push
The reason why not use git update-index --skip-worktree <file>
as below:
git pull
, the config file will be update as remote.git pull
, git will detect conflict and shows error as: error: Your local changes to the following files would be overwritten by merge: file Please commit your changes or stash them before you merge. Aborting
error: Your local changes to the following files would be overwritten by checkout: file Please commit your changes or stash them before you switch branches. Aborting
git update-index --skip-worktree
is usually for temporarily use.You can also compare the usage of git update-index --skip-worktree
here.
Upvotes: 1
Reputation: 45659
The documentation is a little unclear here; in my tests (using version 2.12.0.windows.1) I am unable to pull if the remote contains changes to a file I've marked with --skip-worktree
. (I get the usual error for operations that would overwrite local changes. I tried various ways to resolve this, but the only one that worked was to clear the skip-worktree bit, which of course would put you back to square one on protecting the local changes.)
Personally I think this solution is pretty messy. You might be better off arranging things so that either (a) environment-specific settings are not source controlled (i.e. stored at paths covered by .gitignore
or that are outside the repo), or (b) you keep versions of the files for each environment and the means of selecting the "active" one is not source controlled.
Upvotes: 1