0

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?

Snedden27
  • 1,870
  • 7
  • 32
  • 60
  • It does not, but [setting up sparse checkout does](https://stackoverflow.com/a/37066742/2303202) – max630 Apr 18 '17 at 09:21

2 Answers2

1

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.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
  • I can't gitignore the file . Right now I am stashing my local file everything I pull and poping the stash back once the pull is done . But I wished I could do this in a easier way – Snedden27 Apr 18 '17 at 03:15
  • Well, if you insist on relying on manual procedures to avoid committing changes that you mean to be local, it is only a matter of time between failures. Oh, but you're good enough to never make a mistake? Cool. Is everyone you work with (or ever will)? Look, I don't care what you do with your repo, but don't ask a question and then pooh pooh what is in fact the correct way of handling the problem. (Did you notice the other answer is actually giving exactly the same advice?) – Mark Adelsberger Apr 18 '17 at 12:28
1

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:

  • The config file is not changed locally, but only updated in remote, when you execute git pull, the config file will be update as remote.
  • The config file both changed in local and remote, when you execute 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
  • The config file only changed locally, when you want to checkout other branches, git will show
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.

Marina Liu
  • 36,876
  • 5
  • 61
  • 74
  • Thanks for detailed answer, this has got me confused though as I had assume marking a file as skip-worktree would make it be ignore by git pulls and branch checkouts . If this is the case I don't understand the utility of git update-index --skip-worktree – Snedden27 Apr 18 '17 at 03:13
  • Skip-worktree pretends its working directory version is up to date and read the index version instead. But when you use `git pull` or `git checkout branch`, the current index is trying to be discard, so skip-worktree will stop working. – Marina Liu Apr 18 '17 at 05:13