0

I have a problem that's almost identical to this one. The only difference being that I'm not sure what Mercurial is, and I'm just using Git. In my repository is an empty .htpasswd file that should remain empty on the remote repository, but locally should be given a username and password value.

My .gitignore contains the following line:

.htpasswd

So in theory it should ignore my file, right? But that is not what's happening in my case. When I change anything in this file, my Git will still recognize this file as changed, and add it to the list of unstaged changes. I want to prevent this from happening. How can I achieve that?

EDIT: I consider the solution in the linked question as a last resort. Since I'm not sure how similar (or not) Mercurial is to Git, I'm wondering if there is a better solution available for Git.

Metoniem
  • 239
  • 2
  • 15
  • https://stackoverflow.com/search?q=%5Bgit%5D+track+file+ignore+changes – phd Mar 11 '20 at 15:47
  • 1
    Git and Mercurial behave identically here. In both systems, the "ignore" file does not mean *ignore changes*, it means *do not complain about the file being untracked* (and, if it is untracked, do not auto-add it with any of the "add all untracked" operations). Once the file is tracked, it's *tracked*. Git does have the ability to *assume* a file is unchanged, or skip it in the work-tree, but that's a much worse alternative than the thing you're thinking of as a last resort, which really is a *first* resort. – torek Mar 11 '20 at 17:09
  • @torek thanks for that! I'm still quite new to Git so your comment helped me to better understand the way gitignore works :) – Metoniem Mar 12 '20 at 08:13

1 Answers1

1

Git doesn't provide a way to ignore changes to tracked files. The documentation specifically recommends the solution that answers to other questions have provided. There are techniques people use to try to trick Git, but that same documentation passage explains why those don't work and why the right solution is to use separate files.

bk2204
  • 64,793
  • 6
  • 84
  • 100
  • Thanks for your answer! It seems like I will indeed use a `.template` version of my file instead, and I should probably implement a check somewhere that checks if the 'real' file exists. – Metoniem Mar 12 '20 at 08:20